
    9i                     d    S r SSKrSSKrSSKJrJr  SSKJr  SSK	J
r
  SSK	Jr  S rS rS	 rS
 rg)a  
This module provides general http handler functions for processing http responses from AIHC V2 services.

This module contains utility functions for parsing HTTP responses from AIHC (AI Hosting Cloud) services,
including JSON parsing, error handling, and response object conversion. The functions are designed
to work with the BCE (Baidu Cloud Engine) SDK framework.

Functions:
    parse_json: Parse JSON response and convert to Python object
    dict_to_python_object: Convert dictionary to Python object with Expando
    parse_json_list: Parse JSON list response
    parse_error: Handle error responses and raise appropriate exceptions
    N)compatutils)Expando)BceClientError)BceServerErrorc                    U R                  5       nU(       aY  UR                  R                  [        R                  " U[
        S9R                  5        SUR                  ;   a  UR                  S	 U R                  5         g)a  
Parse JSON response and convert to Python object.

If the response body is not empty, convert it to a Python object and update
the response object's attributes. The http_response is always closed if no error occurs.

Args:
    http_response: The http_response object returned by HTTPConnection.getresponse()
    response: General response object which will be returned to the caller
    
Returns:
    bool: Always returns True
    
Note:
    This function removes the 'metadata' key from the response if it exists.
object_hookmetadataT)read__dict__updatejsonloadsdict_to_python_objectclosehttp_responseresponsebodys      c/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/baidubce/services/aihc/aihc_handler.py
parse_jsonr   #   sl    " D  3"55=X	? ***!!*-    c                 ~    0 n[        U R                  5       5       H  u  p#[        U5      nX1U'   M     [        U5      $ )z
Convert dictionary to Python object with Expando.

Args:
    d: Dictionary to convert
    
Returns:
    Expando: Python object with dictionary attributes
)listitemsstrr   )dattrkvs       r   r   r   @   s;     DQWWYFQ   4=r   c                     U R                  5       nU(       aT  [        R                  " U5      n[        R                  " U[
        R                  S9UR                  S'   X!R                  S'   U R                  5         g)a  
Parse JSON list response and convert to Python object.

If the body is not empty, convert it to a Python object and set as the value of
response.result. The http_response is always closed if no error occurs.

Args:
    http_response: The http_response object returned by HTTPConnection.getresponse()
    response: General response object which will be returned to the caller
    
Returns:
    bool: Always returns True
r	   resultraw_dataT)	r   r   convert_to_stringr   r   r   r   r   r   r   s      r   parse_json_listr&   Q   sc     D''-&*jj5C^C^&_(#(,*%r   c                    U R                   S-  [        R                  R                  S-  :X  a  gU R                   S-  [        R                  R                  S-  :X  a  [        S5      eU R                  5       nU(       d;  [        U R                  UR                  R                  S9nU R                   Ul        Ue[        R                  " [        R                  " U5      5      n[!        U5      nSU;   a  US   b  US   nSnSU;   a  US   b  US   nUR                  R                  nSU;   a  US   b  US   n[        XVUS	9nU R                   Ul        Ue)
a0  
Handle error responses and raise appropriate exceptions.

If the HTTP status code is not 2xx, parse the error response and raise
appropriate BCE exceptions. The http_response is always closed.

Args:
    http_response: The http_response object returned by HTTPConnection.getresponse()
    response: General response object which will be returned to the caller
    
Returns:
    bool: False if HTTP status code is 2xx
    
Raises:
    BceClientError: If HTTP status code is 1xx (not handled)
    BceServerError: If HTTP status code is not 2xx (error response)
d   Fs#   Can not handle 1xx http status code)
request_idmessage	Exceptioncoder)   )r,   r)   )statushttpclientOKCONTINUEr   r   r   reasonr   bce_request_idstatus_coder   r   r   r%   r   )r   r   r   bse
error_dictr*   r,   r)   s           r   parse_errorr7   h   s@   $ s"dkknn&;;s"dkk&:&:c&AACDDD]11h>O>O>^>^_'..	F44T:;J*oGJ:i#8#DY'D
6 2 >&!""11Jz!j&>&J-



CC#**CO
Ir   )__doc__http.clientr.   r   baidubcer   r   baidubce.utilsr   baidubce.exceptionr   r   r   r   r&   r7    r   r   <module>r>      s2      " " - -:".)r   