Skip to content

Commit aecc876

Browse files
committed
merge
2 parents a279ea6 + df45b45 commit aecc876

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

open_sea_v1/endpoints/client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ 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+
5253
@dataclass
5354
class BaseClient(ABC):
5455
"""
@@ -61,11 +62,20 @@ class BaseClient(ABC):
6162
If True, will throttle the amount of requests per second to the OpenSea API.
6263
If you pass an API key into the client_params instance, the rate limiting will change accordingly.
6364
If False, will not throttle.
65+
66+
retry: bool
67+
OpenSea will occasionally return an empty response object, although the same query would yield
68+
a full response object afterwards.
69+
If True, will pause the request for one second before trying again once.
70+
If it fails again, the empty response is returned.
71+
If set to False, the client always returns the first response object, even if it is empty.
72+
6473
"""
6574

6675
client_params: ClientParams
6776
url = None
6877
rate_limiting: bool = True
78+
retry: bool = True
6979

7080
def __post_init__(self):
7181
self.processed_pages: int = 0
@@ -103,11 +113,17 @@ def get_pages(self) -> Generator[list[list[BaseResponse]], None, None]:
103113

104114
while self.remaining_pages():
105115
self._http_response = self._get_request()
116+
self.check_if_response_is_valid()
106117
if self.parsed_http_response is not None: # edge case
107118
self.processed_pages += 1
108119
self.client_params.offset += self.client_params.page_size
109120
yield self.parsed_http_response
110121

122+
def check_if_response_is_valid(self):
123+
if not self._http_response.ok:
124+
logger.warning(f'Page {self.processed_pages} returned a {self._http_response.status_code} status code: {self._http_response.reason}\n'
125+
f'{self._http_response.text}')
126+
111127
def remaining_pages(self) -> bool:
112128
if self._http_response is None:
113129
return True

open_sea_v1/endpoints/orders.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ def url(self):
119119

120120
@property
121121
def parsed_http_response(self) -> list[OrderResponse]:
122-
if self._http_response:
123-
orders_jsons = self._http_response.json()['orders']
124-
orders = [OrderResponse(order_json) for order_json in orders_jsons]
125-
return orders
126-
return list()
122+
return self.parse_http_response(OrderResponse, 'orders')
127123

128124
def _get_request(self, **kwargs):
129125
params = dict(
@@ -160,6 +156,7 @@ def _validate_request_params(self) -> None:
160156
self._validate_contract_address_defined_with_token_id_or_tokens_ids()
161157
self._validate_token_id_defined_with_contract_address()
162158
self._validate_token_ids_defined_with_contract_address()
159+
self._validate_token_ids_quantity_is_lower_or_equal_to_30()
163160
self._validate_token_id_and_token_ids_cannot_be_defined_together()
164161
self._validate_listed_after_and_listed_before_are_datetimes()
165162
self._validate_order_by_eth_price_is_defined_with_asset_contract_address_and_token_id_or_token_ids()
@@ -180,6 +177,10 @@ def _validate_token_id_defined_with_contract_address(self) -> None:
180177
if not self.asset_contract_address:
181178
raise AttributeError('Attribute token_id must be defined together with asset_contract_address')
182179

180+
def _validate_token_ids_quantity_is_lower_or_equal_to_30(self):
181+
if self.token_ids and len(self.token_ids) > 30:
182+
raise ValueError('Attribute token_ids length must be equal or below 30.')
183+
183184
def _validate_token_ids_defined_with_contract_address(self) -> None:
184185
if self.token_ids is None:
185186
return

open_sea_v1/endpoints/tests/test_orders.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def test_attr_token_id_raises_if_not_defined_together_with_asset_contract_addres
4242
self.endpoint_kwargs |= dict(token_id=self.token_id)
4343
self.assertRaises(AttributeError, OrdersEndpoint, **self.endpoint_kwargs)
4444

45+
def test_attr_token_ids_raises_if_len_is_above_30(self):
46+
self.endpoint_kwargs |= dict(asset_contract_address=self.asset_contract_address, token_ids=list(range(1, 32)))
47+
self.assertRaises(ValueError, OrdersEndpoint, **self.endpoint_kwargs)
48+
4549
def test_attr_token_ids_raises_if_not_defined_together_with_asset_contract_address(self):
4650
self.endpoint_kwargs |= dict(token_ids=[self.token_id, self.token_id_2])
4751
self.assertRaises(AttributeError, OrdersEndpoint, **self.endpoint_kwargs)

open_sea_v1/responses/order.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class OrderResponse(BaseResponse):
1414
_json: dict
1515

1616
def __str__(self):
17-
return f"order_id={self.id}"
17+
return f"order_id={self.id} {str(self.asset)}"
1818

1919
def __post_init__(self):
2020
self._set_common_attrs()

0 commit comments

Comments
 (0)