Skip to content

Commit 5745365

Browse files
committed
Implemented CollectionsEndpoint.
Added extra doc strings to events and assets endpoints.
1 parent 67cff5b commit 5745365

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

open_sea_v1/endpoints/assets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AssetsOrderBy(str, Enum):
2222
@dataclass
2323
class AssetsEndpoint(BaseClient, BaseEndpoint):
2424
"""
25-
Opensea API Assets Endpoint
25+
Opensea API Assets Endpoint.
2626
2727
Parameters
2828
----------
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from dataclasses import dataclass
2+
from typing import Optional
3+
4+
from open_sea_v1.endpoints.abc import BaseEndpoint
5+
from open_sea_v1.endpoints.client import BaseClient, ClientParams
6+
from open_sea_v1.endpoints.urls import EndpointURLS
7+
from open_sea_v1.responses.collection import CollectionResponse
8+
9+
10+
@dataclass
11+
class CollectionsEndpoint(BaseClient, BaseEndpoint):
12+
"""
13+
From OpenSea documentation:
14+
----------
15+
Use this endpoint to fetch collections and dapps that OpenSea shows on opensea.io,
16+
along with dapps and smart contracts that a particular user cares about.
17+
18+
Maintainer observations:
19+
----------
20+
You cannot specify the collection name for the data you wish to retrieve.
21+
The only way to retrieve such data, is by getting ALL collections from OpenSea, then
22+
iterating over them.
23+
24+
25+
Parameters
26+
----------
27+
client_params:
28+
ClientParams instance.
29+
30+
asset_owner: Optional[str]
31+
A wallet address. If specified, will return collections where the owner
32+
owns at least one asset belonging to smart contracts in the collection.
33+
The number of assets the account owns is shown as owned_asset_count for
34+
each collection.
35+
36+
:return: Parsed JSON
37+
"""
38+
39+
client_params: ClientParams = None
40+
asset_owner: Optional[str] = None
41+
42+
def __post_init__(self):
43+
self._validate_request_params()
44+
45+
@property
46+
def url(self):
47+
return EndpointURLS.COLLECTIONS.value
48+
49+
@property
50+
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
55+
56+
def _get_request(self, **kwargs):
57+
params = dict(
58+
asset_owner=self.asset_owner,
59+
offset=self.client_params.offset,
60+
limit=self.client_params.limit,
61+
)
62+
get_request_kwargs = dict(params=params)
63+
self._http_response = super()._get_request(**get_request_kwargs)
64+
return self._http_response
65+
66+
def _validate_request_params(self) -> None:
67+
if self.asset_owner is not None and not isinstance(self.asset_owner, str):
68+
raise TypeError(f'{self.asset_owner=} must be a str instance representing a wallet address.')

open_sea_v1/endpoints/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class AuctionType(ExtendedStrEnum):
3636
@dataclass
3737
class EventsEndpoint(BaseClient, BaseEndpoint):
3838
"""
39-
Opensea API Events Endpoint
39+
The events endpoint provides a list of events that occur on the assets that OpenSea tracks.
4040
4141
Parameters
4242
----------
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from unittest import TestCase
2+
3+
from open_sea_v1.endpoints.client import ClientParams
4+
from open_sea_v1.endpoints.collections import CollectionsEndpoint
5+
from open_sea_v1.responses.collection import CollectionResponse
6+
7+
8+
class TestCollectionsEndpoint(TestCase):
9+
10+
def setUp(self) -> None:
11+
self.asset_owner = "0x5ca12f79e4d33b0bd153b40df59f6db9ee03482e" # punk
12+
self.endpoint_kwargs = dict(
13+
client_params=ClientParams(limit=2, max_pages=1),
14+
)
15+
16+
@staticmethod
17+
def create_and_get(**kwargs) -> list[CollectionResponse]:
18+
collections_client = CollectionsEndpoint(**kwargs)
19+
collections_client._get_request()
20+
return collections_client.parsed_http_response
21+
22+
def test_collection_resp_works(self):
23+
basic_collections_resp = self.create_and_get(**self.endpoint_kwargs)
24+
self.assertTrue(basic_collections_resp)
25+
26+
self.endpoint_kwargs |= dict(asset_owner=self.asset_owner)
27+
owner_asset_collections_resp = self.create_and_get(**self.endpoint_kwargs)
28+
self.assertTrue(owner_asset_collections_resp)
29+
30+
self.assertNotEqual(
31+
{c.slug for c in basic_collections_resp},
32+
{c.slug for c in owner_asset_collections_resp},
33+
)

0 commit comments

Comments
 (0)