Ë
    ÑÕ¯iÓ6  ã                   óÀ  — d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	  G d„ de
«      Z G d„ de«      Z G d	„ d
e«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ dej.                  j0                  «      Zd„ Z G d „ d!e«      Z G d"„ d#e«      Zy)$ú4A connection object to interface with REST services.é    N)Úurllib)Úformatsc                   ó   — e Zd ZdZdd„Zy)ÚErrorz'A general error derived from Exception.Nc                 óL   — t         j                  | |«       || _        || _        y ©N)Ú	ExceptionÚ__init__ÚurlÚcode)ÚselfÚmsgr   r   s       úN/var/www/html/venv/lib/python3.12/site-packages/pyactiveresource/connection.pyr   zError.__init__   s    € Ü×Ñ˜4 Ô%ØˆŒØˆ�	ó    )NNN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   © r   r   r   r      s
   „ Ù1ôr   r   c                   ó   — e Zd ZdZdd„Zy)ÚServerErrorz,An error caused by an ActiveResource server.Nc                 ó    — |�7t         j                  | |j                  |j                  |j                  «       y t         j                  | «       y r	   )r   r   r   r   r   )r   Úresponses     r   r   zServerError.__init__   s2   € ØÐÜ�N‰N˜4 §¡¨x¯|©|¸X¿]¹]ÕKä�N‰N˜4Õ r   r	   r   r   r   r   r   r      s
   „ Ù6ô!r   r   c                   ó   — e Zd ZdZdd„Zy)ÚConnectionErrorz&An error caused by network connection.Nc                 ó  — |st        d d«      | _        d }n&t         j                  |«      | _        |j                  }|st	        | j                  «      }t
        j                  | ||| j                  j                  «       y )NÚ )ÚResponser   Úfrom_httpresponser   Ústrr   r   r   )r   r   Úmessager   s       r   r   zConnectionError.__init__%   s`   € ÙÜ$ T¨2Ó.ˆDŒMØ‰Cä$×6Ñ6°xÓ@ˆDŒMØ—,‘,ˆCÙÜ˜$Ÿ-™-Ó(ˆGä�‰�t˜W c¨4¯=©=×+=Ñ+=Õ>r   ©NNr   r   r   r   r   r   "   s
   „ Ù0ô
?r   r   c                   ó   — e Zd ZdZy)ÚRedirectionzHTTP 3xx redirection.N©r   r   r   r   r   r   r   r&   r&   2   s   „ ÙØr   r&   c                   ó   — e Zd ZdZy)ÚClientErrorz,An error caused by an ActiveResource client.Nr'   r   r   r   r)   r)   7   s   „ Ù6àr   r)   c                   ó   — e Zd ZdZy)ÚResourceConflictz2An error raised when there is a resource conflict.Nr'   r   r   r   r+   r+   =   s   „ Ù<àr   r+   c                   ó   — e Zd ZdZy)ÚResourceInvalidz+An error raised when a resource is invalid.Nr'   r   r   r   r-   r-   C   ó   „ Ù5àr   r-   c                   ó   — e Zd ZdZdd„Zy)ÚResourceNotFoundz-An error raised when a resource is not found.Nc                 ór   — |�|€|j                   ›d|j                  ›�}t        j                  | ||¬«       y )Nz: )r   r#   )r   r   r)   r   )r   r   r#   s      r   r   zResourceNotFound.__init__M   s3   € ØÐ G OØ"*§,£,°·²Ð=ˆGä×Ñ˜T¨H¸gÐÕFr   r$   r   r   r   r   r0   r0   I   s   „ Ù7ôGr   r0   c                   ó   — e Zd ZdZy)Ú
BadRequestz0An error raised when client sends a bad request.Nr'   r   r   r   r3   r3   T   s   „ Ù:àr   r3   c                   ó   — e Zd ZdZy)ÚUnauthorizedAccessz/An error raised when an access is unauthorized.Nr'   r   r   r   r5   r5   Z   s   „ Ù9àr   r5   c                   ó   — e Zd ZdZy)ÚForbiddenAccessz+An error raised when access is not allowed.Nr'   r   r   r   r7   r7   `   r.   r   r7   c                   ó   — e Zd ZdZy)ÚMethodNotAllowedz-An error raised when a method is not allowed.Nr'   r   r   r   r9   r9   f   s   „ Ù7àr   r9   c                   ó"   — e Zd ZdZd„ Zd„ Zd„ Zy)ÚRequestz1A request object which allows additional methods.c                 ól   — d | _         t        j                  j                  j                  | g|¢­i |¤Ž y r	   )Ú_methodr   Úrequestr;   r   )r   ÚargsÚkwargss      r   r   zRequest.__init__o   s+   € ØˆŒÜ�‰×Ñ×'Ñ'¨Ð>¨tÒ>°vÓ>r   c                 ó„   — | j                   s)t        j                  j                  j	                  | «      S | j                   S )zReturn the HTTP method.)r=   r   r>   r;   Ú
get_method©r   s    r   rB   zRequest.get_methods   s.   € à�|Š|Ü—>‘>×)Ñ)×4Ñ4°TÓ:Ð:Ø�|‰|Ðr   c                 ó   — || _         y)zSet the HTTP method.N)r=   )r   Úmethods     r   Ú
set_methodzRequest.set_methody   s	   € àˆ�r   N)r   r   r   r   r   rB   rF   r   r   r   r;   r;   l   s   „ Ù;ò?òór   r;   c                  óF   — t         j                  } | d   dk\  xr | d   dk\  S )zKDetermines if our version of urllib.request.urlopen has a timeout argument.r   é   é   é   )ÚsysÚversion_info)Úversions    r   Ú_urllib_has_timeoutrN   ~   s*   € ô ×Ñ€'Ø	�‰�q‰Ò	,˜W Q™Z¨1™_Ð,r   c                   óB   — e Zd ZdZd	d„Zd„ Zd„ Zd„ Zd
d„Ze	d„ «       Z
y)r    z+Represents a response from the http server.Nc                 óR   — || _         || _        || _        |€i }|| _        || _        y)a  Initialize a new Response object.

        code, body, headers, msg are retrievable as instance attributes.
        Individual headers can be retrieved using dictionary syntax (i.e.
        response['header'] => value.

        Args:
            code: The HTTP response code returned by the server.
            body: The body of the response.
            headers: A dictionary of HTTP headers.
            msg: The HTTP message (e.g. 200 OK => 'OK').
            response: The original httplib.HTTPResponse (if any).
        N)r   r   ÚbodyÚheadersr   )r   r   rQ   rR   r   r   s         r   r   zResponse.__init__‰   s1   € ð ˆŒ	ØˆŒØˆŒ	Øˆ?ØˆGØˆŒØ ˆ�r   c                 ó²   — t        |t        «      rG| j                  | j                  | j                  f|j                  |j                  |j                  fk(  S y)NF)Ú
isinstancer    r   rQ   rR   )r   Úothers     r   Ú__eq__zResponse.__eq__Ÿ   sE   € Ü�eœXÔ&Ø—Y‘Y §	¡	¨4¯<©<Ð8Ø—Z‘Z §¡¨U¯]©]Ð;ñ<ð =àr   c           	      óp   — d| j                   ›d| j                  ›d| j                  ›d| j                  ›d�	S )NzResponse(code=z, body="z", headers=z, msg="z"))r   rQ   rR   r   rC   s    r   Ú__repr__zResponse.__repr__¥   s%   � à�I‹I�t—y“y $§,£,°·³ð:ð 	:r   c                 ó    — | j                   |   S r	   ©rR   )r   Úkeys     r   Ú__getitem__zResponse.__getitem__©   s   € Ø�|‰|˜CÑ Ð r   c                 ó:   — | j                   j                  ||«      S r	   )rR   Úget)r   r[   Úvalues      r   r^   zResponse.get¬   s   € Ø�|‰|×Ñ  UÓ+Ð+r   c                 ó„   —  | |j                   |j                  «       t        |j                  «      |j                  |«      S )z¿Create a Response object based on an httplib.HTTPResponse object.

        Args:
            response: An httplib.HTTPResponse object.
        Returns:
            A Response object.
        )r   ÚreadÚdictrR   r   )Úclsr   s     r   r!   zResponse.from_httpresponse¯   s7   € ñ �8—=‘= (§-¡-£/Ü˜×(Ñ(Ó)¨8¯<©<¸óCð 	Cr   )Nr   Nr	   )r   r   r   r   r   rV   rX   r\   r^   Úclassmethodr!   r   r   r   r    r    †   s4   „ Ù5ó!ò,ò:ò!ó,ð ñ	Có ñ	Cr   r    c                   ó„   — e Zd ZdZdddej
                  fd„Zd„ Zd„ Zdd„Z	d„ Z
dd„Zdd	„Zdd
„Zdd„Zdd„Zdd„Zd„ Zy)Ú
Connectionr   Nc                 óü  — |€t        d«      ‚| j                  |«      \  | _        | _        | _        |xs | j                  xs d| _        |xs | j                  xs d| _        | j                  s| j                  rRt        j                  | j                  ›d| j                  ›�j                  d«      «      j                  d«      | _	        nd| _	        || _
        t        j                  d«      | _        || _        y)aR  Initialize a new Connection object.

        Args:
            site: The base url for connections (e.g. 'http://foo')
            user: username for basic authentication.
            password: password for basic authentication.
            timeout: socket timeout.
            format: format object for en/decoding resource data.
        Nz&Connection site argument requires siter   Ú:zutf-8zpyactiveresource.connection)Ú
ValueErrorÚ_parse_siteÚsiteÚuserÚpasswordÚbase64Ú	b64encodeÚencodeÚdecodeÚauthÚtimeoutÚloggingÚ	getLoggerÚlogÚformat)r   rk   rl   rm   rs   rw   s         r   r   zConnection.__init__¿   sÈ   € ð ˆ<ÜÐEÓFÐFØ.2×.>Ñ.>¸tÓ.DÑ+ˆŒ	�4”9˜dœmØÒ+˜DŸI™IÒ+¨ˆŒ	Ø Ò7 D§M¡MÒ7°RˆŒà�9Š9˜ŸšÜ×(Ñ(°T·Y³YÀÇÂÐ*N×)VÑ)VÐW^Ó)_Ó`×gÑgÐhoÓpˆD�IàˆDŒIØˆŒÜ×$Ñ$Ð%BÓCˆŒØˆ�r   c                 ó6  — t         j                  j                  |«      }|j                  }|j                  r|dt        |j                  «      z   z  }t         j                  j                  |j                  |ddddf«      }||j                  |j                  fS )z¾Retrieve the auth information and base url for a site.

        Args:
            site: The URL to parse.
        Returns:
            A tuple containing (site, username, password).
        rh   r   )
r   ÚparseÚurlparseÚhostnameÚportr"   Ú
urlunparseÚschemeÚusernamerm   )r   rk   ÚpartsÚhostÚnew_sites        r   rj   zConnection._parse_siteÚ   sy   € ô —‘×%Ñ% dÓ+ˆà�~‰~ˆØ�:Š:Ø�Cœ#˜eŸj™j›/Ñ)Ñ)ˆDä—<‘<×*Ñ*¨E¯L©L¸$ÀÀBÈÈBÐ+OÓPˆØ˜%Ÿ.™.¨%¯.©.Ð9Ð9r   c                 ó   — t        |«      S )z‹Return a new request object.

        Args:
            url: The url to connect to.
        Returns:
            A Request object.
        )r;   )r   r   s     r   Ú_requestzConnection._requestë   s   € ô �s‹|Ðr   c           
      óŽ  — t         j                  j                  | j                  |«      }| j                  j                  d||«       | j                  |«      }|j                  |«       |r/t        j                  |«      D ]  \  }}|j                  ||«       Œ | j                  r|j                  dd| j                  z   «       |j                  rndj                  t        j                  |j                  «      D �	�
cg c]  \  }	}
dj                  |	|
f«      ‘Œ c}
}	«      }| j                  j                  d|«       |rT|j                  d| j                  j                   «       ||_        | j                  j                  d|j"                  «       n<|d	v r8|j                  d| j                  j                   «       |j                  d
d«       | j$                  r=t'        «       s3t)        j*                  «       }t)        j,                  | j$                  «       	 d}	 | j/                  | j1                  |«      «      }t:        j=                  |«      }| j                  j                  d|j>                  |j                  |j@                  «       |r|jC                  «        | j$                  r t'        «       st)        j,                  «       	 | j                  j                  d|j>                  |j@                  tE        |jF                  «      «       |S c c}
}	w # t         j2                  j4                  $ r}| j/                  |«      }Y d}~�Œd}~wt         j2                  j6                  $ r}t9        ||«      ‚d}~ww xY w# r|jC                  «        | j$                  r!t'        «       st)        j,                  «       w w w xY w)aE  Perform an HTTP request.

        Args:
            method: The HTTP method (GET, PUT, POST, DELETE).
            path: The HTTP path to retrieve.
            headers: A dictionary of HTTP headers to add.
            data: The data to send as the body of the request.
        Returns:
             A Response object.
        z%s %sÚAuthorizationzBasic Ú
rh   zrequest-headers:%szContent-Typezrequest-body:%s)ÚPOSTÚPUTzContent-LengthÚ0Nz'Response(code=%d, headers=%s, msg="%s")z--> %d %s %db)$r   ry   Úurljoinrk   rv   Úinfor„   rF   ÚsixÚ	iteritemsÚ
add_headerrr   rR   ÚjoinÚdebugrw   Ú	mime_typeÚdatars   rN   ÚsocketÚgetdefaulttimeoutÚsetdefaulttimeoutÚ_handle_errorÚ_urlopenÚerrorÚ	HTTPErrorÚURLErrorr   r    r!   r   r   ÚcloseÚlenrQ   )r   rE   ÚpathrR   r“   r   r>   r[   r_   ÚkÚvÚheader_stringÚold_timeoutÚhttp_responseÚerrr   s                   r   Ú_openzConnection._openõ   sÜ  € ô �l‰l×"Ñ" 4§9¡9¨dÓ3ˆØ�‰�‰�g˜v sÔ+Ø—-‘- Ó$ˆØ×Ñ˜6Ô"ÙÜ!Ÿm™m¨GÖ4‘
��UØ×"Ñ" 3¨Õ.ð 5à�9Š9à×Ñ˜°¸4¿9¹9Ñ0DÔEØ�?Š?Ø ŸI™IÜ'*§}¡}°W·_±_Ô'Eô'GÙ'Eñ =A¸A¸q s§x¡x°°A°Õ'7Ø'Eò'Gó HˆMà�H‰H�N‰NÐ/°Ô?ÙØ×Ñ˜~¨t¯{©{×/DÑ/DÔEØˆGŒLØ�H‰H�N‰NÐ,¨g¯l©lÕ;Ø�Ñ&à
×
Ñ
˜^¨T¯[©[×-BÑ-BÔ
CØ
×
Ñ
Ð-¨sÔ
3à�<Š<Ô 3Ô 5ä ×2Ñ2Ó4ˆKÜ×$Ñ$ T§\¡\Ô2ð	6Ø ˆMð&Ø $× 2Ñ 2°4·=±=ÀÓ3IÓ J�ô
  ×1Ñ1°-Ó@ˆHØ�H‰H�N‰NÐDØ#Ÿ=™=¨(×*:Ñ*:¸H¿L¹LôJñ Ø×#Ñ#Ô%Ø�|Š|Ô$7Ô$9Ü×(Ñ(¨Õ5à�‰�‰�o x§}¡}°h·l±lÜ˜(Ÿ-™-Ó(ô	*àˆùóG'Gøô( —<‘<×)Ñ)ò 8Ø $× 2Ñ 2°3Ó 7–ûÜ—<‘<×(Ñ(ò &Ü˜C “oÐ%ûð&ûñ Ø×#Ñ#Ô%Ø�|Š|Ô$7Ô$9Ü×(Ñ(¨Õ5ð %:ˆ|úsJ   Ã?L
ÈN È L È?AN ÌN Ì8MÍ	N Í N Í/M;Í;N Î N ÎAOc                 óª   — t        «       r+t        j                  j                  || j                  ¬«      S t        j                  j                  |«      S )a  Wrap calls to urllib so they can be overriden.

        Args:
            request: A Request object.
        Returns:
            An httplib.HTTPResponse object.
        Raises:
            urllib.error.HTTPError on server errors.
            urllib.error.URLError on IO errors.
        )rs   )rN   r   r>   Úurlopenrs   )r   r>   s     r   r˜   zConnection._urlopen0  s=   € ô Ô Ü—‘×'Ñ'¨¸¿¹Ð'ÓFÐ
Fä—‘×'Ñ'¨Ó0Ð
0r   c                 ó*   — | j                  d||¬«      S )zËPerform an HTTP get request.

        Args:
            path: The HTTP path to retrieve.
            headers: A dictionary of HTTP headers to add.
        Returns:
            A Response object.
        ÚGETrZ   ©r¥   ©r   rž   rR   s      r   r^   zConnection.get@  s   € ð �z‰z˜% ¨wˆzÓ7Ð7r   c                 ól   — | j                   j                  | j                  ||«      j                  «      S )zòPerform an HTTP get request and return the formatted response.

        Args:
            path: The HTTP path to retrieve.
            headers: A dictionary of HTTP headers to add.
        Returns:
            The resource as a dict.
        )rw   rq   r^   rQ   r«   s      r   Úget_formattedzConnection.get_formattedK  s*   € ð �{‰{×!Ñ! $§(¡(¨4°Ó"9×">Ñ">Ó?Ð?r   c                 ó*   — | j                  d||¬«      S )zÎPerform an HTTP delete request.

        Args:
            path: The HTTP path to retrieve.
            headers: A dictionary of HTTP headers to add.
        Returns:
            A Response object.
        ÚDELETErZ   rª   r«   s      r   ÚdeletezConnection.deleteV  s   € ð �z‰z˜( D°'ˆzÓ:Ð:r   c                 ó,   — | j                  d|||¬«      S )a
  Perform an HTTP put request.

        Args:
            path: The HTTP path to retrieve.
            headers: A dictionary of HTTP headers to add.
            data: The data to send as the body of the request.
        Returns:
            A Response object.
        r‰   ©rR   r“   rª   ©r   rž   rR   r“   s       r   ÚputzConnection.puta  s   € ð �z‰z˜% ¨w¸TˆzÓBÐBr   c                 ó,   — | j                  d|||¬«      S )a  Perform an HTTP post request.

        Args:
            path: The HTTP path to retrieve.
            headers: A dictionary of HTTP headers to add.
            data: The data to send as the body of the request.
        Returns:
            A Response object.
        rˆ   r²   rª   r³   s       r   ÚpostzConnection.postm  s   € ð �z‰z˜& $°¸dˆzÓCÐCr   c                 ó*   — | j                  d||¬«      S )zËPerform an HTTP put request.

        Args:
            path: The HTTP path to retrieve.
            headers: A dictionary of HTTP headers to add.
        Returns:
            A Response object.
        ÚHEADrZ   rª   r«   s      r   ÚheadzConnection.heady  s   € ð �z‰z˜& $°ˆzÓ8Ð8r   c                 ó€  — |j                   dv rt        |«      ‚d|j                   cxk  rdk  r|S  |j                   dk(  rt        |«      ‚|j                   dk(  rt        |«      ‚|j                   dk(  rt	        |«      ‚|j                   dk(  rt        |«      ‚|j                   dk(  rt        |«      ‚|j                   dk(  rt        |«      ‚|j                   d	k(  rt        |«      ‚d|j                   cxk  rd
k  rt        |«      ‚ d
|j                   cxk  rdk  rt        |«      ‚ t        |«      ‚t        |«      ‚)a€  Handle an HTTP error.

        Args:
            err: A urllib.error.HTTPError object.
        Returns:
            An HTTP response object if the error is recoverable.
        Raises:
            Redirection: if HTTP error code 301,302 returned.
            BadRequest: if HTTP error code 400 returned.
            UnauthorizedAccess: if HTTP error code 401 returned.
            ForbiddenAccess: if HTTP error code 403 returned.
            ResourceNotFound: if HTTP error code 404 is returned.
            MethodNotAllowed: if HTTP error code 405 is returned.
            ResourceConflict: if HTTP error code 409 is returned.
            ResourceInvalid: if HTTP error code 422 is returned.
            ClientError: if HTTP error code falls in 401 - 499.
            ServerError: if HTTP error code falls in 500 - 599.
            ConnectionError: if unknown HTTP error code returned.
        )i-  i.  éÈ   i�  i‘  i“  i”  i•  i™  i¦  iô  iX  )r   r&   r3   r5   r7   r0   r9   r+   r-   r)   r   r   )r   r¤   s     r   r—   zConnection._handle_error„  s(  € ð( �8‰8�zÑ!Ü˜cÓ"Ð"Ø�C—H‘HÔ"˜sÒ"ØˆJð #à�X‰X˜Š_Ü˜S“/Ð!Ø�X‰X˜Š_Ü$ SÓ)Ð)Ø�X‰X˜Š_Ü! #Ó&Ð&Ø�X‰X˜Š_Ü" 3Ó'Ð'Ø�X‰X˜Š_Ü" 3Ó'Ð'Ø�X‰X˜Š_Ü" 3Ó'Ð'Ø�X‰X˜Š_Ü! #Ó&Ð&Ø�C—H‘HÔ"˜sÒ"Ü˜cÓ"Ð"ð #à�C—H‘HÔ"˜sÒ"Ü˜cÓ"Ð"ð #ô " #Ó&Ð&”/ #Ó&Ð&r   r$   r	   )r   r   r   r   r   Ú
JSONFormatr   rj   r„   r¥   r˜   r^   r­   r°   r´   r¶   r¹   r—   r   r   r   rf   rf   ¼   sU   „ Ù>à"&°¸tØ×*Ñ*óò6:ò"ó9òv1ó 	8ó	@ó	;ó
Có
Dó	9ó+'r   rf   )r   rn   rt   r”   rK   r�   Ú	six.movesr   Úpyactiveresourcer   r
   r   r   r   r&   r)   r+   r-   r0   r3   r5   r7   r9   r>   r;   rN   Úobjectr    rf   r   r   r   Ú<module>rÀ      sé   ðñ ;ã Û Û Û 
Û 
Ý Ý $ôˆIô ô!�%ô !ô?�eô ?ô 	�/ô 	ô
	�/ô 	ô	�{ô 	ô	�kô 	ôG�{ô Gô	�ô 	ô	˜ô 	ô	�kô 	ô	�{ô 	ôˆf�n‰n×$Ñ$ô ò$-ô3Cˆvô 3Côls'�õ s'r   