
    =[j                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZ ddlmZmZ dd	lmZmZ d
dlmZmZmZmZmZmZm Z   G d d          Z!dS )z(Photos-specific CloudKit client helpers.    )annotations)Path)Dict)	urlencode)ValidationError)CKLookupResponseCKModifyOperationCKModifyResponseCKQueryObjectCKQueryResponseCKZoneChangesZoneReqCKZoneIDReq)CloudKitApiErrorCloudKitContainerClient)CONTENT_TYPECONTENT_TYPE_TEXT   )PhotosBatchCountFilterPhotosBatchCountQueryPhotosBatchCountRequestPhotosBatchCountRequestBatchPhotosBatchCountResponsePhotosBatchCountStringListValuePhotosUploadResponsec                      e Zd ZdZddd:dZddddd;dZd<dZddd=d"Zdd#d>d'Zd( Z	dd)d?d+Z
d@d.ZdAd2ZedBd5            ZdCd9ZdS )DPhotosCloudKitClientz?Photos container adapter on top of the generic CloudKit client.N)
upload_urlbase_urlstrbase_paramsDict[str, object]r   
str | NonereturnNonec               \    || _         || _        || _        t          |||          | _        d S N)_session_upload_url_base_paramsr   _client)selfr   sessionr    r   s        p/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/pyicloud/services/photos_cloudkit/client.py__init__zPhotosCloudKitClient.__init__(   s2      %'.x+NN    )results_limitcontinuationdesired_keysqueryr   zone_idr   r0   
int | Noner1   r2   list[str] | Noner   c               @    | j                             |||||          S )N)r3   r4   r0   r1   r2   )r*   r3   )r+   r3   r4   r0   r1   r2   s         r-   r3   zPhotosCloudKitClient.query5   s2     |!!'%% " 
 
 	
r/   zone_reqr   c             #  L   K   | j                             |          E d {V  d S )N)r8   )r*   iter_changes)r+   r8   s     r-   r:   z!PhotosCloudKitClient.iter_changesF   s9      
 <,,h,???????????r/   )atomic
operationslist[CKModifyOperation]r;   bool | Noner
   c               <    | j                             |||          S )N)r<   r4   r;   )r*   modify)r+   r<   r4   r;   s       r-   r@   zPhotosCloudKitClient.modifyM   s*     |""!76 # 
 
 	
r/   )r2   record_names	list[str]r   c               <    | j                             |||          S )N)rA   r4   r2   )r*   lookup)r+   rA   r4   r2   s       r-   rD   zPhotosCloudKitClient.lookupX   s,     |""%% # 
 
 	
r/   c                4    | j                                         S r&   )r*   
zones_list)r+   s    r-   rF   zPhotosCloudKitClient.zones_liste   s    |&&(((r/   
sync_tokenrH   c               8    | j                             |          S )NrG   )r*   database_changes)r+   rH   s     r-   rJ   z%PhotosCloudKitClient.database_changesh   s    |,,
,CCCr/   urlbytesc                6    | j                             |          S r&   )r*   download_asset_bytes)r+   rK   s     r-   rN   z)PhotosCloudKitClient.download_asset_bytesk   s    |00555r/   container_iddict[str, str]intc               r   t          t          dt          dt          ddt	          d|g                              d	t          di |
          g                              dd	          }| j        j        	                    d|t          t          i          }	 t          j        |          }n$# t          $ r}t          d|          |d}~ww xY w	 |j        d         j        d         j        j        j        S # t*          $ r+}t          d|                    d                    |d}~ww xY w)z
        Query the Hyperion index count used by Photos albums.

        This remains a Photos-specific raw endpoint because the shared CloudKit
        request models do not yet represent the batched internal count API.
        r   HyperionIndexCountLookupindexCountIDINSTRING_LIST)typevalue)	fieldName
comparator
fieldValue)
recordTypefilterByT)resultsLimitr3   zoneWidezoneID)batchjson)modeexclude_nonez/internal/records/query/batch)headerszPhotos count query failedpayloadNr   )rc    )r   r   r   r   r   r   
model_dumpr*   _httppostr   r   r   model_validater   r   ra   recordsfields	itemCountrX   	Exception)r+   rO   r4   rg   raw_datadataexcs          r-   batch_countz PhotosCloudKitClient.batch_countn   s    *,!"/#=!7&4'+'F%2'3n( ( (" " "
 
 
 "&1111  
 
 
& *&t*
4
4' 	( <%**+!#45 + 
 

	+:8DDDD 	 	 	"+X  		:a=(+2<BB 	 	 	"+T__&_5Q5Q  	s0    B5 5
C?CC&D 
D6&D11D6contextr   c          	        t          | dd          }t          |t                    sd}|dk    rM	 |                                 }n!# t          $ r t          | dd           }Y nw xY wt          | d| |          	 |                                 S # t          $ r)}t          | dt          | dd                     |d }~ww xY w)	Nstatus_coder      i  textz failed with HTTP rf   z returned invalid JSON)getattr
isinstancerQ   rb   rp   r   )responseru   coderg   rs   s        r-   _response_jsonz#PhotosCloudKitClient._response_json   s
   x22$$$ 	D3;;:"--// : : :!(FD99:"44d44g   	==??" 	 	 	"222&$77   	s)   A A#"A#=B 
C$B??Cpathdsidr   c               *   | j         st          d          t          |          }||j        d}| j          dt	          |           }|                    d          5 }| j                            ||| j        j	        j
                  }ddd           n# 1 swxY w Y   t          j        |                     |d                    }|j        rA|j        d	         }	t          |	j        pd
 d|	j        pd                                           |S )u7   Upload a file through Apple’s uploadimagews endpoint.z!Photos uploads are not configured)r   filenamez/upload?rb)rK   rr   timeoutNzPhotos upload)ru   r   UPLOAD_ERRORz:  )r(   r   r   namer   openr'   rk   r*   rj   _REQUEST_TIMEOUTr   rl   r~   errorsr}   messagestrip)
r+   r   r   upload_pathparamsrK   handler|   rr   firsts
             r-   upload_filez PhotosCloudKitClient.upload_file   sp     	H"#FGGG4jjK,<==!>>9V+<+<>>d## 	v}))*; *  H	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 $2/BB
 
 ; 	KNE":/HH5=3FBHHNNPP   s   -BBB)r   r   r    r!   r   r"   r#   r$   )r3   r   r4   r   r0   r5   r1   r"   r2   r6   r#   r   )r8   r   )r<   r=   r4   r   r;   r>   r#   r
   )rA   rB   r4   r   r2   r6   r#   r   )rH   r"   )rK   r   r#   rL   )rO   r   r4   rP   r#   rQ   )ru   r   r#   r   )r   r   r   r   r#   r   )__name__
__module____qualname____doc__r.   r3   r:   r@   rD   rF   rJ   rN   rt   staticmethodr~   r   rh   r/   r-   r   r   %   s`       II "&O O O O O O$ %)#')-
 
 
 
 
 
"@ @ @ @ #	
 	
 	
 	
 	
 	
  *.
 
 
 
 
 
) ) ) <@ D D D D D D6 6 6 6, , , ,\    \(     r/   r   N)"r   
__future__r   pathlibr   typingr   urllib.parser   pydanticr   pyicloud.common.cloudkitr   r	   r
   r   r   r   r   pyicloud.common.cloudkit.clientr   r   pyicloud.constr   r   modelsr   r   r   r   r   r   r   r   rh   r/   r-   <module>r      s   . . " " " " " "             " " " " " " $ $ $ $ $ $                         ; : : : : : : :                 b b b b b b b b b br/   