Skip to content

Commit 39835c5

Browse files
authored
Refactor client.py to use user_agent instead of access_token
Removed access_token parameter and added user_agent parameter to the client class.
1 parent 850db80 commit 39835c5

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

wikidata/client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ def __init__(self,
100100
None] = None,
101101
entity_type_guess: bool = True,
102102
cache_policy: CachePolicy = NullCachePolicy(),
103-
repr_string: Optional[str] = None,
104-
access_token: Optional[str] = None) -> None:
103+
repr_string: Optional[str] = None
104+
user_agent: Optional[str] = 'WikidataClientPython (https://github.com/dahlia/wikidata; hong@minhee.org)'
105+
) -> None:
105106
self._using_default_opener = opener is None
106107
if self._using_default_opener:
107108
if urllib.request._opener is None: # type: ignore
@@ -123,7 +124,7 @@ def __init__(self,
123124
self.identity_map = cast(MutableMapping[EntityId, Entity],
124125
weakref.WeakValueDictionary())
125126
self.repr_string = repr_string
126-
self.access_token = access_token
127+
self.user_agent = user_agent
127128

128129
def get(self, entity_id: EntityId, load: bool = False) -> Entity:
129130
"""Get a Wikidata entity by its :class:`~.entity.EntityId`.
@@ -200,12 +201,12 @@ def request(self, path: str) -> Union[
200201
result = self.cache_policy.get(CacheKey(url))
201202
if result is None:
202203
logger.debug('%r: no cache; make a request...', url)
203-
req = urllib.request.Request(url)
204-
req.add_header('Content-Type', 'application/json')
205-
if self.access_token:
206-
req.add_header('Authorization', f'Bearer {self.access_token}')
204+
self.opener.addheaders = [(
205+
'User-Agent',
206+
self.user_agent
207+
)]
207208
try:
208-
response = self.opener.open(req)
209+
response = self.opener.open(url)
209210
except urllib.error.HTTPError as e:
210211
logger.debug('HTTP error code: %s', e.code, exc_info=True)
211212
if e.code == 400 and b'Invalid ID' in e.read():

0 commit comments

Comments
 (0)