-
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.
Added missing attribute in CollectionResponse. Added extra doc strings to events and assets endpoints.
- Loading branch information
1 parent
a0877d0
commit ee2e172
Showing
5 changed files
with
104 additions
and
2 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
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,68 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from open_sea_v1.endpoints.abc import BaseEndpoint | ||
from open_sea_v1.endpoints.client import BaseClient, ClientParams | ||
from open_sea_v1.endpoints.urls import EndpointURLS | ||
from open_sea_v1.responses.collection import CollectionResponse | ||
|
||
|
||
@dataclass | ||
class CollectionsEndpoint(BaseClient, BaseEndpoint): | ||
""" | ||
From OpenSea documentation: | ||
---------- | ||
Use this endpoint to fetch collections and dapps that OpenSea shows on opensea.io, | ||
along with dapps and smart contracts that a particular user cares about. | ||
Maintainer observations: | ||
---------- | ||
You cannot specify the collection name for the data you wish to retrieve. | ||
The only way to retrieve such data, is by getting ALL collections from OpenSea, then | ||
iterating over them. | ||
Parameters | ||
---------- | ||
client_params: | ||
ClientParams instance. | ||
asset_owner: Optional[str] | ||
A wallet address. If specified, will return collections where the owner | ||
owns at least one asset belonging to smart contracts in the collection. | ||
The number of assets the account owns is shown as owned_asset_count for | ||
each collection. | ||
:return: Parsed JSON | ||
""" | ||
|
||
client_params: ClientParams = None | ||
asset_owner: Optional[str] = None | ||
|
||
def __post_init__(self): | ||
self._validate_request_params() | ||
|
||
@property | ||
def url(self): | ||
return EndpointURLS.COLLECTIONS.value | ||
|
||
@property | ||
def parsed_http_response(self) -> list[CollectionResponse]: | ||
resp_json = self._http_response.json() | ||
collections_json = resp_json if isinstance(resp_json, list) else resp_json['collections'] | ||
collections = [CollectionResponse(collection_json) for collection_json in collections_json] | ||
return collections | ||
|
||
def _get_request(self, **kwargs): | ||
params = dict( | ||
asset_owner=self.asset_owner, | ||
offset=self.client_params.offset, | ||
limit=self.client_params.limit, | ||
) | ||
get_request_kwargs = dict(params=params) | ||
self._http_response = super()._get_request(**get_request_kwargs) | ||
return self._http_response | ||
|
||
def _validate_request_params(self) -> None: | ||
if self.asset_owner is not None and not isinstance(self.asset_owner, str): | ||
raise TypeError(f'{self.asset_owner=} must be a str instance representing a wallet address.') |
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,33 @@ | ||
from unittest import TestCase | ||
|
||
from open_sea_v1.endpoints.client import ClientParams | ||
from open_sea_v1.endpoints.collections import CollectionsEndpoint | ||
from open_sea_v1.responses.collection import CollectionResponse | ||
|
||
|
||
class TestCollectionsEndpoint(TestCase): | ||
|
||
def setUp(self) -> None: | ||
self.asset_owner = "0x5ca12f79e4d33b0bd153b40df59f6db9ee03482e" # punk | ||
self.endpoint_kwargs = dict( | ||
client_params=ClientParams(limit=2, max_pages=1), | ||
) | ||
|
||
@staticmethod | ||
def create_and_get(**kwargs) -> list[CollectionResponse]: | ||
collections_client = CollectionsEndpoint(**kwargs) | ||
collections_client._get_request() | ||
return collections_client.parsed_http_response | ||
|
||
def test_collection_resp_works(self): | ||
basic_collections_resp = self.create_and_get(**self.endpoint_kwargs) | ||
self.assertTrue(basic_collections_resp) | ||
|
||
self.endpoint_kwargs |= dict(asset_owner=self.asset_owner) | ||
owner_asset_collections_resp = self.create_and_get(**self.endpoint_kwargs) | ||
self.assertTrue(owner_asset_collections_resp) | ||
|
||
self.assertNotEqual( | ||
{c.slug for c in basic_collections_resp}, | ||
{c.slug for c in owner_asset_collections_resp}, | ||
) |
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