22import gc
33import secrets
44from pathlib import Path
5- from typing import Optional , List , Dict
6-
7- from httpx import URL
85
96try :
107 from importlib import resources
118except ImportError :
129 import importlib_resources as resources
1310
1411import httpx
15- from pydantic import parse_obj_as
16- from authlib .oidc .core import IDToken
1712from authlib .jose import jwt
18- from .queries import load_query
19- from . import errors
20- from . import models as m
21- from . import keys
13+ from authlib .oidc .core import IDToken
14+ from httpx import URL
15+ from pydantic import parse_obj_as
2216
17+ from . import errors , keys
18+ from . import models as m
19+ from .queries import load_query
2320
2421CLIENT_ID = "ONEKEY Python SDK"
2522TOKEN_NAMESPACE = "https://www.onekey.com/"
@@ -51,8 +48,8 @@ class Client:
5148 def __init__ (
5249 self ,
5350 api_url : str ,
54- ca_bundle : Optional [ Path ] = None ,
55- disable_tls_verify : Optional [ bool ] = False ,
51+ ca_bundle : Path | None = None ,
52+ disable_tls_verify : bool | None = False ,
5653 ):
5754 self ._api_url = URL (api_url )
5855 self ._client = self ._setup_httpx_client (api_url , ca_bundle , disable_tls_verify )
@@ -66,8 +63,8 @@ def __init__(
6663 def _setup_httpx_client (
6764 self ,
6865 api_url : str ,
69- ca_bundle : Optional [ Path ] = None ,
70- disable_tls_verify : Optional [ bool ] = False ,
66+ ca_bundle : Path | None = None ,
67+ disable_tls_verify : bool | None = False ,
7168 ):
7269 if disable_tls_verify :
7370 return httpx .Client (base_url = api_url , verify = False )
@@ -78,17 +75,15 @@ def _setup_httpx_client(
7875 raise errors .InvalidCABundle
7976
8077 return httpx .Client (base_url = api_url , verify = str (ca ))
81- else :
82- with resources .path (keys , "ca.pem" ) as ca :
83- return httpx .Client (base_url = api_url , verify = str (ca ))
78+ with resources .path (keys , "ca.pem" ) as ca :
79+ return httpx .Client (base_url = api_url , verify = str (ca ))
8480
85- def _load_key (self , key_name : str , path : Optional [ Path ] = None ):
81+ def _load_key (self , key_name : str , path : Path | None = None ):
8682 if path is not None :
8783 return path .read_bytes ()
88- else :
89- response = self ._client .get (f"/{ key_name } .pem" )
90- response .raise_for_status ()
91- return response .read ()
84+ response = self ._client .get (f"/{ key_name } .pem" )
85+ response .raise_for_status ()
86+ return response .read ()
9287
9388 @property
9489 def api_url (self ) -> URL :
@@ -111,7 +106,7 @@ def login(self, email: str, password: str):
111106 claims_cls = IDToken ,
112107 )
113108 tenants = id_token [TOKEN_NAMESPACE + "tenants" ]
114- tenants = parse_obj_as (List [m .Tenant ], tenants )
109+ tenants = parse_obj_as (list [m .Tenant ], tenants )
115110 self ._state .tenants = {e .name : e for e in tenants }
116111 self ._state .email = email
117112 self ._state .raw_id_token = json_res ["id_token" ]
@@ -131,7 +126,7 @@ def use_token(self, token: str):
131126 self ._state .tenants = {tenant .name : tenant }
132127 self ._state .tenant = tenant
133128
134- def _post (self , path : str , headers : Optional [ Dict ] = None , ** kwargs ):
129+ def _post (self , path : str , headers : dict | None = None , ** kwargs ):
135130 response = self ._client .post (path , headers = headers , ** kwargs )
136131 response .raise_for_status ()
137132 return response .json ()
@@ -152,7 +147,7 @@ def get_tenant(self, name: str):
152147 return self ._state .tenants [name ]
153148
154149 @_login_required
155- def get_all_tenants (self ) -> List [m .Tenant ]:
150+ def get_all_tenants (self ) -> list [m .Tenant ]:
156151 """Get the list of Tenants you have access to."""
157152 return list (self ._state .tenants .values ())
158153
@@ -182,7 +177,7 @@ def refresh_tenant_token(self):
182177 self .use_tenant (self ._state .tenant )
183178
184179 @_tenant_required
185- def query (self , query : str , variables : Optional [ Dict ] = None , timeout = 60 ):
180+ def query (self , query : str , variables : dict | None = None , timeout = 60 ):
186181 """Issues a GraphQL query and returns the results"""
187182 res = self ._post_with_token (
188183 "/graphql" , json = {"query" : query , "variables" : variables }, timeout = timeout
0 commit comments