-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikidata_request_executor.py
154 lines (128 loc) · 5.87 KB
/
wikidata_request_executor.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import time
import requests
import logging
import email.utils
import uuid
from datetime import datetime, timedelta, timezone
from .return_types import LiteralReturnType, StringLiteralReturnType, UriReturnType, DatatypeLiteralReturnType
class URITooLongException(Exception):
pass
class WikidataRequestExecutor:
def __init__(self, owning_endpoint):
self._owner = owning_endpoint
self._on_error = None
self._on_timeout = None
self.response = None
self.query = None
self.id = uuid.uuid1()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if isinstance(exc_type, TypeError):
self._invoke_on_error(exc_val)
self._owner.return_executor(self)
def get(self, query, on_error=None, on_timeout=None):
self._set_callbacks(on_error, on_timeout)
self.query = query
max_query_len = self._owner.config().max_query_length()
if len(query) > max_query_len:
self._invoke_on_error(
URITooLongException(f"Request URI exceeds maximum length of {max_query_len} (currently {len(query)})"))
self._wait_while_blocked()
try:
self.response = requests.get(self._owner.config().remote_url(), params={"format": "json", "query": query},
timeout=self._owner.config().request_timeout(),
headers={"User-Agent": str(self.id)})
return self._unpack_results()
except requests.Timeout:
self._invoke_on_timeout()
return []
def post(self, query, on_error=None, on_timeout=None):
self._set_callbacks(on_error, on_timeout)
self.query = query
self._wait_while_blocked()
try:
self.response = requests.post(self._owner.config().remote_url(), params={"format": "json"},
data={"query": query}, timeout=self._owner.config().request_timeout(),
headers={"User-Agent": str(self.id)})
return self._unpack_results()
except requests.Timeout:
self._invoke_on_timeout()
return []
def _set_callbacks(self, on_error, on_timeout):
self._on_error = on_error
self._on_timeout = on_timeout
def _wait_while_blocked(self):
if not self._owner.execution_blocked_until:
return
delta = self._owner.execution_blocked_until - datetime.now()
time.sleep(max(0, delta.total_seconds()))
@staticmethod
def _parse_unpacked_results(query_result):
if query_result['type'] == 'uri':
return UriReturnType(query_result['value'])
elif query_result['type'] == 'bnode':
# ToDo: Handle bnode properly?
return LiteralReturnType(query_result['value'])
elif query_result['type'] == 'literal':
if 'xml:lang' in query_result.keys():
return StringLiteralReturnType(query_result['value'], query_result['xml:lang'])
if 'datatype' in query_result.keys():
return DatatypeLiteralReturnType(query_result['value'], query_result['datatype'])
else:
return LiteralReturnType(query_result['value'])
else:
raise Exception(f"Invalid type {query_result['type']}")
def _unpack_results(self):
if self.response.status_code != 200:
self._invoke_on_error(None)
if self._owner.config().with_return_type():
return self._unpack_results_with_type()
else:
return self._unpack_results_without_type()
def _unpack_results_without_type(self):
try:
for query_result in self.response.json()["results"]["bindings"]:
yield {key: query_result[key]["value"] for key in query_result.keys()}
except Exception as e:
self._invoke_on_error(e)
return []
def _unpack_results_with_type(self):
try:
for query_result in self.response.json()["results"]["bindings"]:
yield {key: self._parse_unpacked_results(query_result[key]) for key in query_result.keys()}
except Exception as e:
self._invoke_on_error(e)
return []
def _invoke_on_timeout(self):
logging.error(f"Timeout during request against {self._owner.config().remote_url()}")
if self._on_timeout:
self._on_timeout(self)
def _invoke_on_error(self, error):
printed_error = error.__str__() if error else "No Error"
printed_status_code = self.response.status_code if self.response else "No Response"
logging.error(f"Error during request against"
f"{self._owner.config().remote_url()}: [{printed_status_code}] {printed_error}")
if self._propagate_faulty_request() and self._on_error:
self._on_error(self, error)
def _propagate_faulty_request(self):
if not self.response:
return True
if self.response.status_code == 429:
self._owner.execution_blocked_until = WikidataRequestExecutor._parse_http_retry(
self.response.headers["Retry-After"])
logging.warning(f"Rate limit exceeded. Blocked until {self._owner.execution_blocked_until}")
return False
if self.response.status_code == 500:
if "java.util.concurrent.TimeoutException" in self.response.content:
self._invoke_on_timeout()
return False
return True
@staticmethod
def _parse_http_retry(retry_header):
if not retry_header:
return None
gmt_timestamp = email.utils.parsedate_to_datetime(retry_header)
if not gmt_timestamp:
return datetime.now() + timedelta(seconds=int(retry_header))
return gmt_timestamp.astimezone(datetime.now(timezone.utc).astimezone().tzinfo)