Skip to content

Commit 1cbe92b

Browse files
committed
Refactor: passed parsing of http response responbility to the BaseClient ABC class
1 parent afeefb9 commit 1cbe92b

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

open_sea_v1/endpoints/client.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from abc import ABC
33
from dataclasses import dataclass
4-
from typing import Optional, Generator, Union
4+
from typing import Optional, Generator, Union, Type
55

66
from ratelimit import limits, sleep_and_retry
77
from requests import Response, request
@@ -49,16 +49,38 @@ def _set_max_rate_limit(self) -> None:
4949
if self.api_key:
5050
raise NotImplementedError("I don't know what the rate limit is for calls with an API key is yet.")
5151

52-
52+
@dataclass
5353
class BaseClient(ABC):
54+
"""
55+
Parameters
56+
----------
57+
client_params:
58+
ClientParams instance.
59+
60+
rate_limiting: bool
61+
If True, will throttle the amount of requests per second to the OpenSea API.
62+
If you pass an API key into the client_params instance, the rate limiting will change accordingly.
63+
If False, will not throttle.
64+
65+
retry: bool
66+
OpenSea will occasionally return an empty response object, although the same query would yield
67+
a full response object afterwards.
68+
If True, will pause the request for one second before trying again once.
69+
If it fails again, the empty response is returned.
70+
If set to False, the client always returns the first response object, even if it is empty.
71+
72+
"""
73+
5474
client_params: ClientParams
55-
processed_pages: int = 0
56-
response = None
57-
parsed_http_response = None
5875
url = None
5976
rate_limiting: bool = True
60-
_http_response = None
61-
_rate_limiting_timer: float = 0.0
77+
retry: bool = True
78+
79+
def __post_init__(self):
80+
self.processed_pages: int = 0
81+
self.response = None
82+
self.parsed_http_response = None
83+
self._http_response = None
6284

6385
@property
6486
def http_headers(self) -> dict:
@@ -74,6 +96,15 @@ def _get_request(self, **kwargs) -> Response:
7496
updated_kwargs = kwargs | self.http_headers
7597
return request('GET', self.url, **updated_kwargs)
7698

99+
def parse_http_response(self, response_type: Type[BaseResponse], key: str)\
100+
-> list[Union[Type[BaseResponse], BaseResponse]]:
101+
if self._http_response:
102+
the_json = self._http_response.json()
103+
the_json = the_json[key] if isinstance(the_json, dict) else the_json # the collections endpoint needs this
104+
responses = [response_type(element) for element in the_json]
105+
return responses
106+
return list()
107+
77108
def get_pages(self) -> Generator[list[list[BaseResponse]], None, None]:
78109
self.processed_pages = 0
79110
self.client_params.offset = 0 if self.client_params.offset is None else self.client_params.offset

open_sea_v1/endpoints/collections.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ def url(self):
5050

5151
@property
5252
def parsed_http_response(self) -> list[CollectionResponse]:
53-
resp_json = self._http_response.json()
54-
collections_json = resp_json if isinstance(resp_json, list) else resp_json['collections']
55-
collections = [CollectionResponse(collection_json) for collection_json in collections_json]
56-
return collections
53+
return self.parse_http_response(CollectionResponse, 'collections')
5754

5855
def _get_request(self, **kwargs):
5956
params = dict(

0 commit comments

Comments
 (0)