-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
31 lines (25 loc) · 1015 Bytes
/
api_client.py
File metadata and controls
31 lines (25 loc) · 1015 Bytes
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
import logging
import time
import requests
import slumber
log = logging.getLogger(__name__)
class APISession(requests.Session):
"""Logging wrapper around requests session"""
def request(self, method, url, **kwargs):
start = time.time()
response = super().request(method, url, **kwargs)
duration = int(1000 * (time.time() - start))
log.info(
"[AM-Vision API] %s %s %s %dms",
method, url, response.status_code, duration
)
if 400 <= response.status_code <= 499:
log.warning("AMvision Error message: %s", response.content)
return response
class APIClient(slumber.API):
def __init__(self, url, token):
# this example adds error printing and response time tracking
super().__init__(url, session=APISession())
# add authentication header to slumber's default api client
self._store['session'].auth = None
self._store['session'].headers['Authorization'] = "Token " + token