-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathapi_v4.py
More file actions
273 lines (215 loc) · 7.6 KB
/
api_v4.py
File metadata and controls
273 lines (215 loc) · 7.6 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import logging
import os
import ssl
import typing as T
from json import dumps
import requests
from requests.adapters import HTTPAdapter
LOG = logging.getLogger(__name__)
MAPILLARY_CLIENT_TOKEN = os.getenv(
"MAPILLARY_CLIENT_TOKEN", "MLY|5675152195860640|6b02c72e6e3c801e5603ab0495623282"
)
MAPILLARY_GRAPH_API_ENDPOINT = os.getenv(
"MAPILLARY_GRAPH_API_ENDPOINT", "https://graph.mapillary.com"
)
REQUESTS_TIMEOUT = 60 # 1 minutes
USE_SYSTEM_CERTS: bool = False
class HTTPSystemCertsAdapter(HTTPAdapter):
"""
This adapter uses the system's certificate store instead of the certifi module.
The implementation is based on the project https://pypi.org/project/pip-system-certs/,
which has a system-wide effect.
"""
def init_poolmanager(self, *args, **kwargs):
ssl_context = ssl.create_default_context()
ssl_context.load_default_certs()
kwargs["ssl_context"] = ssl_context
super().init_poolmanager(*args, **kwargs)
def cert_verify(self, *args, **kwargs):
super().cert_verify(*args, **kwargs)
# By default Python requests uses the ca_certs from the certifi module
# But we want to use the certificate store instead.
# By clearing the ca_certs variable we force it to fall back on that behaviour (handled in urllib3)
if "conn" in kwargs:
conn = kwargs["conn"]
else:
conn = args[0]
conn.ca_certs = None
@T.overload
def _truncate(s: bytes, limit: int = 512) -> bytes: ...
@T.overload
def _truncate(s: str, limit: int = 512) -> str: ...
def _truncate(s, limit=512):
if limit < len(s):
remaining = len(s) - limit
if isinstance(s, bytes):
return (
s[:limit]
+ b"..."
+ f"({remaining} more bytes truncated)".encode("utf-8")
)
else:
return str(s[:limit]) + f"...({remaining} more chars truncated)"
else:
return s
def _sanitize(headers: T.Dict):
new_headers = {}
for k, v in headers.items():
if k.lower() in [
"authorization",
"cookie",
"x-fb-access-token",
"access-token",
"access_token",
"password",
]:
new_headers[k] = "[REDACTED]"
else:
new_headers[k] = _truncate(v)
return new_headers
def _log_debug_request(
method: str,
url: str,
json: T.Optional[T.Dict] = None,
params: T.Optional[T.Dict] = None,
headers: T.Optional[T.Dict] = None,
timeout: T.Any = None,
):
if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
return
msg = f"HTTP {method} {url}"
if USE_SYSTEM_CERTS:
msg += " (w/sys_certs)"
if json:
t = _truncate(dumps(_sanitize(json)))
msg += f" JSON={t}"
if params:
msg += f" PARAMS={_sanitize(params)}"
if headers:
msg += f" HEADERS={_sanitize(headers)}"
if timeout is not None:
msg += f" TIMEOUT={timeout}"
LOG.debug(msg)
def _log_debug_response(resp: requests.Response):
if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
return
data: T.Union[str, bytes]
try:
data = _truncate(dumps(_sanitize(resp.json())))
except Exception:
data = _truncate(resp.content)
LOG.debug(f"HTTP {resp.status_code} ({resp.reason}): %s", data)
def readable_http_error(ex: requests.HTTPError) -> str:
req = ex.request
resp = ex.response
data: T.Union[str, bytes]
try:
data = _truncate(dumps(_sanitize(resp.json())))
except Exception:
data = _truncate(resp.content)
return f"{req.method} {resp.url} => {resp.status_code} ({resp.reason}): {str(data)}"
def request_post(
url: str,
data: T.Optional[T.Any] = None,
json: T.Optional[dict] = None,
**kwargs,
) -> requests.Response:
global USE_SYSTEM_CERTS
_log_debug_request(
"POST",
url,
json=json,
params=kwargs.get("params"),
headers=kwargs.get("headers"),
timeout=kwargs.get("timeout"),
)
if USE_SYSTEM_CERTS:
with requests.Session() as session:
session.mount("https://", HTTPSystemCertsAdapter())
resp = session.post(url, data=data, json=json, **kwargs)
else:
try:
resp = requests.post(url, data=data, json=json, **kwargs)
except requests.exceptions.SSLError as ex:
if "SSLCertVerificationError" not in str(ex):
raise ex
USE_SYSTEM_CERTS = True
# HTTPSConnectionPool(host='graph.mapillary.com', port=443): Max retries exceeded with url: /login (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1018)')))
LOG.warning(
"SSL error occurred, falling back to system SSL certificates: %s", ex
)
return request_post(url, data=data, json=json, **kwargs)
_log_debug_response(resp)
return resp
def request_get(
url: str,
params: T.Optional[dict] = None,
**kwargs,
) -> requests.Response:
global USE_SYSTEM_CERTS
_log_debug_request(
"GET",
url,
params=kwargs.get("params"),
headers=kwargs.get("headers"),
timeout=kwargs.get("timeout"),
)
if USE_SYSTEM_CERTS:
with requests.Session() as session:
session.mount("https://", HTTPSystemCertsAdapter())
resp = session.get(url, params=params, **kwargs)
else:
try:
resp = requests.get(url, params=params, **kwargs)
except requests.exceptions.SSLError as ex:
if "SSLCertVerificationError" not in str(ex):
raise ex
USE_SYSTEM_CERTS = True
# HTTPSConnectionPool(host='graph.mapillary.com', port=443): Max retries exceeded with url: /login (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1018)')))
LOG.warning(
"SSL error occurred, falling back to system SSL certificates: %s", ex
)
resp = request_get(url, params=params, **kwargs)
_log_debug_response(resp)
return resp
def get_upload_token(email: str, password: str) -> requests.Response:
resp = request_post(
f"{MAPILLARY_GRAPH_API_ENDPOINT}/login",
headers={"Authorization": f"OAuth {MAPILLARY_CLIENT_TOKEN}"},
json={"email": email, "password": password, "locale": "en_US"},
timeout=REQUESTS_TIMEOUT,
)
resp.raise_for_status()
return resp
def fetch_organization(
user_access_token: str, organization_id: T.Union[int, str]
) -> requests.Response:
resp = request_get(
f"{MAPILLARY_GRAPH_API_ENDPOINT}/{organization_id}",
params={
"fields": ",".join(["slug", "description", "name"]),
},
headers={
"Authorization": f"OAuth {user_access_token}",
},
timeout=REQUESTS_TIMEOUT,
)
resp.raise_for_status()
return resp
ActionType = T.Literal[
"upload_started_upload", "upload_finished_upload", "upload_failed_upload"
]
def log_event(action_type: ActionType, properties: T.Dict) -> requests.Response:
resp = request_post(
f"{MAPILLARY_GRAPH_API_ENDPOINT}/logging",
json={
"action_type": action_type,
"properties": properties,
},
headers={
"Authorization": f"OAuth {MAPILLARY_CLIENT_TOKEN}",
},
timeout=REQUESTS_TIMEOUT,
)
resp.raise_for_status()
return resp