mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-09-01 05:33:49 +08:00
tools: add retry mechanism for API requests (#1480)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
|
from requests.adapters import HTTPAdapter, Retry
|
||||||
API_HOST = os.getenv('API_HOST', 'https://api.commadotai.com')
|
API_HOST = os.getenv('API_HOST', 'https://api.commadotai.com')
|
||||||
|
|
||||||
# TODO: this should be merged into common.api
|
# TODO: this should be merged into common.api
|
||||||
@@ -11,6 +12,9 @@ class CommaApi:
|
|||||||
if token:
|
if token:
|
||||||
self.session.headers['Authorization'] = 'JWT ' + token
|
self.session.headers['Authorization'] = 'JWT ' + token
|
||||||
|
|
||||||
|
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
||||||
|
self.session.mount('https://', HTTPAdapter(max_retries=retries))
|
||||||
|
|
||||||
def request(self, method, endpoint, **kwargs):
|
def request(self, method, endpoint, **kwargs):
|
||||||
with self.session.request(method, API_HOST + '/' + endpoint, **kwargs) as resp:
|
with self.session.request(method, API_HOST + '/' + endpoint, **kwargs) as resp:
|
||||||
resp_json = resp.json()
|
resp_json = resp.json()
|
||||||
|
|||||||
Reference in New Issue
Block a user