hiagent-api 2.0.1__tar.gz → 2.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hiagent-api
3
- Version: 2.0.1
3
+ Version: 2.2.0
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: aiofiles>=23.1.0
@@ -8,6 +8,7 @@ Requires-Dist: httpx-sse>=0.4.0
8
8
  Requires-Dist: httpx>=0.28.1
9
9
  Requires-Dist: pydantic>=2.11.5
10
10
  Requires-Dist: python-dotenv>=1.1.0
11
+ Requires-Dist: strenum>=0.4.15
11
12
  Requires-Dist: volcengine>=1.0.187
12
13
  Description-Content-Type: text/markdown
13
14
 
@@ -67,11 +67,11 @@ class VolcAuth(AuthBase):
67
67
 
68
68
  class Service(object):
69
69
  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,
75
75
  ):
76
76
  self.service_info = service_info
77
77
  self.api_info = api_info
@@ -79,9 +79,9 @@ class Service(object):
79
79
  self.init_http_client(http_client, async_http_client)
80
80
 
81
81
  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,
85
85
  ):
86
86
  if http_client:
87
87
  self.http_client = http_client
@@ -361,9 +361,9 @@ class Service(object):
361
361
  def prepare_request(self, api_info, params, doseq=0):
362
362
  for key in params:
363
363
  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
367
367
  ):
368
368
  params[key] = str(params[key])
369
369
  elif sys.version_info[0] != 3:
@@ -444,12 +444,12 @@ class Service(object):
444
444
  inner_token.signature = Util.to_hex(Util.hmac_sha256(key, sign_str))
445
445
 
446
446
  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()
453
453
  )
454
454
  return sts
455
455
 
@@ -459,14 +459,14 @@ class Service(object):
459
459
  pos = format_time.find("+")
460
460
  if pos == -1:
461
461
  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]
463
463
 
464
464
 
465
465
  class AppAPIMixin:
466
466
  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,
470
470
  ) -> None:
471
471
  self.base_url = ""
472
472
  self.http_client = http_client
@@ -475,7 +475,7 @@ class AppAPIMixin:
475
475
  def set_app_base_url(self, base_url: str):
476
476
  self.base_url = base_url
477
477
 
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] = None) -> str:
479
479
  if self.base_url == "":
480
480
  raise Exception(
481
481
  "base_url not set, you should call set_app_base_url() first"
@@ -484,7 +484,8 @@ class AppAPIMixin:
484
484
  app_url = f"{self.base_url}/{action}"
485
485
 
486
486
  headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
487
-
487
+ if _headers is not None:
488
+ headers.update(_headers)
488
489
  response = await self.async_http_client.post(
489
490
  app_url, json=params, headers=headers
490
491
  )
@@ -497,15 +498,17 @@ class AppAPIMixin:
497
498
  raise Exception("empty response")
498
499
  return res_text
499
500
 
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] = None) -> str:
501
502
  if self.base_url == "":
502
503
  raise Exception(
503
504
  "base_url not set, you should call set_app_base_url() first"
504
505
  )
505
506
 
506
507
  app_url = f"{self.base_url}/{action}"
507
-
508
508
  headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
509
+ if _headers is not None:
510
+ headers.update(_headers)
511
+
509
512
  response = self.http_client.post(app_url, json=params, headers=headers)
510
513
  try:
511
514
  response.raise_for_status() # Raise an exception for bad status codes
@@ -517,7 +520,7 @@ class AppAPIMixin:
517
520
  return res_text
518
521
 
519
522
  def _sse_post(
520
- self, app_key: str, action: str, params: dict
523
+ self, app_key: str, action: str, params: dict
521
524
  ) -> Generator[ServerSentEvent, None, None]:
522
525
  if self.base_url == "":
523
526
  raise Exception(
@@ -529,17 +532,17 @@ class AppAPIMixin:
529
532
  headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
530
533
 
531
534
  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,
537
540
  ) as event_source:
538
541
  for sse in event_source.iter_sse():
539
542
  yield sse
540
543
 
541
544
  async def _asse_post(
542
- self, app_key: str, action: str, params: dict
545
+ self, app_key: str, action: str, params: dict
543
546
  ) -> AsyncGenerator[ServerSentEvent, None]:
544
547
  if self.base_url == "":
545
548
  raise Exception(
@@ -551,11 +554,11 @@ class AppAPIMixin:
551
554
  headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
552
555
 
553
556
  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,
559
562
  ) as event_source:
560
563
  async for sse in event_source.aiter_sse():
561
564
  yield sse