def update(self, attributes=None): attributes = attributes or self.to_dict() url = util.join_url(self.path, str(self['id'])) new_attributes = self.api.patch(url, attributes, self.http_headers()) self.error = None self.merge(new_attributes) return self.success()
def test_join_url(self, one, two, three, expected): parts = [one, two] if three: parts.append(three) url = util.join_url(*parts) assert url == expected
def patch(self, action, params=None, headers=None): """Make PATCH request Usage:: >>> api.patch("api/1/customers/1", {'name': 'Andrew Wiggins'}) """ return self.request(util.join_url(self.endpoint, action), 'PATCH', body=params or {}, headers=headers or {})
def post(self, action, params=None, headers=None): """Make POST request Usage:: >>> api.post("api/1/customers", {'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference: C1'}) """ return self.request(util.join_url(self.endpoint, action), 'POST', body=params or {}, headers=headers or {})
def get(self, action, headers=None): """Make GET request Usage:: >>> api.get("api/1/customers") >>> api.get("api/1/customers/1") """ return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers or {})
def find(cls, resource_id, api=None): """Locate resource e.g. customer with given id Usage:: >>> payment = Customer.find("1") """ api = api or default_api() url = util.join_url(cls.path, str(resource_id)) return cls(api.get(url), api=api)
def list_bank_accounts(self): # /customers/<CUSTOMER-ID>/bank_accounts endpoint = util.join_url(self.path, str(self['id']), 'bank_accounts') response = self.api.get(endpoint) try: return Resource(response, api=self.api) except AttributeError: # To handle the case when response is JSON Array if isinstance(response, list): new_resp = [Resource(elem, api=self.api) for elem in response] return new_resp
def delete(self): """Deletes a resource e.g. bank_account Usage:: >>> bank_account.delete() """ url = util.join_url(self.path, str(self['id'])) new_attributes = self.api.delete(url) self.error = None self.merge(new_attributes) return self.success()
def post(self, name, attributes=None, cls=Resource, fieldname='id'): """Constructs url with passed in headers and makes post request via post method in api class. Usage:: >>> client.post("stats", {'id': '1234'}, client) # return True or False """ attributes = attributes or {} url = util.join_url(self.path, str(self[fieldname]), name) if not isinstance(attributes, Resource): attributes = Resource(attributes, api=self.api) new_attributes = self.api.post(url, attributes.to_dict(), attributes.http_headers()) if isinstance(cls, Resource): cls.error = None cls.merge(new_attributes) return self.success() else: return cls(new_attributes, api=self.api)
def delete(self, action, headers=None): """Make DELETE request """ return self.request(util.join_url(self.endpoint, action), 'DELETE', headers=headers or {})