@@ -67,21 +67,21 @@ def __call__(self, r):
6767
6868class Service (object ):
6969 def __init__ (
70- self ,
71- service_info : ServiceInfo ,
72- api_info : dict [str , ApiInfo ],
73- http_client : Optional [httpx .Client ] = None ,
74- async_http_client : Optional [httpx .AsyncClient ] = None ,
70+ self ,
71+ service_info : ServiceInfo ,
72+ api_info : dict [str , ApiInfo ],
73+ http_client : Optional [httpx .Client ] = None ,
74+ async_http_client : Optional [httpx .AsyncClient ] = None ,
7575 ):
7676 self .service_info = service_info
7777 self .api_info = api_info
7878 self .init ()
7979 self .init_http_client (http_client , async_http_client )
8080
8181 def init_http_client (
82- self ,
83- http_client : Optional [httpx .Client ] = None ,
84- async_http_client : Optional [httpx .AsyncClient ] = None ,
82+ self ,
83+ http_client : Optional [httpx .Client ] = None ,
84+ async_http_client : Optional [httpx .AsyncClient ] = None ,
8585 ):
8686 if http_client :
8787 self .http_client = http_client
@@ -361,9 +361,9 @@ async def _arequest(self, action, params):
361361 def prepare_request (self , api_info , params , doseq = 0 ):
362362 for key in params :
363363 if (
364- type (params [key ]) == int
365- or type (params [key ]) == float
366- or type (params [key ]) == bool
364+ type (params [key ]) == int
365+ or type (params [key ]) == float
366+ or type (params [key ]) == bool
367367 ):
368368 params [key ] = str (params [key ])
369369 elif sys .version_info [0 ] != 3 :
@@ -444,12 +444,12 @@ def sign_sts2(self, policy, expire):
444444 inner_token .signature = Util .to_hex (Util .hmac_sha256 (key , sign_str ))
445445
446446 sts .session_token = (
447- "STS2"
448- + base64 .b64encode (
449- json .dumps (inner_token , cls = ComplexEncoder , sort_keys = True )
450- .replace (" " , "" )
451- .encode ("utf-8" )
452- ).decode ()
447+ "STS2"
448+ + base64 .b64encode (
449+ json .dumps (inner_token , cls = ComplexEncoder , sort_keys = True )
450+ .replace (" " , "" )
451+ .encode ("utf-8" )
452+ ).decode ()
453453 )
454454 return sts
455455
@@ -459,14 +459,14 @@ def to_rfc3339(t):
459459 pos = format_time .find ("+" )
460460 if pos == - 1 :
461461 pos = format_time .find ("-" )
462- return format_time [: pos + 3 ] + ":" + format_time [pos + 3 : pos + 5 ]
462+ return format_time [: pos + 3 ] + ":" + format_time [pos + 3 : pos + 5 ]
463463
464464
465465class AppAPIMixin :
466466 def __init__ (
467- self ,
468- http_client : httpx .Client ,
469- async_http_client : httpx .AsyncClient ,
467+ self ,
468+ http_client : httpx .Client ,
469+ async_http_client : httpx .AsyncClient ,
470470 ) -> None :
471471 self .base_url = ""
472472 self .http_client = http_client
@@ -475,7 +475,7 @@ def __init__(
475475 def set_app_base_url (self , base_url : str ):
476476 self .base_url = base_url
477477
478- async def _apost (self , app_key : str , action : str , params : dict ) -> str :
478+ async def _apost (self , app_key : str , action : str , params : dict , _headers : Optional [ dict ] ) -> str :
479479 if self .base_url == "" :
480480 raise Exception (
481481 "base_url not set, you should call set_app_base_url() first"
@@ -484,7 +484,8 @@ async def _apost(self, app_key: str, action: str, params: dict) -> str:
484484 app_url = f"{ self .base_url } /{ action } "
485485
486486 headers = {"Apikey" : f"{ app_key } " , "Content-Type" : "application/json" }
487-
487+ if _headers is not None :
488+ headers .update (_headers )
488489 response = await self .async_http_client .post (
489490 app_url , json = params , headers = headers
490491 )
@@ -497,15 +498,17 @@ async def _apost(self, app_key: str, action: str, params: dict) -> str:
497498 raise Exception ("empty response" )
498499 return res_text
499500
500- def _post (self , app_key : str , action : str , params : dict ) -> str :
501+ def _post (self , app_key : str , action : str , params : dict , _headers : Optional [ dict ] ) -> str :
501502 if self .base_url == "" :
502503 raise Exception (
503504 "base_url not set, you should call set_app_base_url() first"
504505 )
505506
506507 app_url = f"{ self .base_url } /{ action } "
507-
508508 headers = {"Apikey" : f"{ app_key } " , "Content-Type" : "application/json" }
509+ if _headers is not None :
510+ headers .update (_headers )
511+
509512 response = self .http_client .post (app_url , json = params , headers = headers )
510513 try :
511514 response .raise_for_status () # Raise an exception for bad status codes
@@ -517,7 +520,7 @@ def _post(self, app_key: str, action: str, params: dict) -> str:
517520 return res_text
518521
519522 def _sse_post (
520- self , app_key : str , action : str , params : dict
523+ self , app_key : str , action : str , params : dict
521524 ) -> Generator [ServerSentEvent , None , None ]:
522525 if self .base_url == "" :
523526 raise Exception (
@@ -529,17 +532,17 @@ def _sse_post(
529532 headers = {"Apikey" : f"{ app_key } " , "Content-Type" : "application/json" }
530533
531534 with connect_sse (
532- self .http_client ,
533- method = "POST" ,
534- url = app_url ,
535- json = params ,
536- headers = headers ,
535+ self .http_client ,
536+ method = "POST" ,
537+ url = app_url ,
538+ json = params ,
539+ headers = headers ,
537540 ) as event_source :
538541 for sse in event_source .iter_sse ():
539542 yield sse
540543
541544 async def _asse_post (
542- self , app_key : str , action : str , params : dict
545+ self , app_key : str , action : str , params : dict
543546 ) -> AsyncGenerator [ServerSentEvent , None ]:
544547 if self .base_url == "" :
545548 raise Exception (
@@ -551,11 +554,11 @@ async def _asse_post(
551554 headers = {"Apikey" : f"{ app_key } " , "Content-Type" : "application/json" }
552555
553556 async with aconnect_sse (
554- self .async_http_client ,
555- method = "POST" ,
556- url = app_url ,
557- json = params ,
558- headers = headers ,
557+ self .async_http_client ,
558+ method = "POST" ,
559+ url = app_url ,
560+ json = params ,
561+ headers = headers ,
559562 ) as event_source :
560563 async for sse in event_source .aiter_sse ():
561564 yield sse
0 commit comments