Skip to content

Contacts

Bases: BaseService

The 'Contacts' iCloud service, connects to iCloud and returns contacts.

Source code in pyicloud/services/contacts.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class ContactsService(BaseService):
    """
    The 'Contacts' iCloud service, connects to iCloud and returns contacts.
    """

    def __init__(
        self, service_root: str, session: PyiCloudSession, params: dict[str, Any]
    ) -> None:
        super().__init__(service_root, session, params)
        self._contacts_endpoint: str = f"{self.service_root}/co"
        self._contacts_refresh_url: str = f"{self._contacts_endpoint}/startup"
        self._contacts_next_url: str = f"{self._contacts_endpoint}/contacts"
        self._contacts_changeset_url: str = f"{self._contacts_endpoint}/changeset"
        self._contacts_me_card_url: str = f"{self._contacts_endpoint}/mecard"

        self._contacts: list[dict[str, Any]] | None = None

    def refresh_client(self) -> None:
        """
        Refreshes the ContactsService endpoint, ensuring that the
        contacts data is up-to-date.
        """
        params_contacts: dict[str, Any] = dict(self.params)
        params_contacts.update({
            "locale": "en_US",
            "order": "last,first",
            "includePhoneNumbers": True,
            "includePhotos": True,
        })
        req: Response = self.session.get(
            self._contacts_refresh_url, params=params_contacts
        )
        response: dict[str, Any] = req.json()

        params_next: dict[str, Any] = dict(params_contacts)
        params_next.update({
            "prefToken": response["prefToken"],
            "syncToken": response["syncToken"],
            "limit": "0",
            "offset": "0",
        })
        req = self.session.get(self._contacts_next_url, params=params_next)
        response = req.json()
        self._contacts = response.get("contacts")

    @property
    def all(self) -> list[dict[str, Any]] | None:
        """
        Retrieves all contacts.
        """
        self.refresh_client()
        return self._contacts

    @property
    def me(self) -> "MeCard":
        """
        Retrieves the user's own contact information.
        """
        params_contacts = dict(self.params)
        req: Response = self.session.get(
            self._contacts_me_card_url, params=params_contacts
        )
        response = req.json()
        return MeCard(response)

all property

Retrieves all contacts.

me property

Retrieves the user's own contact information.

refresh_client()

Refreshes the ContactsService endpoint, ensuring that the contacts data is up-to-date.

Source code in pyicloud/services/contacts.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def refresh_client(self) -> None:
    """
    Refreshes the ContactsService endpoint, ensuring that the
    contacts data is up-to-date.
    """
    params_contacts: dict[str, Any] = dict(self.params)
    params_contacts.update({
        "locale": "en_US",
        "order": "last,first",
        "includePhoneNumbers": True,
        "includePhotos": True,
    })
    req: Response = self.session.get(
        self._contacts_refresh_url, params=params_contacts
    )
    response: dict[str, Any] = req.json()

    params_next: dict[str, Any] = dict(params_contacts)
    params_next.update({
        "prefToken": response["prefToken"],
        "syncToken": response["syncToken"],
        "limit": "0",
        "offset": "0",
    })
    req = self.session.get(self._contacts_next_url, params=params_next)
    response = req.json()
    self._contacts = response.get("contacts")