Skip to content

Commit ed0d8e2

Browse files
authored
Enh/code revamp 4 (#34)
* Enhance docstring in _validate_params method * Remove a won't do todo * Update JSONResponseMeta._body_json initialization * Fix a typo in docstrings * Rename body_json to response_json * Make response_fields modules private
1 parent 67aa02c commit ed0d8e2

File tree

59 files changed

+86
-74
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+86
-74
lines changed

src/abstract_api/avatars/avatars.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ class Avatars(BaseService[AvatarsResponse]):
2020

2121
@staticmethod
2222
def _validate_params(**kwargs) -> None:
23-
"""Validates passed service parameters."""
23+
"""Validates passed service parameters.
24+
25+
Raises:
26+
ClientRequestError if a parameter has invalid/not acceptable value.
27+
"""
2428
ranged = {
2529
"image_size": (6, 512),
2630
"font_size": (0.1, 1.0),

src/abstract_api/company_enrichment/response_fields.py renamed to src/abstract_api/company_enrichment/_response_fields.py

File renamed without changes.

src/abstract_api/company_enrichment/company_enrichment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from ..core.bases import BaseService
44
from ..core.mixins import ResponseFieldsMixin
5+
from ._response_fields import RESPONSE_FIELDS
56
from .company_enrichment_response import CompanyEnrichmentResponse
6-
from .response_fields import RESPONSE_FIELDS
77

88

99
class CompanyEnrichment(

src/abstract_api/core/bases/_base_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class BaseResponseMeta(ABC):
88
"""Base response metadata for Abstract API service response."""
99

1010
def __init__(self, response: requests.models.Response) -> None:
11-
"""Initialize a new BaseResponseMeta."""
11+
"""Initializes a new BaseResponseMeta."""
1212
self._http_status: int = response.status_code
1313
self._body: bytes = response.content
1414

@@ -32,7 +32,7 @@ class BaseResponse(ABC):
3232
_meta_class: ClassVar[Type[BaseResponseMeta]]
3333

3434
def __init__(self, response: requests.models.Response) -> None:
35-
"""Initialize a new BaseResponse."""
35+
"""Initializes a new BaseResponse."""
3636
self._meta: BaseResponseMeta = self._meta_class(response)
3737

3838
@property

src/abstract_api/core/bases/file_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FileResponse(BaseResponse):
1515
meta: FileResponseMeta
1616

1717
def __init__(self, response: requests.models.Response) -> None:
18-
"""Initialize a new FileResponse."""
18+
"""Initializes a new FileResponse."""
1919
super().__init__(response)
2020
self._content: bytes = response.content
2121

src/abstract_api/core/bases/json_response.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ class JSONResponseMeta(BaseResponseMeta):
99
"""Metadata about a JSON-based API response."""
1010

1111
def __init__(self, response: requests.models.Response) -> None:
12-
"""Initialize a new JSONResponseMeta."""
12+
"""Initializes a new JSONResponseMeta."""
1313
super().__init__(response)
14-
try:
15-
self._body_json = response.json()
16-
except: # noqa: E722
17-
self._body_json = None
14+
if response.status_code == requests.codes.NO_CONTENT:
15+
self._response_json = None
16+
else:
17+
self._response_json = response.json()
1818

1919
@property
20-
def body_json(self) -> dict[str, Any] | list[dict[str, Any]]:
20+
def response_json(self) -> dict[str, Any] | list[dict[str, Any]] | None:
2121
"""JSON representation of response body returned from API request."""
22-
return self._body_json
22+
return self._response_json
2323

2424

2525
class JSONResponse(BaseResponse):
@@ -65,23 +65,23 @@ def __init__(
6565
response_fields: frozenset[str],
6666
list_response: bool = False
6767
) -> None:
68-
"""Initialize a new JSONResponse."""
68+
"""Initializes a new JSONResponse."""
6969
self._response_fields = response_fields # Must be set first.
7070

7171
super().__init__(response)
7272

73-
if self.meta.body_json is None:
73+
if self.meta.response_json is None:
7474
return
7575

7676
if TYPE_CHECKING:
77-
assert isinstance(self.meta.body_json, dict)
77+
assert isinstance(self.meta.response_json, dict)
7878

7979
not_in_response = object()
8080
for field in response_fields:
8181
value = (
82-
self.meta.body_json.get(field, not_in_response)
82+
self.meta.response_json.get(field, not_in_response)
8383
if not list_response
84-
else self.meta.body_json
84+
else self.meta.response_json
8585
)
8686
# Initialize property only if field was returned
8787
if value is not not_in_response:

src/abstract_api/core/exceptions/api_request_error.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ def details(self) -> dict[str, Any] | None:
5050
return self._details
5151

5252
@staticmethod
53-
def _get_error_details(body_json: dict[str, Any]) -> dict[str, Any]:
53+
def _get_error_details(response_json: dict[str, Any]) -> dict[str, Any]:
5454
"""Extracts error details from response.
5555
5656
Args:
57-
body_json: Returned response body as JSON.
57+
response_json: Returned response body as JSON.
5858
5959
Returns:
6060
A dict containing error details: message, code, and details.
6161
"""
6262
error_details = {}
6363

64-
if body_json and body_json.get("error"):
65-
error_details["message"] = body_json["error"].get("message")
66-
error_details["code"] = body_json["error"].get("code")
67-
error_details["details"] = body_json["error"].get("details")
64+
if response_json and response_json.get("error"):
65+
error_details["message"] = response_json["error"].get("message")
66+
error_details["code"] = response_json["error"].get("code")
67+
error_details["details"] = response_json["error"].get("details")
6868

6969
return error_details
7070

@@ -107,11 +107,11 @@ def raise_from_response(
107107
error details returned from API when available.
108108
"""
109109
try:
110-
body_json = response.json()
110+
response_json = response.json()
111111
except Exception:
112-
body_json = {}
112+
response_json = {}
113113

114-
error_details = cls._get_error_details(body_json)
114+
error_details = cls._get_error_details(response_json)
115115
raised_error_message = cls._build_raised_error_message(
116116
error_details, response.status_code
117117
)
File renamed without changes.

src/abstract_api/email_validation/email_validation_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import requests
55

66
from ..core.bases import JSONResponse
7-
from .response_fields import RESPONSE_FIELDS
7+
from ._response_fields import RESPONSE_FIELDS
88

99

1010
class EmailValidationResponse(JSONResponse):

src/abstract_api/exchange_rates/response_fields/__init__.py renamed to src/abstract_api/exchange_rates/_response_fields/__init__.py

File renamed without changes.

0 commit comments

Comments
 (0)