Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@classmethod
def _make_request(cls, method, url, connection, data=None, params={}, headers={}):
return connection.make_request(method, url, data, params, headers)
@classmethod
def _get_path(cls, id):
return "%s/%s" % (cls.resource_name, id)
@classmethod
def get(cls, id, connection=None, **params):
response = cls._make_request('GET', cls._get_path(id), connection, params=params)
return cls._create_object(response, connection=connection)
class ApiSubResource(ApiResource):
parent_resource = ""
parent_key = ""
@classmethod
def _get_path(cls, id, parentid):
return "%s/%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name, id)
@classmethod
def get(cls, parentid, id, connection=None, **params):
response = cls._make_request('GET', cls._get_path(id, parentid), connection, params=params)
return cls._create_object(response, connection=connection)
def parent_id(self):
return self[self.parent_key]
@classmethod
def delete_all(cls, connection=None):
return cls._make_request('DELETE', cls._delete_all_path(), connection)
class CollectionDeleteableApiSubResource(ApiSubResource):
@classmethod
def _delete_all_path(cls, parentid):
return "%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name)
@classmethod
def delete_all(cls, parentid, connection=None):
return cls._make_request('DELETE', cls._delete_all_path(parentid), connection)
class CountableApiResource(ApiResource):
@classmethod
def _count_path(cls):
return "%s/count" % (cls.resource_name)
@classmethod
def count(cls, connection=None, **params):
response = cls._make_request('GET', cls._count_path(), connection, params=params)
return response['count']
class CountableApiSubResource(ApiSubResource):
# Account for the fairly common case where the count path doesn't include the parent id
count_resource = None
@classmethod
def _count_path(cls, parentid=None):
class ListableApiSubResource(ApiSubResource):
@classmethod
def _get_all_path(cls, parentid=None):
# Not all sub resources require a parent id. Eg: /api/v2/products/skus?sku=
if (parentid):
return "%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name)
else:
return "%s/%s" % (cls.parent_resource, cls.resource_name)
@classmethod
def all(cls, parentid=None, connection=None, **params):
response = cls._make_request('GET', cls._get_all_path(parentid), connection, params=params)
return cls._create_object(response, connection=connection)
class UpdateableApiResource(ApiResource):
def _update_path(self):
return "%s/%s" % (self.resource_name, self.id)
def update(self, **updates):
response = self._make_request('PUT', self._update_path(), self._connection, data=updates)
return self._create_object(response, connection=self._connection)
class UpdateableApiSubResource(ApiSubResource):
def _update_path(self):
return "%s/%s/%s/%s" % (self.parent_resource, self.parent_id(), self.resource_name, self.id)
def update(self, **updates):
response = self._make_request('PUT', self._update_path(), self._connection, data=updates)
return self._create_object(response, connection=self._connection)
response = cls._make_request('POST', cls._create_path(), connection, data=params)
return cls._create_object(response, connection=connection)
class CreateableApiSubResource(ApiSubResource):
@classmethod
def _create_path(cls, parentid):
return "%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name)
@classmethod
def create(cls, parentid, connection=None, **params):
response = cls._make_request('POST', cls._create_path(parentid), connection, data=params)
return cls._create_object(response, connection=connection)
class ListableApiResource(ApiResource):
@classmethod
def _get_all_path(cls):
return cls.resource_name
@classmethod
def all(cls, connection=None, **params):
request = cls._make_request('GET', cls._get_all_path(), connection, params=params)
return cls._create_object(request, connection=connection)
class ListableApiSubResource(ApiSubResource):
@classmethod
def _get_all_path(cls, parentid=None):
# Not all sub resources require a parent id. Eg: /api/v2/products/skus?sku=
if (parentid):
return "%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name)
parent_key = ""
@classmethod
def _get_path(cls, id, parentid):
return "%s/%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name, id)
@classmethod
def get(cls, parentid, id, connection=None, **params):
response = cls._make_request('GET', cls._get_path(id, parentid), connection, params=params)
return cls._create_object(response, connection=connection)
def parent_id(self):
return self[self.parent_key]
class CreateableApiResource(ApiResource):
@classmethod
def _create_path(cls):
return cls.resource_name
@classmethod
def create(cls, connection=None, **params):
response = cls._make_request('POST', cls._create_path(), connection, data=params)
return cls._create_object(response, connection=connection)
class CreateableApiSubResource(ApiSubResource):
@classmethod
def _create_path(cls, parentid):
return "%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name)
@classmethod
def _delete_path(self):
return "%s/%s" % (self.resource_name, self.id)
def delete(self):
return self._make_request('DELETE', self._delete_path(), self._connection)
class DeleteableApiSubResource(ApiSubResource):
def _delete_path(self):
return "%s/%s/%s/%s" % (self.parent_resource, self.parent_id(), self.resource_name, self.id)
def delete(self):
return self._make_request('DELETE', self._delete_path(), self._connection)
class CollectionDeleteableApiResource(ApiResource):
@classmethod
def _delete_all_path(cls):
return cls.resource_name
@classmethod
def delete_all(cls, connection=None):
return cls._make_request('DELETE', cls._delete_all_path(), connection)
class CollectionDeleteableApiSubResource(ApiSubResource):
@classmethod
def _delete_all_path(cls, parentid):
return "%s/%s/%s" % (cls.parent_resource, parentid, cls.resource_name)
@classmethod
def delete_all(cls, parentid, connection=None):
def update(self, **updates):
response = self._make_request('PUT', self._update_path(), self._connection, data=updates)
return self._create_object(response, connection=self._connection)
class UpdateableApiSubResource(ApiSubResource):
def _update_path(self):
return "%s/%s/%s/%s" % (self.parent_resource, self.parent_id(), self.resource_name, self.id)
def update(self, **updates):
response = self._make_request('PUT', self._update_path(), self._connection, data=updates)
return self._create_object(response, connection=self._connection)
class DeleteableApiResource(ApiResource):
def _delete_path(self):
return "%s/%s" % (self.resource_name, self.id)
def delete(self):
return self._make_request('DELETE', self._delete_path(), self._connection)
class DeleteableApiSubResource(ApiSubResource):
def _delete_path(self):
return "%s/%s/%s/%s" % (self.parent_resource, self.parent_id(), self.resource_name, self.id)
def delete(self):
return self._make_request('DELETE', self._delete_path(), self._connection)
class CollectionDeleteableApiResource(ApiResource):