-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththothapi.py
More file actions
59 lines (46 loc) · 1.78 KB
/
thothapi.py
File metadata and controls
59 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""Helpers for configuring and patching Thoth client access."""
from os import environ
def get_thoth_client_url(client_url=None):
"""
Return the Thoth API base URL expected by thothlibrary.
Accept either a base URL such as `https://api.thoth.pub` or a GraphQL URL
such as `https://api.thoth.pub/graphql`.
"""
resolved_url = client_url or environ.get('THOTH_API_URL')
if resolved_url is None:
return None
stripped_url = resolved_url.rstrip('/')
if stripped_url.endswith('/graphql'):
return stripped_url[:-8]
return stripped_url
def patch_thoth_client_queries():
"""
Patch known query mismatches between thothlibrary 1.0.0 and the launch schema.
The released client still requests `workFeaturedVideos` on `Work`, but the
launch schema exposes a singular `featuredVideo` field. thoth-dissemination
does not consume featured-video data, so removing that selection is safe.
"""
from thothlibrary import ThothClient
for query_name in [
'work',
'workByDoi',
'bookByDoi',
'chapterByDoi',
'works',
'books',
'chapters',
]:
query_spec = ThothClient.QUERIES.get(query_name)
if query_spec is None or 'fields' not in query_spec:
continue
query_spec['fields'] = [
field for field in query_spec['fields']
if not field.lstrip().startswith('workFeaturedVideos ')
]
def get_thoth_client(client_url=None):
"""Instantiate a patched Thoth client using an optional endpoint override."""
from thothlibrary import ThothClient
patch_thoth_client_queries()
resolved_url = get_thoth_client_url(client_url)
return ThothClient(resolved_url) if resolved_url else ThothClient()