|
| 1 | +import os |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Dict |
| 5 | + |
| 6 | +import requests |
| 7 | +from requests.auth import HTTPBasicAuth |
| 8 | +from requests.exceptions import JSONDecodeError |
| 9 | + |
| 10 | +API_VERSION = "v1" |
| 11 | +API_BASE_URL = os.path.join("https://picketapi.com/api/", API_VERSION) |
| 12 | + |
| 13 | + |
| 14 | +def is_successful_status_code(status_code): |
| 15 | + return status_code >= 200 and status_code < 300 |
| 16 | + |
| 17 | + |
| 18 | +class PicketAPIException(Exception): |
| 19 | + def __init__(self, msg: str, code: str): |
| 20 | + super().__init__(msg) |
| 21 | + self.msg = msg |
| 22 | + self.code = code |
| 23 | + |
| 24 | + def __str__(self): |
| 25 | + return self.msg |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class NonceResponse: |
| 30 | + nonce: str |
| 31 | + statement: str |
| 32 | + format: str |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def from_dict(cls, d): |
| 36 | + return cls(d["nonce"], d["statement"], d["format"]) |
| 37 | + |
| 38 | + |
| 39 | +TokenBalances = Dict[str, Dict[str, str]] |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class AuthorizedUser: |
| 44 | + chain: str |
| 45 | + wallet_address: str |
| 46 | + display_name: str |
| 47 | + token_balances: TokenBalances |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def from_dict(cls, d): |
| 51 | + return cls( |
| 52 | + d["chain"], d["wallet_address"], d["display_name"], d["token_balances"] |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +@dataclass |
| 57 | +class AuthResponse: |
| 58 | + access_token: str |
| 59 | + user: AuthorizedUser |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def from_dict(cls, d): |
| 63 | + user = AuthorizedUser.from_dict(d["user"]) |
| 64 | + return cls(d["accessToken"], user) |
| 65 | + |
| 66 | + |
| 67 | +@dataclass |
| 68 | +class TokenOwnershipResponse: |
| 69 | + allowed: bool |
| 70 | + walletAddress: str |
| 71 | + token_balances: TokenBalances |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def from_dict(cls, d): |
| 75 | + return cls(d["allowed"], d["walletAddress"], d["token_balances"]) |
| 76 | + |
| 77 | + |
| 78 | +class Picket: |
| 79 | + def __init__(self, api_key: str, **kwargs): |
| 80 | + self.api_key = api_key |
| 81 | + # Base URL for API |
| 82 | + # Configurable for testing |
| 83 | + self.base_url = kwargs.get("base_url", API_BASE_URL) |
| 84 | + |
| 85 | + def headers(self): |
| 86 | + return { |
| 87 | + "User-Agent": "Picket Python Client", |
| 88 | + "Content-Type": "application/json", |
| 89 | + } |
| 90 | + |
| 91 | + def post_request(self, path: str, **kwargs): |
| 92 | + url = os.path.join(self.base_url, path) |
| 93 | + auth = HTTPBasicAuth(self.api_key, "") |
| 94 | + headers = self.headers() |
| 95 | + |
| 96 | + req = requests.post(url, auth=auth, headers=headers, data=kwargs) |
| 97 | + try: |
| 98 | + data = req.json() |
| 99 | + except JSONDecodeError: |
| 100 | + raise Exception(req.text) |
| 101 | + |
| 102 | + if not is_successful_status_code(req.status_code): |
| 103 | + raise PicketAPIException(data["msg"], data["code"]) |
| 104 | + |
| 105 | + return data |
| 106 | + |
| 107 | + # nonce |
| 108 | + def nonce( |
| 109 | + self, chain: str, wallet_address: str, locale: str = "en-US" |
| 110 | + ) -> NonceResponse: |
| 111 | + data = self.post_request( |
| 112 | + "auth/nonce", chain=chain, wallet_address=wallet_address, locale=locale |
| 113 | + ) |
| 114 | + return NonceResponse.from_dict(data) |
| 115 | + |
| 116 | + def auth( |
| 117 | + self, |
| 118 | + chain: str, |
| 119 | + wallet_address: str, |
| 120 | + signature: str, |
| 121 | + requirements: dict = {}, |
| 122 | + context: dict = {}, |
| 123 | + ) -> AuthResponse: |
| 124 | + data = self.post_request( |
| 125 | + "auth", |
| 126 | + chain=chain, |
| 127 | + wallet_address=wallet_address, |
| 128 | + signature=signature, |
| 129 | + requirements=requirements, |
| 130 | + context=context, |
| 131 | + ) |
| 132 | + return AuthResponse.from_dict(data) |
| 133 | + |
| 134 | + def authz( |
| 135 | + self, access_token: str, requirements: dict, revalidate: bool = False |
| 136 | + ) -> AuthResponse: |
| 137 | + data = self.post_request( |
| 138 | + "authz", |
| 139 | + access_token=access_token, |
| 140 | + requirements=requirements, |
| 141 | + revalidate=revalidate, |
| 142 | + ) |
| 143 | + return AuthResponse.from_dict(data) |
| 144 | + |
| 145 | + def validate(self, access_token: str, requirements: dict = {}) -> AuthorizedUser: |
| 146 | + data = self.post_request( |
| 147 | + "auth/validate", access_token=access_token, requirements=requirements |
| 148 | + ) |
| 149 | + return AuthorizedUser.from_dict(data) |
| 150 | + |
| 151 | + def token_ownesrhip( |
| 152 | + self, chain: str, wallet_address: str, **kwargs |
| 153 | + ) -> TokenOwnershipResponse: |
| 154 | + path = os.path.join( |
| 155 | + "chains", chain, "wallets", wallet_address, "tokenOwnership" |
| 156 | + ) |
| 157 | + data = self.post_request(path, **kwargs) |
| 158 | + return TokenOwnershipResponse.from_dict(data) |
0 commit comments