From adaa79a44e8e5283c1b3fbe71ab10a3a83878389 Mon Sep 17 00:00:00 2001 From: shermangriffiths Date: Sun, 14 Jul 2024 00:37:36 -0500 Subject: [PATCH 1/4] Fix APIErrorCode class (did not work as expected) --- n2y/errors.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/n2y/errors.py b/n2y/errors.py index 1c18e81..1b22d5a 100644 --- a/n2y/errors.py +++ b/n2y/errors.py @@ -1,3 +1,5 @@ +from enum import EnumMeta + try: from enum import StrEnum except ImportError: @@ -92,15 +94,28 @@ def __init__(self, response, message) -> None: super().__init__(response, message, APIErrorCode.ObjectNotFound) -class APIErrorCode(StrEnum): +class MetaEnum(EnumMeta): is_retryable: bool + values: list[str] = [] + + @property + def RetryableCodes(self): + return [ec.value for ec in self if ec.is_retryable] + + @property + def NonRetryableCodes(self): + return [ec.value for ec in self if not ec.is_retryable] + + def __contains__(cls, key): + return key in cls.values + +class APIErrorCode(StrEnum, metaclass=MetaEnum): def __new__(cls, code: str, is_retryable: bool): obj = str.__new__(cls, code) + cls.values.append(code) obj._value_ = code obj.is_retryable = is_retryable - cls.RetryableCodes = [ec.value for ec in cls if ec.is_retryable] - cls.NonretryableCodes = [ec.value for ec in cls if not ec.is_retryable] return obj BadGateway = "bad_gateway", True From db5d6aafd3a5ec589b65743f47c21f6a03e29df5 Mon Sep 17 00:00:00 2001 From: shermangriffiths Date: Mon, 15 Jul 2024 17:19:18 -0500 Subject: [PATCH 2/4] Write tests --- tests/test_errors.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/test_errors.py diff --git a/tests/test_errors.py b/tests/test_errors.py new file mode 100644 index 0000000..914bee5 --- /dev/null +++ b/tests/test_errors.py @@ -0,0 +1,36 @@ +from n2y.errors import APIErrorCode + + +def test_apierrorcode_contains(): + errors = [ + ("bad_gateway", True), + ("conflict_error", True), + ("database_connection_unavailable", True), + ("gateway_timeout", True), + ("internal_server_error", True), + ("invalid_grant", False), + ("invalid_json", False), + ("invalid_request", False), + ("invalid_request_url", False), + ("missing_version", False), + ("object_not_found", False), + ("rate_limited", True), + ("restricted_resource", False), + ("service_unavailable", True), + ("unauthorized", False), + ("validation_error", False), + ] + for error, is_retryable in errors: + assert error in APIErrorCode + if is_retryable: + assert ( + error in APIErrorCode.RetryableCodes + and error not in APIErrorCode.NonRetryableCodes + ) + else: + assert ( + error in APIErrorCode.NonRetryableCodes + and error not in APIErrorCode.RetryableCodes + ) + assert APIErrorCode(error).is_retryable == is_retryable + assert APIErrorCode(error).value == error From d3b4a19dd15065ac69a74385cb89988478ea96ed Mon Sep 17 00:00:00 2001 From: shermangriffiths Date: Mon, 15 Jul 2024 17:25:03 -0500 Subject: [PATCH 3/4] Update readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 58a6f51..4894391 100644 --- a/README.md +++ b/README.md @@ -398,6 +398,9 @@ Here are some features we're planning to add in the future: the `ExpandingLinkToPageBlock` and not the linked page itself. - Add the `UniqueIdProperty` class to represent database properties of the "unique_id" type - Add the `UniqueIdPropertyValue` class to represent page property values of the "unique_id" type +- Modify the `APIErrorCode` class so that the `in` keyword can be used on it and to have two new + attributes that store error codes where the HTTP request should be retried and where the HTTP + request should not be retried. ### v0.10.2 - Have the `ConnectionThrottled` exception inherit the `HTTPResponseError` exception and update tests From 15f786c09469298eadde800c38f9a9b32c3313df Mon Sep 17 00:00:00 2001 From: shermangriffiths Date: Thu, 18 Jul 2024 19:13:09 -0500 Subject: [PATCH 4/4] Update supported python versions in setup.py --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index 866e6d6..5a20d04 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,8 @@ "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", ], keywords="notion documentation yaml markdown", packages=find_packages(exclude=["tests"]),