create a decorator the the re-try / push-backs from server instead of copying.
github-actions opened this issue · comments
create a decorator the the re-try / push-backs from server instead of copying.
LiuAlgoTrader/liualgotrader/trading/alpaca.py
Line 421 in e611b57
f"HTTP ERROR {response.status_code} from ALPACA BROKERAGE API with error {response.text}"
)
async def _get_request(self, url: str) -> Dict:
response = requests.get(
url=url,
auth=HTTPBasicAuth(
self.alpaca_brokage_api_key, self.alpaca_brokage_api_secret
),
)
if response.status_code in (429, 504):
if "x-ratelimit-reset" in response.headers:
tlog(
f"ALPACA BROKERAGE rate-limit till {response.headers['x-ratelimit-reset']}"
)
asyncio.sleep(
int(time.time())
- int(response.headers["x-ratelimit-reset"])
)
tlog("ALPACA BROKERAGE going to retry")
else:
tlog(
f"ALPACA BROKERAGE push-back w/ {response.status_code} and no x-ratelimit-reset header"
)
asyncio.sleep(10.0)
return await self._get_request(url)
if response.status_code in (200, 201, 204):
return response.json()
raise AssertionError(
f"HTTP ERROR {response.status_code} from ALPACA BROKERAGE API with error {response.text}"
)
async def _delete_request(self, url: str) -> int:
response = requests.delete(
url=url,
auth=HTTPBasicAuth(
self.alpaca_brokage_api_key, self.alpaca_brokage_api_secret
),
)
# TODO: create a decorator the the re-try / push-backs from server instead of copying.
if response.status_code in (429, 504):
if "x-ratelimit-reset" in response.headers:
tlog(
f"ALPACA BROKERAGE rate-limit till {response.headers['x-ratelimit-reset']}"
)
asyncio.sleep(
int(time.time())
- int(response.headers["x-ratelimit-reset"])
)
tlog("ALPACA BROKERAGE going to retry")
else:
tlog(
f"ALPACA BROKERAGE push-back w/ {response.status_code} and no x-ratelimit-reset header"
)
asyncio.sleep(10.0)
return await self._delete_request(url)
return response.status_code
async def _order_on_behalf(
self,
symbol: str,
c7309cf16bb54c1b0912f276d0ecdab17cf17e41
duplicate