-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project structure changes, and added tests to endpoint paginator method.
- Loading branch information
1 parent
34c0513
commit 30525c7
Showing
20 changed files
with
161 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +0,0 @@ | ||
""" | ||
Exposes classes and objects meant to be used by You! | ||
Import other modules at your own risk, as their location may change. | ||
""" | ||
from open_sea_v1.endpoints.endpoint_client import _ClientParams as ClientParams | ||
|
||
from open_sea_v1.endpoints.endpoint_assets import _AssetsEndpoint as AssetsEndpoint | ||
from open_sea_v1.endpoints.endpoint_assets import _AssetsOrderBy as AssetsOrderBy | ||
|
||
from open_sea_v1.endpoints.endpoint_events import _EventsEndpoint as EventsEndpoint | ||
from open_sea_v1.endpoints.endpoint_events import AuctionType | ||
from open_sea_v1.endpoints.endpoint_events import EventType | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from itertools import chain | ||
from unittest import TestCase | ||
|
||
from open_sea_v1.endpoints.client import ClientParams | ||
from open_sea_v1.endpoints.events import EventsEndpoint, EventType | ||
|
||
|
||
class TestBaseEndpointClient(TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls) -> None: | ||
cls.max_pages = 2 | ||
cls.limit = 5 | ||
cls.sample_client = EventsEndpoint( | ||
client_params=ClientParams(max_pages=cls.max_pages, limit=cls.limit), | ||
asset_contract_address="0x76be3b62873462d2142405439777e971754e8e77", | ||
token_id=str(10152), | ||
event_type=EventType.SUCCESSFUL, | ||
) | ||
cls.sample_pages = list(cls.sample_client.get_pages()) | ||
|
||
def test_remaining_pages_true_if_http_response_is_none(self): | ||
self.sample_client._http_response = None | ||
self.assertTrue(self.sample_client.remaining_pages()) | ||
|
||
def test_get_pages_resets_processed_pages_and_offset_attr_on_new_calls(self): | ||
for _ in range(2): | ||
next(self.sample_client.get_pages()) | ||
self.assertEqual(self.sample_client.processed_pages, 1) | ||
expected_offset_value = self.sample_client.client_params.limit | ||
self.assertEqual(self.sample_client.client_params.offset, expected_offset_value) | ||
|
||
def test_get_pages_does_not_append_empty_pages(self): | ||
no_empty_pages = all(not page == list() for page in self.sample_pages) | ||
self.assertTrue(no_empty_pages) | ||
|
||
def test_get_pages_max_pages_and_limit_params_works(self): | ||
self.assertLessEqual(len(self.sample_pages), self.max_pages + 1) | ||
for page in self.sample_pages[:-1]: | ||
self.assertEqual(self.limit, len(page)) | ||
|
||
def test_pagination_works(self): | ||
id_list_1 = [[e.id for e in page] for page in self.sample_client.get_pages()] | ||
id_list_1 = list(chain.from_iterable(id_list_1)) | ||
id_list_1.sort(reverse=True) | ||
|
||
self.sample_client.client_params = ClientParams(limit=4, offset=0, max_pages=2) | ||
id_list_2 = [[e.id for e in page] for page in self.sample_client.get_pages()] | ||
id_list_2 = list(chain.from_iterable(id_list_2)) | ||
id_list_2.sort(reverse=True) | ||
|
||
self.assertEqual(len(id_list_2), 12) # updated limit * max_pages+1 | ||
self.assertGreater(len(id_list_1), len(id_list_2)) | ||
self.assertTrue(id_list_1[i] == id_list_2[i] for i in range(len(id_list_2))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +0,0 @@ | ||
""" | ||
Exposes classes and objects meant to be used by You! | ||
Import other modules at your own risk, as their location may change. | ||
""" | ||
from open_sea_v1.helpers.ether_converter import EtherConverter, EtherUnit | ||
from open_sea_v1.helpers.extended_classes import ExtendedStrEnum | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import Type, Optional, Any, Union | ||
|
||
from open_sea_v1.responses import OpenSeaResponse | ||
|
||
|
||
@dataclass | ||
class ResponseParser: | ||
""" | ||
Interface for saving and loading OpenseaAPI responses from and to JSON files. | ||
""" | ||
destination: Path | ||
response_type: Type[OpenSeaResponse] | ||
|
||
def __post_init__(self): | ||
if not self.destination.exists(): | ||
self.destination.parent.mkdir(parents=True, exist_ok=True) | ||
|
||
def dump(self, to_parse: Optional[Union[OpenSeaResponse, list[OpenSeaResponse]]]) -> None: | ||
if isinstance(to_parse, list): | ||
the_jsons = [e._json for e in to_parse] | ||
else: | ||
the_jsons = to_parse._json | ||
with open(str(self.destination), 'w') as f: | ||
json.dump(the_jsons, f) | ||
|
||
def load(self) -> Any: | ||
with open(str(self.destination), 'r') as f: | ||
parsed_json = json.load(f) | ||
return [self.response_type(collection) for collection in parsed_json] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +0,0 @@ | ||
""" | ||
Exposes classes and objects meant to be used by You! | ||
Import other modules at your own risk, as their location may change. | ||
""" | ||
from open_sea_v1.responses.response_asset import _AssetResponse as AssetResponse | ||
from open_sea_v1.responses.response_asset import _CollectionResponse as CollectionResponse | ||
|
||
from open_sea_v1.responses.response_event import _EventReponse as EventResponse | ||
|
||
from open_sea_v1.responses.response_abc import _OpenSeaResponse as OpenSeaAPIResponse | ||
from open_sea_v1.responses.response_parser import _ResponseParser as ResponseParser | ||
2 changes: 1 addition & 1 deletion
2
open_sea_v1/responses/response_abc.py → open_sea_v1/responses/abc.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.