Skip to content

Commit d2697eb

Browse files
authored
Merge pull request #65 from webysther/patch-2
2 parents 0a3e0ec + 6b28851 commit d2697eb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

wikidata/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +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) -> None:
103+
repr_string: Optional[str] = None,
104+
user_agent: Optional[str] = 'WikidataClientPython (https://github.com/dahlia/wikidata; hong@minhee.org)'
105+
) -> None:
104106
self._using_default_opener = opener is None
105107
if self._using_default_opener:
106108
if urllib.request._opener is None: # type: ignore
@@ -122,6 +124,7 @@ def __init__(self,
122124
self.identity_map = cast(MutableMapping[EntityId, Entity],
123125
weakref.WeakValueDictionary())
124126
self.repr_string = repr_string
127+
self.user_agent = user_agent
125128

126129
def get(self, entity_id: EntityId, load: bool = False) -> Entity:
127130
"""Get a Wikidata entity by its :class:`~.entity.EntityId`.
@@ -198,6 +201,10 @@ def request(self, path: str) -> Union[
198201
result = self.cache_policy.get(CacheKey(url))
199202
if result is None:
200203
logger.debug('%r: no cache; make a request...', url)
204+
self.opener.addheaders = [(
205+
'User-Agent',
206+
self.user_agent
207+
)]
201208
try:
202209
response = self.opener.open(url)
203210
except urllib.error.HTTPError as e:

0 commit comments

Comments
 (0)