Skip to content

Commit 14969f4

Browse files
committed
Refactor: passed parsing of http response responbility to the BaseClient ABC class
1 parent aeacc51 commit 14969f4

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

open_sea_v1/endpoints/assets.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ def url(self):
7474

7575
@property
7676
def parsed_http_response(self) -> list[AssetResponse]:
77-
assets_json = self._http_response.json()['assets']
78-
assets = [AssetResponse(asset_json) for asset_json in assets_json]
79-
return assets
77+
return self.parse_http_response(AssetResponse, 'assets')
8078

8179
def _get_request(self, **kwargs):
8280
params = dict(

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
@@ -48,10 +48,7 @@ def url(self):
4848

4949
@property
5050
def parsed_http_response(self) -> list[CollectionResponse]:
51-
resp_json = self._http_response.json()
52-
collections_json = resp_json if isinstance(resp_json, list) else resp_json['collections']
53-
collections = [CollectionResponse(collection_json) for collection_json in collections_json]
54-
return collections
51+
return self.parse_http_response(CollectionResponse, 'collections')
5552

5653
def _get_request(self, **kwargs):
5754
params = dict(

open_sea_v1/endpoints/events.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ def _get_request(self, **kwargs) -> Response:
104104

105105
@property
106106
def parsed_http_response(self) -> list[EventResponse]:
107-
events_json = self._http_response.json()['asset_events']
108-
events = [EventResponse(event) for event in events_json]
109-
return events
107+
return self.parse_http_response(EventResponse, 'asset_events')
110108

111109
def _validate_request_params(self) -> None:
112110
self._validate_param_auction_type()

0 commit comments

Comments
 (0)