-
Notifications
You must be signed in to change notification settings - Fork 3
/
giosg.py
79 lines (67 loc) · 2.44 KB
/
giosg.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
"""
Defines helper methods for making requests to giosg HTTP API endpoints.
"""
from conf import SERVICE_URL
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import requests
class APIClient:
def __init__(self, auth):
self.auth = auth
def list(self, url):
return list(self.iterate(url))
def retrieve(self, url):
session = retry_session(2)
response = session.get(SERVICE_URL + url, headers={
'Authorization': '{token_type} {access_token}'.format(**self.auth),
})
response.raise_for_status()
return response.json()
def create(self, url, payload):
session = retry_session(2)
response = session.post(SERVICE_URL + url, json=payload, headers={
'Authorization': '{token_type} {access_token}'.format(**self.auth),
})
response.raise_for_status()
return response.json() if response.text else None
def update(self, url, payload):
session = retry_session(2)
response = session.patch(SERVICE_URL + url, json=payload, headers={
'Authorization': '{token_type} {access_token}'.format(**self.auth),
})
response.raise_for_status()
return response.json() if response.text else None
def destroy(self, url):
session = retry_session(2)
response = session.delete(SERVICE_URL + url, headers={
'Authorization': '{token_type} {access_token}'.format(**self.auth),
})
response.raise_for_status()
return response.json() if response.text else None
def search(self, url, checker):
for result in self.iterate(url):
if checker(result):
return result
return None
def iterate(self, url):
while url:
page = self.retrieve(url)
for result in page['results']:
yield result
url = page['next']
def retry_session(retries, session=None, backoff_factor=0.3, status_forcelist=(500, 502, 503, 504)):
"""
Custom implementation of requests to enable retry policy
"""
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session