From 5c53a5b2cf56428e007ef0a1130044b0f409897c Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Sun, 18 Oct 2026 08:53:39 +0100 Subject: [PATCH] fix: time out requests after 10 seconds --- CHANGELOG.md | 22 ---------------------- README.md | 3 ++- ecologi/ecologi.py | 11 ++++++++--- tests/test_ecologi.py | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 26 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 6ede7d8..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,22 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). - -## Unreleased - -## v0.1.2 - 2022-11-18 - -### Added -- Enforce types with `py.typed` file ([#2](https://github.com/owenvoke/ecologi-python-sdk/pull/2)) - -## v0.1.1 - 2022-11-16 - -### Added -- Add Hatch / PyProject configuration ([#1](https://github.com/owenvoke/ecologi-python-sdk/pull/1)) - -## v0.1.0 - 2022-11-09 - -### Added -- Initial release diff --git a/README.md b/README.md index 3335130..5634fb3 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ sites = ecologi.impact('owenvoke') ## Change log -Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. +Please see [GitHub Releases][link-github-releases] for more information on what has changed recently. ## Testing @@ -70,6 +70,7 @@ Read more about Treeware at [treeware.earth][link-treeware]. [link-pypi]: https://pypi.org/project/ecologi-sdk [link-github-actions]: https://github.com/owenvoke/ecologi-python-sdk/actions +[link-github-releases]: https://github.com/owenvoke/ecologi-python-sdk/releases [link-treeware]: https://treeware.earth [link-treeware-gifting]: https://ecologi.com/owenvoke?gift-trees [link-author]: https://github.com/owenvoke diff --git a/ecologi/ecologi.py b/ecologi/ecologi.py index de1accd..d96faa2 100644 --- a/ecologi/ecologi.py +++ b/ecologi/ecologi.py @@ -4,7 +4,8 @@ from .types import Impact -TIMEOUT = 3 +# Seconds to wait for a connection, and between bytes of the response +TIMEOUT = 10 API_BASE_URI = "https://public.ecologi.com" @@ -17,8 +18,9 @@ class NotFoundException(EcologiException): class Ecologi: - def __init__(self, base_uri: str = API_BASE_URI) -> None: + def __init__(self, base_uri: str = API_BASE_URI, timeout: float = TIMEOUT) -> None: self.base_uri: str = base_uri + self.timeout: float = timeout self.headers: dict = {} def user_exists(self, username: str) -> bool: @@ -39,7 +41,10 @@ def __request(self, method: str, url: str, params: Optional[dict] = None): if method == "GET": response = requests.get( - self.base_uri + url, params=params, headers=self.headers + self.base_uri + url, + params=params, + headers=self.headers, + timeout=self.timeout, ) else: raise RuntimeError("Invalid request method provided") diff --git a/tests/test_ecologi.py b/tests/test_ecologi.py index 3a788ad..ab81577 100644 --- a/tests/test_ecologi.py +++ b/tests/test_ecologi.py @@ -43,3 +43,17 @@ def test_not_authenticated(): assert type(me) == bool assert me is False + + +def test_requests_time_out(): + with requests_mock.Mocker() as m: + m.get( + url="https://public.ecologi.com/users/owenvoke/impact", + json={"trees": 1733, "carbonOffset": 41.05}, + ) + + ecologi.impact("owenvoke") + assert m.last_request.timeout == 10 + + Ecologi(timeout=2.5).impact("owenvoke") + assert m.last_request.timeout == 2.5