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.' )
0 commit comments