blackened (slightly)

master
Marek Isalski 3 years ago
parent 188c4d46c8
commit 167946385a

@ -28,6 +28,7 @@ ACCOUNTTYPE_RESIDENTIAL = "RESIDENTIAL"
class SipcentricException(Exception): class SipcentricException(Exception):
pass pass
class AuthenticationException(SipcentricException): class AuthenticationException(SipcentricException):
pass pass
@ -48,11 +49,9 @@ class API(object):
if method == "GET": if method == "GET":
if params: if params:
r = requests.get( r = requests.get(url, auth=auth, params=params, verify=True, timeout=3.0)
url, auth=auth, params=params, verify=True, timeout=3.000
)
else: else:
r = requests.get(url, auth=auth, verify=True, timeout=3.000) r = requests.get(url, auth=auth, verify=True, timeout=3.0)
elif method == "POST": elif method == "POST":
headers = {"content-type": "application/json"} headers = {"content-type": "application/json"}
@ -104,20 +103,20 @@ class API(object):
def getMany(self, uri, params=None, startPage=1, pageSize=20): def getMany(self, uri, params=None, startPage=1, pageSize=20):
if params is None: if params is None:
params = {'pageSize': pageSize, 'page': startPage} params = {"pageSize": pageSize, "page": startPage}
else: else:
params['pageSize'] = pageSize params["pageSize"] = pageSize
params['page'] = startPage params["page"] = startPage
resp = self._request(uri, "GET", params=params) resp = self._request(uri, "GET", params=params)
for item in resp.get('items', []): for item in resp.get("items", []):
yield item yield item
nextPage = resp.get('nextPage', None) nextPage = resp.get("nextPage", None)
while nextPage: while nextPage:
resp = self._request(nextPage, "GET") resp = self._request(nextPage, "GET")
for item in resp.get('items', []): for item in resp.get("items", []):
yield item yield item
nextPage = resp.get('nextPage', None) nextPage = resp.get("nextPage", None)
def post(self, uri, data=None, params=None): def post(self, uri, data=None, params=None):
return self._request(uri, method="POST", data=data, params=params) return self._request(uri, method="POST", data=data, params=params)
@ -134,7 +133,7 @@ class APIObject(object):
self._api = api self._api = api
self._id = id self._id = id
if data: if data:
self._id = data.get('id', None) self._id = data.get("id", None)
self._data = data self._data = data
@classmethod @classmethod
@ -153,11 +152,13 @@ class APIObject(object):
def create(self): def create(self):
if self._id: if self._id:
raise ValueError("%s ID %d already created" % (self.__class__.__name__, self._id)) raise ValueError(
"%s ID %d already created" % (self.__class__.__name__, self._id)
)
if not self._data: if not self._data:
raise ValueError("No data associated with record.") raise ValueError("No data associated with record.")
self._data = self._api.post(self.__class__.makeUrl(), data=self._data) self._data = self._api.post(self.__class__.makeUrl(), data=self._data)
self._id = self._data.get('id') self._id = self._data.get("id")
return self._data return self._data
def update(self): def update(self):
@ -166,7 +167,7 @@ class APIObject(object):
if not self._data: if not self._data:
raise ValueError("No data associated with record.") raise ValueError("No data associated with record.")
self._data = self._api.patch(self.makeUrl(), data=self._data) self._data = self._api.patch(self.makeUrl(), data=self._data)
self._id = self._data.get('id') self._id = self._data.get("id")
return self._data return self._data
@classmethod @classmethod
@ -189,11 +190,13 @@ class Account(APIObject):
if parent: if parent:
raise ValueError("Account should not have a parent") raise ValueError("Account should not have a parent")
if id: if id:
raise NotImplementedError('There can be only one (Partner account)') raise NotImplementedError("There can be only one (Partner account)")
return "/" return "/"
def create(self): def create(self):
raise NotImplementedError('You need to sign up as a new partner at https://www.simwood.com/partner/') raise NotImplementedError(
"You need to sign up as a new partner at https://www.simwood.com/partner/"
)
def customers(self): def customers(self):
for c in self._api.getMany(Customer.makeUrl()): for c in self._api.getMany(Customer.makeUrl()):

Loading…
Cancel
Save