Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ classifiers = [
requires-python = ">=3.10"
dependencies = [
"aiohttp[speedups]~=3.8",
"aiohttp-retry~=2.9",
"gql[aiohttp,requests]>=4,<5",
"pyjwt[crypto]~=2.8",
"requests~=2.31",
Expand Down
23 changes: 20 additions & 3 deletions src/simple_github/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from typing import TYPE_CHECKING, Any

from aiohttp import ClientResponse, ClientSession
from aiohttp_retry import ExponentialRetry, RetryClient
from gql import Client as GqlClient
from gql import gql
from gql.client import ReconnectingAsyncClientSession, SyncClientSession
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.requests import RequestsHTTPTransport
from requests import Response as RequestsResponse
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

if TYPE_CHECKING:
from simple_github.auth import Auth
Expand Down Expand Up @@ -131,6 +134,13 @@ def _get_requests_session(self) -> Session:
assert session.transport.session
return session.transport.session

def _get_retry_session(self) -> Session:
session = self._get_requests_session()
retry = Retry(total=5, backoff_factor=1, status_forcelist={500, 502, 503, 504})
adapter = HTTPAdapter(max_retries=retry)
session.mount("https://", adapter)
return session

def request(self, method: str, query: str, **kwargs) -> RequestsResponse:
"""Make a request to Github's REST API.

Expand All @@ -144,7 +154,7 @@ def request(self, method: str, query: str, **kwargs) -> RequestsResponse:
Dict: The JSON result of the request.
"""
url = f"{GITHUB_API_ENDPOINT}/{query.lstrip('/')}"
session = self._get_requests_session()
session = self._get_retry_session()

with session.request(method, url, **kwargs) as resp:
return resp
Expand Down Expand Up @@ -281,6 +291,13 @@ async def _get_aiohttp_session(self) -> ClientSession:
assert session.transport.session
return session.transport.session

async def _get_retry_client(self) -> RetryClient:
session = await self._get_aiohttp_session()
return RetryClient(
client_session=session,
retry_options=ExponentialRetry(attempts=5),
)

async def request(self, method: str, query: str, **kwargs: Any) -> ClientResponse:
"""Make a request to Github's REST API.

Expand All @@ -294,8 +311,8 @@ async def request(self, method: str, query: str, **kwargs: Any) -> ClientRespons
Dict: The JSON result of the request.
"""
url = f"{GITHUB_API_ENDPOINT}/{query.lstrip('/')}"
session = await self._get_aiohttp_session()
return await session.request(method, url, **kwargs)
client = await self._get_retry_client()
return await client.request(method, url, **kwargs)

async def get(self, query: str, **kwargs: Any) -> ClientResponse:
"""Make a GET request to Github's REST API.
Expand Down
29 changes: 29 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

import pytest
import pytest_asyncio
from aiohttp import ClientResponseError
Expand Down Expand Up @@ -142,6 +144,8 @@ async def test_async_client_rest(aioresponses, async_client):
},
data="null",
allow_redirects=True,
# internal aiohttp-retry tracking data
trace_request_ctx=mock.ANY,
)

aioresponses.get(url, status=401)
Expand All @@ -150,6 +154,19 @@ async def test_async_client_rest(aioresponses, async_client):
resp.raise_for_status()


@pytest.mark.asyncio
async def test_async_client_retries_on_5xx(aioresponses, async_client):
client = async_client
url = f"{GITHUB_API_ENDPOINT}/octocat"

aioresponses.get(url, status=502)
aioresponses.get(url, status=200, payload={"answer": 42})

resp = await client.get("/octocat")
result = await resp.json()
assert result == {"answer": 42}


def test_sync_client_rest(responses, sync_client):
client = sync_client
url = f"{GITHUB_API_ENDPOINT}/octocat"
Expand Down Expand Up @@ -187,6 +204,18 @@ def test_sync_client_rest(responses, sync_client):
resp.raise_for_status()


def test_sync_client_retries_on_5xx(responses, sync_client):
client = sync_client
url = f"{GITHUB_API_ENDPOINT}/octocat"

responses.get(url, status=502)
responses.get(url, status=200, json={"answer": 42})

resp = client.get("/octocat")
result = resp.json()
assert result == {"answer": 42}


@pytest.mark.asyncio
async def test_async_client_rest_with_text(aioresponses, async_client):
client = async_client
Expand Down
14 changes: 14 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.