-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
51 lines (31 loc) · 1.38 KB
/
api.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
import requests, logging, time, os, ujson
from queries import initialize_query
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
logger = logging.getLogger("listhub-data-capture")
class ListHubClient:
def __init__(self, access_token, query):
self.authorization_header = {"Authorization": f"Bearer {access_token}"}
self.query = query
@classmethod
def create(cls, query_type):
payload = {
"client_id": os.getenv("LISTHUB_CLIENT_ID"),
"client_secret": os.getenv("LISTHUB_CLIENT_SECRET"),
"grant_type": "client_credentials",
}
response = requests.post("https://api.listhub.com/oauth2/token", data=payload)
query = initialize_query(query_type)
return cls(response.json()["access_token"], query)
def get(self, url):
headers = {**self.authorization_header, **{"User-Agent": "listhub-data-capture/1.0.0"}}
start_time = time.perf_counter()
retry_strategy = Retry(total=3, backoff_factor=2)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
response = http.get(url, headers=headers, timeout=(3.05, 57))
return response
def get_listings(self, skip_url=None):
url = self.query(skip_url)
return self.get(url())