Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize LagoApiError #59

Merged
merged 8 commits into from
Jan 12, 2023
25 changes: 15 additions & 10 deletions lago_python_client/clients/base_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import MutableMapping
import json
from http import HTTPStatus
from typing import Any
import sys
from typing import Any, Optional
from urllib.parse import urljoin, urlencode

import orjson
Expand All @@ -11,6 +11,11 @@

from lago_python_client.version import LAGO_VERSION

if sys.version_info < (3, 9):
from typing import MutableMapping
else:
from collections.abc import MutableMapping


class BaseClient:
RESPONSE_SUCCESS_CODES = [200, 201, 202, 204]
Expand All @@ -19,7 +24,7 @@ def __init__(self, base_url: str, api_key: str):
self.base_url = base_url
self.api_key = api_key

def find(self, resource_id: str, params: dict | None = None):
def find(self, resource_id: str, params: Optional[dict] = None):
api_resource = self.api_resource() + '/' + resource_id
query_url = urljoin(self.base_url, api_resource)

Expand All @@ -32,7 +37,7 @@ def find(self, resource_id: str, params: dict | None = None):

return self.prepare_response(data)

def find_all(self, options: dict | None = None):
def find_all(self, options: Optional[dict] = None):
if options:
api_resource = self.api_resource() + '?' + urlencode(options)
else:
Expand Down Expand Up @@ -68,7 +73,7 @@ def create(self, input_object: BaseModel):
else:
return self.prepare_response(data.json().get(self.root_name()))

def update(self, input_object: BaseModel, identifier: str | None = None):
def update(self, input_object: BaseModel, identifier: Optional[str] = None):
api_resource = self.api_resource()

if identifier is not None:
Expand All @@ -95,7 +100,7 @@ def headers(self):

return headers

def handle_response(self, response: Response) -> Response | None:
def handle_response(self, response: Response) -> Optional[Response]:
if response.status_code in BaseClient.RESPONSE_SUCCESS_CODES:
if response.text:
return response
Expand All @@ -104,7 +109,7 @@ def handle_response(self, response: Response) -> Response | None:
else:
if response.text:
response_data: Any = orjson.loads(response.text)
detail: str | None = getattr(response_data, 'error', None)
detail: Optional[str] = getattr(response_data, 'error', None)
else:
response_data = None
detail = None
Expand Down Expand Up @@ -134,10 +139,10 @@ class LagoApiError(Exception):
def __init__(
self,
status_code: int,
url: str | None,
url: Optional[str],
response: Any,
detail: str | None = None,
headers: MutableMapping[str, str] | None = None,
detail: Optional[str] = None,
headers: Optional[MutableMapping[str, str]] = None,
) -> None:
if detail is None:
detail = HTTPStatus(status_code).phrase
Expand Down