Skip to content

Commit 0eebf40

Browse files
committed
README.md: Added some basic information about the package.
Added test runner, all tests pass. Minor fix: poorly coded paginator was tested and fixed. Minor fix: changed id attributes from responses from int to str.
1 parent a279ea6 commit 0eebf40

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

open_sea_v1/endpoints/client.py

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

6-
from ratelimit import limits, sleep_and_retry
76
from requests import Response, request
87

98
from open_sea_v1.responses.abc import BaseResponse
109

1110
logger = logging.getLogger(__name__)
1211

13-
MAX_CALLS_PER_SECOND = 2 # gets overriden if API key is passed to ClientParams instance
14-
RATE_LIMIT = 1 # second
15-
1612
@dataclass
1713
class ClientParams:
1814
"""Common OpenSea Endpoint parameters to pass in."""
@@ -25,7 +21,6 @@ class ClientParams:
2521
def __post_init__(self):
2622
self._validate_attrs()
2723
self._decrement_max_pages_attr()
28-
self._set_max_rate_limit()
2924

3025
def _validate_attrs(self) -> None:
3126
if self.limit is not None and not 0 < self.limit <= 300:
@@ -43,35 +38,14 @@ def _decrement_max_pages_attr(self) -> None:
4338
if self.max_pages is not None:
4439
self.max_pages -= 1
4540

46-
def _set_max_rate_limit(self) -> None:
47-
global MAX_CALLS_PER_SECOND
48-
MAX_CALLS_PER_SECOND = 2 # per second
49-
if self.api_key:
50-
raise NotImplementedError("I don't know what the rate limit is for calls with an API key is yet.")
5141

52-
@dataclass
5342
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-
6643
client_params: ClientParams
44+
processed_pages: int = 0
45+
response = None
46+
parsed_http_response = None
6747
url = None
68-
rate_limiting: bool = True
69-
70-
def __post_init__(self):
71-
self.processed_pages: int = 0
72-
self.response = None
73-
self.parsed_http_response = None
74-
self._http_response = None
48+
_http_response = None
7549

7650
@property
7751
def http_headers(self) -> dict:
@@ -80,22 +54,10 @@ def http_headers(self) -> dict:
8054
params['headers'] = {'X-API-Key': self.client_params.api_key}
8155
return params
8256

83-
@sleep_and_retry
84-
@limits(calls=MAX_CALLS_PER_SECOND, period=RATE_LIMIT)
8557
def _get_request(self, **kwargs) -> Response:
86-
"""Get requests with a rate limiter."""
8758
updated_kwargs = kwargs | self.http_headers
8859
return request('GET', self.url, **updated_kwargs)
8960

90-
def parse_http_response(self, response_type: Type[BaseResponse], key: str)\
91-
-> list[Union[Type[BaseResponse], BaseResponse]]:
92-
if self._http_response:
93-
the_json = self._http_response.json()
94-
the_json = the_json[key] if isinstance(the_json, dict) else the_json # the collections endpoint needs this
95-
responses = [response_type(element) for element in the_json]
96-
return responses
97-
return list()
98-
9961
def get_pages(self) -> Generator[list[list[BaseResponse]], None, None]:
10062
self.processed_pages = 0
10163
self.client_params.offset = 0 if self.client_params.offset is None else self.client_params.offset

open_sea_v1/endpoints/tests/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
from open_sea_v1.endpoints.client import ClientParams
55
from open_sea_v1.endpoints.events import EventsEndpoint, EventType
66

7+
class TestClientParams(TestCase):
8+
9+
def test_max_pages_attr_is_automatically_decremented_by_1(self):
10+
params = ClientParams(max_pages=1)
11+
self.assertEqual(params.max_pages, 0)
12+
13+
def test_max_pages_attr_raises_value_error_if_below_or_equal_to_zero(self):
14+
self.assertRaises(ValueError, ClientParams, max_pages=-1)
15+
16+
def test_limit_attr_raises_value_error_if_not_between_0_and_300(self):
17+
self.assertRaises(ValueError, ClientParams, limit=-1)
18+
self.assertRaises(ValueError, ClientParams, limit=301)
19+
20+
def test_page_size_attr_raises_value_error_if_not_between_0_and_50(self):
21+
self.assertRaises(ValueError, ClientParams, page_size=-1)
22+
self.assertRaises(ValueError, ClientParams, page_size=51)
23+
724

825
class TestClientParams(TestCase):
926

0 commit comments

Comments
 (0)