o
    l>ji                     @   sZ   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 G dd deZ
G dd	 d	Zd
S )zContacts service.    )AnyOptional)Response)BaseService)PyiCloudSessionc                       st   e Zd ZdZdededeeef ddf fddZdd	d
Z	e
deeeef  dB fddZe
dddZ  ZS )ContactsServicezQ
    The 'Contacts' iCloud service, connects to iCloud and returns contacts.
    service_rootsessionparamsreturnNc                    s`   t  ||| | j d| _| j d| _| j d| _| j d| _| j d| _d | _d S )Nz/coz/startupz	/contactsz
/changesetz/mecard)	super__init__r   _contacts_endpoint_contacts_refresh_url_contacts_next_url_contacts_changeset_url_contacts_me_card_url	_contacts)selfr   r	   r
   	__class__ O/home/thesage/.local/lib/python3.10/site-packages/pyicloud/services/contacts.pyr      s   
zContactsService.__init__c                 C   s   t | j}|ddddd | jj| j|d}| }t |}||d |d ddd	 | jj| j|d}| }|d
| _dS )zp
        Refreshes the ContactsService endpoint, ensuring that the
        contacts data is up-to-date.
        en_USz
last,firstT)localeorderincludePhoneNumbersincludePhotosr
   	prefToken	syncToken0)r   r    limitoffsetcontactsN)	dictr
   updater	   getr   jsonr   r   )r   params_contactsreqresponseparams_nextr   r   r   refresh_client   s.   
zContactsService.refresh_clientc                 C   s   |    | jS )z)
        Retrieves all contacts.
        )r-   r   r   r   r   r   all<   s   zContactsService.allMeCardc                 C   s,   t | j}| jj| j|d}| }t|S )z?
        Retrieves the user's own contact information.
        r   )r%   r
   r	   r'   r   r(   r0   )r   r)   r*   r+   r   r   r   meD   s   
zContactsService.me)r   N)r   r0   )__name__
__module____qualname____doc__strr   r%   r   r   r-   propertylistr/   r1   __classcell__r   r   r   r   r      s     

  r   c                   @   s   e Zd ZdZdeeef ddfddZedefddZ	edefd	d
Z
edd ZdefddZdefddZedeeef fddZdS )r0   zK
    The 'MeCard' class represents the user's own contact information.
    datar   Nc                 C   s>   || _ |d}t|trt|d tr|d | _d S td)Nr$   r   zcontacts not found in data)_datar'   
isinstancer8   r%   _contactKeyError)r   r:   r$   r   r   r   r   V   s
   
zMeCard.__init__c                 C   
   | j d S )z(
        The user's first name.
        	firstNamer=   r.   r   r   r   
first_name^      
zMeCard.first_namec                 C   r?   )z'
        The user's last name.
        lastNamerA   r.   r   r   r   	last_namee   rC   zMeCard.last_namec                 C   r?   )z#
        The user's photo.
        photorA   r.   r   r   r   rF   l   rC   zMeCard.photoc                 C   s   | j  d| j S )N rB   rE   r.   r   r   r   __str__s   s   zMeCard.__str__c                 C   s   d| j  d| j dS )Nz<MeCard(-z)>rH   r.   r   r   r   __repr__v   s   zMeCard.__repr__c                 C   s   | j S )z-
        The raw data of the mecard.
        )r;   r.   r   r   r   raw_datay   s   zMeCard.raw_data)r2   r3   r4   r5   r%   r6   r   r   r7   rB   rE   rF   rI   rK   rL   r   r   r   r   r0   Q   s    
r0   N)r5   typingr   r   requestsr   pyicloud.services.baser   pyicloud.sessionr   r   r0   r   r   r   r   <module>   s    F