Skip to content

Commit

Permalink
Handle secondary rate limit in device activation (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Dec 10, 2023
1 parent 1c5d4d3 commit b885481
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion aiogithubapi/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
GitHubRequestKwarg,
HttpMethod,
)
from .exceptions import GitHubException
from .exceptions import GitHubAuthenticationException, GitHubException
from .legacy.device import AIOGitHubAPIDeviceLogin as LegacyAIOGitHubAPIDeviceLogin
from .models.base import GitHubBase
from .models.device_login import GitHubLoginDeviceModel
Expand Down Expand Up @@ -151,6 +151,14 @@ async def activation(
},
)

if (
isinstance(response.data, str)
and "You have exceeded a secondary rate limit" in response.data
):
raise GitHubAuthenticationException(
"Secondary rate limit exceeded, try again in 1 hour"
)

if error := response.data.get("error"):
self.logger.debug(response.data.get("error_description"))
if error in (DeviceFlowError.AUTHORIZATION_PENDING, DeviceFlowError.SLOW_DOWN):
Expand Down
11 changes: 11 additions & 0 deletions tests/device/test_device_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=missing-docstring,protected-access
from datetime import datetime
from unittest.mock import AsyncMock
from aiogithubapi.exceptions import GitHubAuthenticationException

import pytest

Expand Down Expand Up @@ -124,3 +125,13 @@ async def test_error_while_waiting(
mock_response.mock_data = {"error": "any", "error_description": "Any error message"}
with pytest.raises(GitHubException, match="Any error message"):
await github_device_api.activation(device_code=DEVICE_CODE)


@pytest.mark.asyncio
async def test_secondary_rate_limit(
github_device_api: GitHubDeviceAPI,
mock_response: MockResponse,
):
mock_response.mock_data = "<html><p>You have exceeded a secondary rate limit.<br></html>"
with pytest.raises(GitHubAuthenticationException, match="Secondary rate limit exceeded"):
await github_device_api.activation(device_code=DEVICE_CODE)

0 comments on commit b885481

Please sign in to comment.