Instead of hard setting the web service URI in each Service class, re-use the service root URI we receive from the login response.

This commit is contained in:
Peter Evans 2012-12-31 15:27:16 +00:00
parent 93a2d9cd79
commit 5930e27188
3 changed files with 15 additions and 9 deletions

View file

@ -97,11 +97,14 @@ class PyiCloudService(object):
self.refresh_validate() self.refresh_validate()
self.discovery = req.json() self.discovery = req.json()
self.webservices = self.discovery['webservices']
@property @property
def iphone(self): def iphone(self):
return FindMyiPhoneService(self.session, self.params) service_root = self.webservices['findme']['url']
return FindMyiPhoneService(service_root, self.session, self.params)
@property @property
def calendar(self): def calendar(self):
return CalendarService(self.session, self.params) service_root = self.webservices['calendar']['url']
return CalendarService(service_root, self.session, self.params)

View file

@ -8,10 +8,11 @@ class CalendarService(object):
""" """
The 'Calendar' iCloud service, connects to iCloud and returns events. 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.session = session
self.params = params 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 self._calendar_refresh_url = '%s/events' % self._calendar_endpoint
def get_system_tz(self): def get_system_tz(self):
@ -33,7 +34,8 @@ class CalendarService(object):
from_dt = datetime(today.year, today.month, first_day) from_dt = datetime(today.year, today.month, first_day)
if not to_dt: if not to_dt:
to_dt = datetime(today.year, today.month, last_day) 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 = dict(self.params)
params.update({ params.update({
'lang': 'en-us', 'lang': 'en-us',

View file

@ -8,11 +8,11 @@ class FindMyiPhoneService(object):
The 'Find my iPhone' iCloud service, connects to iCloud and returns The 'Find my iPhone' iCloud service, connects to iCloud and returns
phone data including the near-realtime latitude and longitude. 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.session = session
self.params = params self.params = params
self._fmip_root = 'https://p12-fmipweb.icloud.com' self._service_root = service_root
self._fmip_endpoint = '%s/fmipservice/client/web' % self._fmip_root self._fmip_endpoint = '%s/fmipservice/client/web' % self._service_root
self._fmip_refresh_url = '%s/refreshClient' % self._fmip_endpoint self._fmip_refresh_url = '%s/refreshClient' % self._fmip_endpoint
self._fmip_sound_url = '%s/playSound' % self._fmip_endpoint self._fmip_sound_url = '%s/playSound' % self._fmip_endpoint
@ -21,7 +21,8 @@ class FindMyiPhoneService(object):
Refreshes the FindMyiPhoneService endpoint, Refreshes the FindMyiPhoneService endpoint,
ensuring that the location data is up-to-date. 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) req = self.session.post(self._fmip_refresh_url, params=self.params)
self.response = req.json() self.response = req.json()
if self.response['content']: if self.response['content']: