|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from requests import Response |
| 5 | + |
| 6 | +from open_sea_v1.endpoints.endpoint_enums import AssetsOrderBy, OpenseaApiEndpoints |
| 7 | +from open_sea_v1.endpoints.endpoint_base_client import OpenSeaClient |
| 8 | +from open_sea_v1.endpoints.endpoint_abc import BaseOpenSeaEndpoint |
| 9 | +from open_sea_v1.responses.asset_obj import Asset |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class AssetsEndpoint(OpenSeaClient, BaseOpenSeaEndpoint): |
| 14 | + """ |
| 15 | + Opensea API Assets Endpoint |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + width: |
| 20 | + width of the snake |
| 21 | +
|
| 22 | + owner: |
| 23 | + The address of the owner of the assets |
| 24 | +
|
| 25 | + token_ids: |
| 26 | + List of token IDs to search for |
| 27 | +
|
| 28 | + asset_contract_address: |
| 29 | + The NFT contract address for the assets |
| 30 | +
|
| 31 | + asset_contract_addresses: |
| 32 | + List of contract addresses to search for. Will return a list of assets with contracts matching any of the addresses in this array. If "token_ids" is also specified, then it will only return assets that match each (address, token_id) pairing, respecting order. |
| 33 | +
|
| 34 | + order_by: |
| 35 | + How to order the assets returned. By default, the API returns the fastest ordering (contract address and token id). Options you can set are token_id, sale_date (the last sale's transaction's timestamp), sale_count (number of sales), visitor_count (number of unique visitors), and sale_price (the last sale's total_price) |
| 36 | +
|
| 37 | + order_direction: |
| 38 | + Can be asc for ascending or desc for descending |
| 39 | +
|
| 40 | + offset: |
| 41 | + Offset |
| 42 | +
|
| 43 | + limit: |
| 44 | + Defaults to 20, capped at 50. |
| 45 | +
|
| 46 | + collection: |
| 47 | + Limit responses to members of a collection. Case sensitive and must match the collection slug exactly. Will return all assets from all contracts in a collection. |
| 48 | +
|
| 49 | + :return: Parsed JSON |
| 50 | + """ |
| 51 | + api_key: Optional[str] = None |
| 52 | + owner: Optional[str] = None |
| 53 | + token_ids: Optional[list[int]] = None |
| 54 | + asset_contract_address: Optional[list[str]] = None |
| 55 | + asset_contract_addresses: Optional[str] = None |
| 56 | + collection: Optional[str] = None |
| 57 | + order_by: Optional[AssetsOrderBy] = None |
| 58 | + order_direction: str = None |
| 59 | + offset: int = 0 |
| 60 | + limit: int = 20 |
| 61 | + |
| 62 | + def __post_init__(self): |
| 63 | + self.validate_request_params() |
| 64 | + self._response: Optional[Response] = None |
| 65 | + |
| 66 | + @property |
| 67 | + def http_response(self): |
| 68 | + self._validate_response_property() |
| 69 | + return self._response |
| 70 | + |
| 71 | + @property |
| 72 | + def response(self) -> list[Asset]: |
| 73 | + self._validate_response_property() |
| 74 | + assets_json = self._response.json()['assets'] |
| 75 | + assets = [Asset(asset_json) for asset_json in assets_json] |
| 76 | + return assets |
| 77 | + |
| 78 | + @property |
| 79 | + def url(self): |
| 80 | + return OpenseaApiEndpoints.ASSETS.value |
| 81 | + |
| 82 | + def get_request(self, *args, **kwargs): |
| 83 | + self._response = super().get_request(self.url, **self._request_params) |
| 84 | + |
| 85 | + @property |
| 86 | + def _request_params(self) -> dict[dict]: |
| 87 | + params = dict( |
| 88 | + owner=self.owner, token_ids=self.token_ids, asset_contract_address=self.asset_contract_address, |
| 89 | + asset_contract_addresses=self.asset_contract_addresses, collection=self.collection, |
| 90 | + order_by=self.order_by, order_direction=self.order_direction, offset=self.offset, limit=self.limit |
| 91 | + ) |
| 92 | + return dict(api_key=self.api_key, params=params) |
| 93 | + |
| 94 | + def validate_request_params(self) -> None: |
| 95 | + self._validate_mandatory_params() |
| 96 | + self._validate_asset_contract_addresses() |
| 97 | + self._validate_order_direction() |
| 98 | + self._validate_order_by() |
| 99 | + self._validate_limit() |
| 100 | + |
| 101 | + def _validate_response_property(self): |
| 102 | + if self._response is None: |
| 103 | + raise AttributeError('You must call self.request prior to accessing self.response') |
| 104 | + |
| 105 | + def _validate_mandatory_params(self): |
| 106 | + mandatory = self.owner, self.token_ids, self.asset_contract_address, self.asset_contract_addresses, self.collection |
| 107 | + if all((a is None for a in mandatory)): |
| 108 | + raise ValueError("At least one of the following parameters must not be None:\n" |
| 109 | + "owner, token_ids, asset_contract_address, asset_contract_addresses, collection") |
| 110 | + |
| 111 | + def _validate_asset_contract_addresses(self): |
| 112 | + if self.asset_contract_address and self.asset_contract_addresses: |
| 113 | + raise ValueError( |
| 114 | + "You cannot simultaneously get_request for a single contract_address and a list of contract_addresses." |
| 115 | + ) |
| 116 | + |
| 117 | + if self.token_ids and not (self.asset_contract_address or self.asset_contract_addresses): |
| 118 | + raise ValueError( |
| 119 | + "You cannot query for token_ids without specifying either " |
| 120 | + "asset_contract_address or asset_contract_addresses." |
| 121 | + ) |
| 122 | + |
| 123 | + def _validate_order_direction(self): |
| 124 | + if self.order_direction is None: |
| 125 | + return |
| 126 | + |
| 127 | + if self.order_direction not in ['asc', 'desc']: |
| 128 | + raise ValueError( |
| 129 | + f"order_direction param value ({self.order_direction}) is invalid. " |
| 130 | + f"Must be either 'asc' or 'desc', case sensitive." |
| 131 | + ) |
| 132 | + |
| 133 | + def _validate_order_by(self) -> None: |
| 134 | + if self.order_by is None: |
| 135 | + return |
| 136 | + |
| 137 | + if self.order_by not in AssetsOrderBy.list(): |
| 138 | + raise ValueError( |
| 139 | + f"order_by param value ({self.order_by}) is invalid. " |
| 140 | + f"Must be a value from {AssetsOrderBy.list()}, case sensitive." |
| 141 | + ) |
| 142 | + |
| 143 | + def _validate_limit(self): |
| 144 | + if not isinstance(self.limit, int) or not 0 <= self.limit <= 50: |
| 145 | + raise ValueError(f"limit param must be an int between 0 and 50.") |
0 commit comments