Coverage for icloudpy/services/contacts.py: 18%

22 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-04-12 14:26 +0000

1"""Contacts service.""" 

2 

3 

4class ContactsService: 

5 """ 

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

7 """ 

8 

9 def __init__(self, service_root, session, params): 

10 self.session = session 

11 self.params = params 

12 self._service_root = service_root 

13 self._contacts_endpoint = f"{self._service_root}/co" 

14 self._contacts_refresh_url = f"{self._contacts_endpoint}/startup" 

15 self._contacts_next_url = f"{self._contacts_endpoint}/contacts" 

16 self._contacts_changeset_url = f"{self._contacts_endpoint}/changeset" 

17 

18 self.response = {} 

19 

20 def refresh_client(self): 

21 """ 

22 Refreshes the ContactsService endpoint, ensuring that the 

23 contacts data is up-to-date. 

24 """ 

25 params_contacts = dict(self.params) 

26 params_contacts.update( 

27 { 

28 "clientVersion": "2.1", 

29 "locale": "en_US", 

30 "order": "last,first", 

31 } 

32 ) 

33 req = self.session.get(self._contacts_refresh_url, params=params_contacts) 

34 self.response = req.json() 

35 

36 params_next = dict(params_contacts) 

37 params_next.update( 

38 { 

39 "prefToken": self.response["prefToken"], 

40 "syncToken": self.response["syncToken"], 

41 "limit": "0", 

42 "offset": "0", 

43 } 

44 ) 

45 req = self.session.get(self._contacts_next_url, params=params_next) 

46 self.response = req.json() 

47 

48 def all(self): 

49 """ 

50 Retrieves all contacts. 

51 """ 

52 self.refresh_client() 

53 return self.response.get("contacts")