Ë
    äš¾j%  ã                   óH  — d dl m Z mZ d dl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mZmZmZmZ d dlZd dlZd dlZd dlmZmZ d dlmZ ddlmZ  ed	«       G d
„ de«      «       Zej8                  e_         ed«       G d„ de«      «       Zej8                  e_        y)é    )ÚdatetimeÚtimezoneN)ÚPath)ÚIterableÚOptionalÚTupleÚUnionÚCallable)ÚClientÚregister_client_class)ÚFileCacheModeé   )ÚHttpPathÚhttpc                   óv  ‡ — e Zd Zddej                  ddddfdeeeef      deeee	j                  f      dee   deej                  j                     deeegee   f      deeegef      d	ee   fˆ fd
„Zdedefd„Zdedee   fd„Zdedeee	j                  f   defd„Zdedefd„Zd$dedededefd„Zd$dededdfd„Zdededeeeef      fd„Zdeee	j                  f   dedefd„Zdedefd„Zd%dededefd„Z dededeeeef      fd „Z!d!ed"edee"jF                  jH                  e%f   fd#„Zˆ xZ&S )&Ú
HttpClientNÚPUTÚfile_cache_modeÚlocal_cache_dirÚcontent_type_methodÚauthÚcustom_list_page_parserÚcustom_dir_matcherÚwrite_file_http_methodc                 ó&  •— t         ‰| �  |||«       || _        | j                  €$t        j                  j                  «       | _        n.t        j                  j                  | j                  «      | _        || _        |�|nd„ | _        || _	        y)a  Class constructor. Creates an HTTP client that can be used to interact with HTTP servers
            using the cloudpathlib library.

        Args:
            file_cache_mode (Optional[Union[str, FileCacheMode]]): How often to clear the file cache; see
                [the caching docs](https://cloudpathlib.drivendata.org/stable/caching/) for more information
                about the options in cloudpathlib.eums.FileCacheMode.
            local_cache_dir (Optional[Union[str, os.PathLike]]): Path to directory to use as cache
                for downloaded files. If None, will use a temporary directory. Default can be set with
                the `CLOUDPATHLIB_LOCAL_CACHE_DIR` environment variable.
            content_type_method (Optional[Callable]): Function to call to guess media type (mimetype) when
                uploading files. Defaults to `mimetypes.guess_type`.
            auth (Optional[urllib.request.BaseHandler]): Authentication handler to use for the client. Defaults to None, which will use the default handler.
            custom_list_page_parser (Optional[Callable[[str], Iterable[str]]]): Function to call to parse pages that list directories. Defaults to looking for `<a>` tags with `href`.
            custom_dir_matcher (Optional[Callable[[str], bool]]): Function to call to identify a url that is a directory. Defaults to a lambda that checks if the path ends with a `/`.
            write_file_http_method (Optional[str]): HTTP method to use when writing files. Defaults to "PUT", but some servers may want "POST".
        Nc                 ó$   — | j                  d«      S )Nú/)Úendswith)Úxs    úf/home/spazio/kingdom_calls/work5/transenv/lib/python3.12/site-packages/cloudpathlib/http/httpclient.pyú<lambda>z%HttpClient.__init__.<locals>.<lambda><   s   € ÐPQ×PZÑPZÐ[^ÓP_€ ó    )
ÚsuperÚ__init__r   ÚurllibÚrequestÚbuild_openerÚopenerr   Údir_matcherr   )	Úselfr   r   r   r   r   r   r   Ú	__class__s	           €r    r$   zHttpClient.__init__   s~   ø€ ô6 	‰Ñ˜¨/Ð;NÔOØˆŒ	à�9‰9ÐÜ Ÿ.™.×5Ñ5Ó7ˆD�Kä Ÿ.™.×5Ñ5°d·i±iÓ@ˆDŒKà'>ˆÔ$ð #5Ð"@ÑÑF_ð 	Ôð '=ˆÕ#r"   Ú
cloud_pathÚreturnc                 ó®  — | j                   j                  |j                  «       «      5 }|j                  j	                  dd «      }|�6t        j                  |d«      }|j                  t        j                  ¬«      }t        |j                  j	                  dd«      «      ||j                  j	                  dd «      dœcd d d «       S # 1 sw Y   y xY w)NzLast-Modifiedz%a, %d %b %Y %H:%M:%S %Z)ÚtzinfozContent-Lengthr   úContent-Type)ÚsizeÚlast_modifiedÚcontent_type)r(   ÚopenÚas_urlÚheadersÚgetr   ÚstrptimeÚreplacer   ÚutcÚint)r*   r,   Úresponser2   s       r    Ú_get_metadatazHttpClient._get_metadataA   sº   € Ø�[‰[×Ñ˜j×/Ñ/Ó1Ó2ð 	°hØ$×,Ñ,×0Ñ0°À$ÓGˆMàÐ(ä (× 1Ñ 1°-ÐA[Ó \�ð !.× 5Ñ 5¼X¿\¹\Ð 5Ó J�ô ˜H×,Ñ,×0Ñ0Ð1AÀ1ÓEÓFØ!.Ø (× 0Ñ 0× 4Ñ 4°^ÀTÓ Jñ÷	÷ 	ò 	ús   ªBCÃCc                 óD   — | j                  |j                  «       «      ryy)NÚdirÚfile)r)   r5   ©r*   r,   s     r    Ú_is_file_or_dirzHttpClient._is_file_or_dirR   s   € Ø×Ñ˜J×-Ñ-Ó/Ô0Øàr"   Ú
local_pathc                 óL  — t        |«      }| j                  j                  |j                  «       «      5 }|j                  j                  dd¬«       |j                  d«      5 }t        j                  ||«       d d d «       d d d «       |S # 1 sw Y   ŒxY w# 1 sw Y   |S xY w)NT)ÚparentsÚexist_okÚwb)r   r(   r4   r5   ÚparentÚmkdirÚshutilÚcopyfileobj)r*   r,   rC   r<   Úout_files        r    Ú_download_filezHttpClient._download_fileX   s•   € Ü˜*Ó%ˆ
Ø�[‰[×Ñ˜j×/Ñ/Ó1Ó2ð 	7°hà×Ñ×#Ñ#¨D¸4Ð#Ô@Ø—‘ Ó&ð 7¨(Ü×"Ñ" 8¨XÔ6÷7÷	7ð
 Ð÷7ð 7ú÷	7ð
 Ðús#   µ/BÁ$BÁ;BÂB	ÂBÂB#c                 óÒ  — t         j                  j                  |j                  «       d¬«      }	 | j                  j                  |«      5 }|j                  dk(  cd d d «       S # 1 sw Y   y xY w# t         j                  j                  t         j                  j                  f$ r>}t        |t         j                  j                  «      s|j                  dk(  rY d }~y‚ d }~ww xY w)NÚHEAD©ÚmethodéÈ   é”  F)r%   r&   ÚRequestr5   r(   r4   ÚstatusÚerrorÚ	HTTPErrorÚURLErrorÚ
isinstanceÚcode)r*   r,   r&   r<   Úes        r    Ú_existszHttpClient._existsa   sª   € Ü—.‘.×(Ñ(¨×):Ñ):Ó)<ÀVÐ(ÓLˆð	Ø—‘×!Ñ! 'Ó*ð .¨hØ—‘¨#Ñ-÷.÷ .ò .ûä—‘×&Ñ&¬¯©×(=Ñ(=Ð>ò 	Ü˜!œVŸ\™\×2Ñ2Ô3°q·v±vÀ²}ÜØûð	ús;   ±A1 ÁA%Á	A1 Á%A.Á*A1 Á.A1 Á17C&Â(3C!Ã C!Ã!C&ÚsrcÚdstÚ
remove_srcc           	      óÔ   — | j                  |j                  |«       |r	 | j                  |«       |S |S # t        $ r)}t	        j
                  d|› d|› d|› �t        «       ‚ d }~ww xY w)Nz"File was successfully uploaded to z but failed to remove original z: )Ú_upload_fileÚfspathÚ_removeÚ	ExceptionÚwarningsÚwarnÚUserWarning)r*   r]   r^   r_   r[   s        r    Ú
_move_filezHttpClient._move_filek   sw   € à×Ñ˜#Ÿ*™* cÔ*ÙðØ—‘˜SÔ!ð ˆ
ˆsˆ
øô ò Ü—‘Ø8¸¸Ð=\Ð]`Ð\aÐacÐdeÐcfÐgÜôð ûðús    5 µ	A'¾$A"Á"A'Ú
missing_okc                 ó˜  — t         j                  j                  |j                  «       d¬«      }	 | j                  j                  |«      5 }|j                  dk7  rt        d|› d�«      ‚	 d d d «       y # 1 sw Y   y xY w# t         j                  j                  $ r+}|j                  dk(  r|rnt        d|› d�«      ‚Y d }~y d }~ww xY w)NÚDELETErP   éÌ   zFailed to delete ú.rS   )r%   r&   rT   r5   r(   r4   rU   rd   rV   rW   rZ   ÚFileNotFoundError)r*   r,   ri   r&   r<   r[   s         r    rc   zHttpClient._removey   sÄ   € Ü—.‘.×(Ñ(¨×):Ñ):Ó)<ÀXÐ(ÓNˆð	KØ—‘×!Ñ! 'Ó*ð G¨hØ—?‘? cÒ)Ü#Ð&7¸
°|À1Ð$EÓFÐFð *÷G÷ Gñ Gûô �|‰|×%Ñ%ò 	KØ�v‰v˜Š}¡Øä'Ð*;¸J¸<ÀqÐ(IÓJÐJô ûð	Kús5   ±B ÁA5Á,B Á5A>Á:B Á>B ÂC	Â!CÃC	Ú	recursivec              #   óœ  K  — 	 | j                   j                  |j                  «       «      5 }| j                  |j	                  «       j                  «       t        |«      ¬«      D ],  \  }}||f–— |sŒ|sŒ| j                  |d¬«      E d {  –—†  Œ. 	 d d d «       y 7 Œ# 1 sw Y   y xY w# t        $ r}t        d|› �«      ‚d }~ww xY w­w)N)Úbase_urlT)ro   z{Unable to parse response as a listing of files; please provide a custom parser as `custom_list_page_parser`. Error raised: )
r(   r4   r5   Ú_parse_list_dir_responseÚreadÚdecodeÚstrÚ	_list_dirrd   ÚNotImplementedError)r*   r,   ro   r<   ÚpathÚis_dirr[   s          r    rv   zHttpClient._list_dir…   sì   è ø€ ð	Ø—‘×!Ñ! *×"3Ñ"3Ó"5Ó6ð 	H¸(à$(×$AÑ$AØ—M‘M“O×*Ñ*Ó,´s¸:³ð %Bó %ò H‘L�D˜&ð  ˜,Ò&ò !¢VØ#'§>¡>°$À$ >Ó#G×GÑGñH÷	Hð 	Hð Hø÷	Hð 	Hûô ò 	Ü%ð Nð  OPð  NQð  Róð ûð	üsj   ‚C„)B- ­AB!Á5B!Á8B!ÂBÂB!ÂB- ÂCÂB!Â!B*Â&B- Â)CÂ*B- Â-	C	Â6CÃC	Ã	Cc           	      ó  — t        |«      }| j                  �| j                  |«      \  }}dxs di}|j                  d«      5 }t        j                  j                  |j                  «       |j                  «       | j                  |¬«      }| j                  j                  |«      5 }|j                  dk7  r!|j                  dk7  rt        d|› d|› d	�«      ‚d d d «       d d d «       |S # 1 sw Y   ŒxY w# 1 sw Y   |S xY w)
Nr0   zapplication/octet-streamÚrb)ÚdatarQ   r6   éÉ   rR   zFailed to upload z to rm   )r   r   r4   r%   r&   rT   r5   rs   r   r(   rU   rd   )	r*   rC   r,   r3   Ú_r6   Ú	file_datar&   r<   s	            r    ra   zHttpClient._upload_file—   s
  € Ü˜*Ó%ˆ
Ø×#Ñ#Ð/Ø"×6Ñ6°zÓB‰OˆL˜!à! <Ò#MÐ3MÐNˆà�_‰_˜TÓ"ð 		W iÜ—n‘n×,Ñ,Ø×!Ñ!Ó#Ø—^‘^Ó%Ø×2Ñ2Øð	 -ó ˆGð —‘×!Ñ! 'Ó*ð W¨hØ—?‘? cÒ)¨h¯o©oÀÒ.DÜ#Ð&7¸
°|À4È
À|ÐSTÐ$UÓVÐV÷W÷		Wð Ð÷Wð Wú÷		Wð Ðús%   ÁA%C9Â*1C-ÃC9Ã-C6	Ã2C9Ã9Dc                 ó"   — |j                  «       S ©N)r5   rA   s     r    Ú_get_public_urlzHttpClient._get_public_urlª   s   € Ø× Ñ Ó"Ð"r"   Úexpire_secondsc                 ó   — t        d«      ‚)Nz.Presigned URLs are not supported using urllib.)rw   )r*   r,   rƒ   s      r    Ú_generate_presigned_urlz"HttpClient._generate_presigned_url­   s   € Ü!Ð"RÓSÐSr"   r<   rq   c              #   óÖ   ‡ ‡K  — ‰j                  d«      s‰dz  Šdt        dt        t           fd„}‰ j                  �‰ j                  n|}ˆˆ fd„ ||«      D «       E d {  –—†  y 7 Œ­w)Nr   Úhtmlr-   c                 ó.   — t        j                  d| «      S )Nz<a\s+href="([^"]+)")ÚreÚfindall)r‡   s    r    Ú_simple_linksz:HttpClient._parse_list_dir_response.<locals>._simple_links·   s   € Ü—:‘:Ð4°dÓ;Ð;r"   c              3   óž   •K  — | ]D  }‰j                  t        j                  j                  ‰|«      «      ‰j	                  |«      f–— ŒF y ­wr�   )Ú	CloudPathr%   ÚparseÚurljoinr)   )Ú.0Úmatchrq   r*   s     €€r    ú	<genexpr>z6HttpClient._parse_list_dir_response.<locals>.<genexpr>À   sD   øè ø€ ò 
àð �^‰^œVŸ\™\×1Ñ1°(¸EÓBÓDÀd×FVÑFVÐW\ÓF]Ô^ñ
ùs   ƒA
A)r   ru   r   r   )r*   r<   rq   r‹   Úparsers   ` `  r    rr   z#HttpClient._parse_list_dir_response°   st   ùè ø€ ð × Ñ  Ô%Ø˜‰OˆHð	<¤ð 	<¬´©ó 	<ð
 ×+Ñ+Ð7ð ×(Ò(àð 	ô
á Ó)ô
÷ 	
ò 	
ús   „AA)Á!A'Á"A)ÚurlrQ   c                 óè   — t        j                  j                  |j                  «       fd|i|¤Ž}| j                  j                  |«      5 }||j                  «       fcd d d «       S # 1 sw Y   y xY w)NrQ   )r%   r&   rT   r5   r(   r4   rs   )r*   r”   rQ   Úkwargsr&   r<   s         r    r&   zHttpClient.requestÅ   sa   € ô —.‘.×(Ñ(¨¯©«ÑO¸fÐOÈÑOˆØ�[‰[×Ñ˜gÓ&ð 	-¨(ð ˜XŸ]™]›_Ð,÷	-÷ 	-ò 	-ús   ÁA(Á(A1)T)i  )'Ú__name__Ú
__module__Ú__qualname__Ú	mimetypesÚ
guess_typer   r	   ru   r   ÚosÚPathLiker
   r%   r&   ÚBaseHandlerr   Úboolr$   r   Údictr=   rB   r   rM   r\   rh   rc   r   rv   ra   r‚   r;   r…   rr   r   ÚclientÚHTTPResponseÚbytesÚ__classcell__)r+   s   @r    r   r      sS  ø„ ð @DØ=AØ2;×2FÑ2FØ59ØLPØ>BØ05ñ)=à! %¨¨]Ð(:Ñ";Ñ<ð)=ð " %¨¨R¯[©[Ð(8Ñ"9Ñ:ð)=ð & hÑ/ð	)=ð
 �v—~‘~×1Ñ1Ñ2ð)=ð "*¨(°C°5¸(À3¹-Ð3GÑ*HÑ!Ið)=ð % X¨s¨e°T¨kÑ%:Ñ;ð)=ð !)¨¡õ)=ðV¨ð °Tó ð"¨(ð °xÀ±}ó ð¨ð ¸uÀSÈ"Ï+É+ÐEUÑ?Vð Ð[_ó ð (ð ¨tó ñ˜hð ¨Xð À4ð ÐS[ó ñ
K (ð 
K¸ð 
KÈó 
Kð Hð ¸ð À(È5ÐQYÐ[_ÐQ_ÑK`ÑBaó ð$ u¨S°"·+±+Ð-=Ñ'>ð ÈHð ÐYaó ð&#¨(ð #°só #ñT°(ð TÈCð TÐ^aó Tð
Øð
Ø'*ð
à	�%˜ $˜Ñ'Ñ	(ó
ð*-Øð-Ø%(ð-à	ˆt�{‰{×'Ñ'¨Ð.Ñ	/÷-r"   r   Úhttpsc                   ó   — e Zd Zy)ÚHttpsClientN)r—   r˜   r™   © r"   r    r§   r§   Ò   s   „ àr"   r§   )r   r   r   rœ   r‰   Úurllib.requestr%   Úurllib.parseÚurllib.errorÚpathlibr   Útypingr   r   r   r	   r
   rJ   rš   re   Úcloudpathlib.clientr   r   Úcloudpathlib.enumsr   Úhttppathr   r   r�   r§   Ú	HttpsPathr¨   r"   r    ú<module>r²      s™   ðß 'Û Û 	Û 	Û Û Û Ý ß =Õ =Û Û Û ç =Ý ,å ñ �vÓôw-�ó w-ó ðw-ðt !×*Ñ*€
Ô ñ �wÓô	�*ó 	ó  ð	ð $×-Ñ-€Õ r"   