diff --git a/.github/workflows/code_coverage.yaml b/.github/workflows/code_coverage.yaml index df63779..1c44a42 100644 --- a/.github/workflows/code_coverage.yaml +++ b/.github/workflows/code_coverage.yaml @@ -17,7 +17,9 @@ jobs: steps: - uses: actions/checkout@v4 with: - persist-credentials: false + persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token + fetch-depth: 0 # otherwise, you will fail to push refs to dest repo + - uses: actions/setup-python@v5 with: python-version: "3.11" @@ -60,6 +62,6 @@ jobs: - name: Commit & Push changes to Readme uses: actions-js/push@master with: - branch: master + branch: ${{ github.head_ref || github.ref_name }} message: Update coverage on Readme github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index d6df4ac..944f5b6 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Retrieve User Information, Trophies, Game and Store data from the PlayStation Ne [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/license/MIT) -Coverage
Coverage Report
FileStmtsMissCoverMissing
src/psnawp_api
   __init__.py20100% 
   psnawp.py4022 95%
src/psnawp_api/core
   __init__.py40100% 
   authenticator.py1291919 85%
   psnawp_exceptions.py110100% 
   request_builder.py6599 86%
src/psnawp_api/models
   __init__.py70100% 
   client.py7322 97%
   game_title.py2622 92%
   group.py5388 85%
   search.py1504242 72%
   title_stats.py7533 96%
   user.py7922 97%
src/psnawp_api/models/listing
   __init__.py20100% 
   pagination_iterator.py5644 93%
src/psnawp_api/models/trophies
   __init__.py60100% 
   trophy.py13099 93%
   trophy_constants.py300100% 
   trophy_group.py9233 97%
   trophy_summary.py2711 96%
   trophy_titles.py8822 98%
   utility_functions.py70100% 
src/psnawp_api/utils
   __init__.py30100% 
   endpoints.py20100% 
   misc.py50100% 
TOTAL116210891% 
+Coverage
Coverage Report
FileStmtsMissCoverMissing
src/psnawp_api
   __init__.py20100% 
   psnawp.py4022 95%
src/psnawp_api/core
   __init__.py40100% 
   authenticator.py1291919 85%
   psnawp_exceptions.py110100% 
   request_builder.py7099 87%
src/psnawp_api/models
   __init__.py70100% 
   client.py7322 97%
   game_title.py2622 92%
   group.py5388 85%
   search.py1504242 72%
   title_stats.py7533 96%
   user.py7922 97%
src/psnawp_api/models/listing
   __init__.py20100% 
   pagination_iterator.py5644 93%
src/psnawp_api/models/trophies
   __init__.py60100% 
   trophy.py13099 93%
   trophy_constants.py300100% 
   trophy_group.py9233 97%
   trophy_summary.py2711 96%
   trophy_titles.py8822 98%
   utility_functions.py70100% 
src/psnawp_api/utils
   __init__.py30100% 
   endpoints.py20100% 
   misc.py50100% 
TOTAL116710891% 
## How to install diff --git a/pyproject.toml b/pyproject.toml index c41703e..b09336a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ python = "^3.9" attrs = "23.1.0" requests = "^2.31.0" typing-extensions = "^4.11.0" +requests-ratelimiter = "^0.7.0" [tool.poetry.group.typing.dependencies] mypy = "^1.4.0" diff --git a/pytest-coverage.txt b/pytest-coverage.txt index 22edd11..eae1fcf 100644 --- a/pytest-coverage.txt +++ b/pytest-coverage.txt @@ -30,7 +30,7 @@ src/psnawp_api/__init__.py 2 0 100% src/psnawp_api/core/__init__.py 4 0 100% src/psnawp_api/core/authenticator.py 129 19 85% src/psnawp_api/core/psnawp_exceptions.py 11 0 100% -src/psnawp_api/core/request_builder.py 65 9 86% +src/psnawp_api/core/request_builder.py 70 9 87% src/psnawp_api/models/__init__.py 7 0 100% src/psnawp_api/models/client.py 73 2 97% src/psnawp_api/models/game_title.py 26 2 92% @@ -52,6 +52,6 @@ src/psnawp_api/utils/__init__.py 3 0 100% src/psnawp_api/utils/endpoints.py 2 0 100% src/psnawp_api/utils/misc.py 5 0 100% -------------------------------------------------------------------------- -TOTAL 1162 108 91% +TOTAL 1167 108 91% -============================== 72 passed in 3.10s ============================== +============================== 72 passed in 3.70s ============================== diff --git a/pytest.xml b/pytest.xml index a110ed0..a657598 100644 --- a/pytest.xml +++ b/pytest.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/psnawp_api/core/request_builder.py b/src/psnawp_api/core/request_builder.py index 0b2da71..c1d12db 100644 --- a/src/psnawp_api/core/request_builder.py +++ b/src/psnawp_api/core/request_builder.py @@ -2,7 +2,9 @@ from typing import TYPE_CHECKING, Any, TypedDict, cast -from requests import Response, request +from pyrate_limiter import Duration, Limiter, RequestRate, SQLiteBucket +from requests import Response +from requests_ratelimiter import LimiterSession from typing_extensions import NotRequired, TypeAlias, Unpack from psnawp_api.core.psnawp_exceptions import ( @@ -98,6 +100,11 @@ def __init__(self, common_headers: RequestBuilderHeaders) -> None: """Initialize Request Handler with default headers.""" self.common_headers = cast(dict[str, str], common_headers) + psn_api_rate = RequestRate(limit=300, interval=Duration.MINUTE * 15) + limiter = Limiter(psn_api_rate, bucket_class=SQLiteBucket) + self.session = LimiterSession(limiter=limiter, per_host=False, limit_statuses=[], burst=0) + self.session.headers.update(self.common_headers) + def request(self, method: str | bytes, **kwargs: Unpack[RequestOptions]) -> Response: """Handles HTTP requests and returns the requests.Response object. @@ -117,8 +124,7 @@ def request(self, method: str | bytes, **kwargs: Unpack[RequestOptions]) -> Resp :raises PSNAWPServerError: If the HTTP response status code is 500 or above. """ - kwargs["headers"] = self.common_headers | kwargs.get("headers", {}) - response = request(method=method, **kwargs) + response = self.session.request(method=method, **kwargs) response_checker(response) return response