Skip to content

Commit

Permalink
Remove extra retry logic
Browse files Browse the repository at this point in the history
The base http client already contains retry logic for the 429 errors
  • Loading branch information
phalbert committed Nov 6, 2024
1 parent 349424f commit 0baa218
Showing 1 changed file with 15 additions and 49 deletions.
64 changes: 15 additions & 49 deletions integrations/terraform-cloud/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import asyncio
import httpx

from enum import StrEnum
from typing import Any, AsyncGenerator, Optional
from aiolimiter import AsyncLimiter
Expand Down Expand Up @@ -49,54 +46,23 @@ async def send_api_request(
json_data: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
async with self.rate_limiter:
try:
url = f"{self.api_url}/{endpoint}"
response = await self.client.request(
method=method,
url=url,
params=query_params,
json=json_data,
)

logger.info(
f"Rate limit info - "
f"Limit: {response.headers.get('x-ratelimit-limit', NO_OF_REQUESTS)}, "
f"Remaining: {response.headers.get('x-ratelimit-remaining', NO_OF_REQUESTS)}, "
f"Reset: {response.headers.get('x-ratelimit-reset', 1)}"
)

if response.status_code == 429:
logger.warning(
f"Rate limited on {endpoint}. "
f"Headers: {dict(response.headers)}. "
f"Waiting {response.headers.get('x-ratelimit-reset', 1)} seconds"
)
await asyncio.sleep(
float(response.headers.get("x-ratelimit-reset", 1))
)
return await self.send_api_request(
endpoint, method, query_params, json_data
)
url = f"{self.api_url}/{endpoint}"
response = await self.client.request(
method=method,
url=url,
params=query_params,
json=json_data,
)

response.raise_for_status()
return response.json()
logger.info(
f"Rate limit info - "
f"Limit: {response.headers.get('x-ratelimit-limit', NO_OF_REQUESTS)}, "
f"Remaining: {response.headers.get('x-ratelimit-remaining', NO_OF_REQUESTS)}, "
f"Reset: {response.headers.get('x-ratelimit-reset', 1)}"
)

except httpx.HTTPStatusError as e:
if e.response.status_code == 429:
reset_in = float(e.response.headers.get("x-ratelimit-reset", 1))
logger.warning(
f"Rate limited on {endpoint}. "
f"Headers: {dict(e.response.headers)}. "
f"Waiting {reset_in} seconds"
)
await asyncio.sleep(reset_in)
return await self.send_api_request(
endpoint, method, query_params, json_data
)
logger.error(
f"HTTP error on {endpoint}: {e.response.status_code} - {e.response.text}"
)
raise
response.raise_for_status()
return response.json()

async def get_paginated_resources(
self, endpoint: str, params: Optional[dict[str, Any]] = None
Expand Down

0 comments on commit 0baa218

Please sign in to comment.