From 5930e27188df4f5dbb7918bb5e3051e999165e70 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Mon, 31 Dec 2012 15:27:16 +0000 Subject: [PATCH] Instead of hard setting the web service URI in each Service class, re-use the service root URI we receive from the login response. --- base.py | 7 +++++-- services/calendar.py | 8 +++++--- services/findmyiphone.py | 9 +++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/base.py b/base.py index 612bbc0..10de085 100644 --- a/base.py +++ b/base.py @@ -97,11 +97,14 @@ class PyiCloudService(object): self.refresh_validate() self.discovery = req.json() + self.webservices = self.discovery['webservices'] @property def iphone(self): - return FindMyiPhoneService(self.session, self.params) + service_root = self.webservices['findme']['url'] + return FindMyiPhoneService(service_root, self.session, self.params) @property def calendar(self): - return CalendarService(self.session, self.params) + service_root = self.webservices['calendar']['url'] + return CalendarService(service_root, self.session, self.params) diff --git a/services/calendar.py b/services/calendar.py index b7e893b..21e29b2 100644 --- a/services/calendar.py +++ b/services/calendar.py @@ -8,10 +8,11 @@ class CalendarService(object): """ The 'Calendar' iCloud service, connects to iCloud and returns events. """ - def __init__(self, session, params): + def __init__(self, service_root, session, params): self.session = session self.params = params - self._calendar_endpoint = 'https://p12-calendarws.icloud.com/ca' + self._service_root = service_root + self._calendar_endpoint = '%s/ca' % self._service_root self._calendar_refresh_url = '%s/events' % self._calendar_endpoint def get_system_tz(self): @@ -33,7 +34,8 @@ class CalendarService(object): from_dt = datetime(today.year, today.month, first_day) if not to_dt: to_dt = datetime(today.year, today.month, last_day) - self.session.headers.update({'host': 'p12-calendarws.icloud.com'}) + host = self._service_root.split('//')[1].split(':')[0] + self.session.headers.update({'host': host}) params = dict(self.params) params.update({ 'lang': 'en-us', diff --git a/services/findmyiphone.py b/services/findmyiphone.py index 759f771..3be69ff 100644 --- a/services/findmyiphone.py +++ b/services/findmyiphone.py @@ -8,11 +8,11 @@ class FindMyiPhoneService(object): The 'Find my iPhone' iCloud service, connects to iCloud and returns phone data including the near-realtime latitude and longitude. """ - def __init__(self, session, params): + def __init__(self, service_root, session, params): self.session = session self.params = params - self._fmip_root = 'https://p12-fmipweb.icloud.com' - self._fmip_endpoint = '%s/fmipservice/client/web' % self._fmip_root + self._service_root = service_root + self._fmip_endpoint = '%s/fmipservice/client/web' % self._service_root self._fmip_refresh_url = '%s/refreshClient' % self._fmip_endpoint self._fmip_sound_url = '%s/playSound' % self._fmip_endpoint @@ -21,7 +21,8 @@ class FindMyiPhoneService(object): Refreshes the FindMyiPhoneService endpoint, ensuring that the location data is up-to-date. """ - self.session.headers.update({'host': 'p12-fmipweb.icloud.com'}) + host = self._service_root.split('//')[1].split(':')[0] + self.session.headers.update({'host': host}) req = self.session.post(self._fmip_refresh_url, params=self.params) self.response = req.json() if self.response['content']: