mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 01:14:15 +02:00
gem selector sketch
This commit is contained in:
40
bauh/api/http.py
Normal file
40
bauh/api/http.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import logging
|
||||
import time
|
||||
import traceback
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class HttpClient:
|
||||
|
||||
def __init__(self, logger: logging.Logger, max_attempts: int = 2, timeout: int = 30, sleep: float = 0.5):
|
||||
self.max_attempts = max_attempts
|
||||
self.session = requests.Session()
|
||||
self.timeout = timeout
|
||||
self.sleep = sleep
|
||||
self.logger = logger
|
||||
|
||||
def get(self, url: str):
|
||||
cur_attempts = 1
|
||||
|
||||
while cur_attempts <= self.max_attempts:
|
||||
cur_attempts += 1
|
||||
|
||||
try:
|
||||
res = self.session.get(url, timeout=self.timeout)
|
||||
|
||||
if res.status_code == 200:
|
||||
return res
|
||||
|
||||
if self.sleep > 0:
|
||||
time.sleep(self.sleep)
|
||||
except:
|
||||
self.logger.error("Could not retrieve data from '{}'".format(url))
|
||||
traceback.print_exc()
|
||||
continue
|
||||
|
||||
self.logger.warning("Could not retrieve data from '{}'".format(url))
|
||||
|
||||
def get_json(self, url: str):
|
||||
res = self.get(url)
|
||||
return res.json() if res else None
|
||||
Reference in New Issue
Block a user