Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_rates(self):
requestor = Requestor(self._api_key)
url = "%s/%s" % (self.instance_url(), "rates")
response, api_key = requestor.request('get', url)
self.refresh_from(response, api_key)
return self
def buy(self, **params):
requestor = Requestor(self._api_key)
url = "%s/%s" % (self.instance_url(), "buy")
response, api_key = requestor.request('post', url, params)
self.refresh_from(response, api_key)
return self
class PickupRate(Resource):
pass
class Event(Resource):
@classmethod
def receive(self, values):
return convert_to_easypost_object(json.loads(values), api_key)
class CarrierAccount(AllResource, CreateResource, UpdateResource, DeleteResource):
@classmethod
def types(cls, api_key=None):
requestor = Requestor(api_key)
response, api_key = requestor.request('get', "/carrier_types")
return convert_to_easypost_object(response, api_key)
url = cls.class_url()
response, api_key = requestor.request('get', url, params)
return convert_to_easypost_object(response, api_key)
class CreateResource(Resource):
@classmethod
def create(cls, api_key=None, **params):
requestor = Requestor(api_key)
url = cls.class_url()
wrapped_params = {cls.class_name(): params}
response, api_key = requestor.request('post', url, wrapped_params)
return convert_to_easypost_object(response, api_key)
class UpdateResource(Resource):
def save(self):
if self._unsaved_values:
requestor = Requestor(self._api_key)
params = {}
for k in self._unsaved_values:
params[k] = getattr(self, k)
if type(params[k]) is EasyPostObject:
params[k] = params[k].flatten_unsaved()
params = {self.class_name(): params}
url = self.instance_url()
response, api_key = requestor.request('put', url, params)
self.refresh_from(response, api_key)
return self
def class_name(cls):
if cls == Resource:
raise NotImplementedError('Resource is an abstract class. '
'You should perform actions on its subclasses.')
cls_name = six.text_type(cls.__name__)
cls_name = cls_name[0:1] + re.sub(r'([A-Z])', r'_\1', cls_name[1:])
return "%s" % cls_name.lower()
self.refresh_from(response, api_key)
return self
def buy(self, **params):
requestor = Requestor(self._api_key)
url = "%s/%s" % (self.instance_url(), "buy")
response, api_key = requestor.request('post', url, params)
self.refresh_from(response, api_key)
return self
class PickupRate(Resource):
pass
class Event(Resource):
@classmethod
def receive(self, values):
return convert_to_easypost_object(json.loads(values), api_key)
class CarrierAccount(AllResource, CreateResource, UpdateResource, DeleteResource):
@classmethod
def types(cls, api_key=None):
requestor = Requestor(api_key)
response, api_key = requestor.request('get', "/carrier_types")
return convert_to_easypost_object(response, api_key)
class User(CreateResource, UpdateResource, DeleteResource):
@classmethod
def create(cls, api_key=None, **params):
return "/%ses" % cls_name
else:
return "/%ss" % cls_name
def instance_url(self):
easypost_id = self.get('id')
if not easypost_id:
raise Error('%s instance has invalid ID: %r' % (type(self).__name__, easypost_id))
easypost_id = Requestor._utf8(easypost_id)
base = self.class_url()
param = quote_plus(easypost_id)
return "{base}/{param}".format(base=base, param=param)
# parent resource classes
class AllResource(Resource):
@classmethod
def all(cls, api_key=None, **params):
requestor = Requestor(api_key)
url = cls.class_url()
response, api_key = requestor.request('get', url, params)
return convert_to_easypost_object(response, api_key)
class CreateResource(Resource):
@classmethod
def create(cls, api_key=None, **params):
requestor = Requestor(api_key)
url = cls.class_url()
wrapped_params = {cls.class_name(): params}
response, api_key = requestor.request('post', url, wrapped_params)
return convert_to_easypost_object(response, api_key)
def _objects_to_ids(cls, param):
if isinstance(param, Resource):
return {'id': param.id}
elif isinstance(param, dict):
out = {}
for k, v in six.iteritems(param):
out[k] = cls._objects_to_ids(v)
return out
elif isinstance(param, list):
out = []
for k, v in enumerate(param):
out.append(cls._objects_to_ids(v))
return out
else:
return param
base = self.class_url()
param = quote_plus(easypost_id)
return "{base}/{param}".format(base=base, param=param)
# parent resource classes
class AllResource(Resource):
@classmethod
def all(cls, api_key=None, **params):
requestor = Requestor(api_key)
url = cls.class_url()
response, api_key = requestor.request('get', url, params)
return convert_to_easypost_object(response, api_key)
class CreateResource(Resource):
@classmethod
def create(cls, api_key=None, **params):
requestor = Requestor(api_key)
url = cls.class_url()
wrapped_params = {cls.class_name(): params}
response, api_key = requestor.request('post', url, wrapped_params)
return convert_to_easypost_object(response, api_key)
class UpdateResource(Resource):
def save(self):
if self._unsaved_values:
requestor = Requestor(self._api_key)
params = {}
for k in self._unsaved_values:
params[k] = getattr(self, k)
if self._unsaved_values:
requestor = Requestor(self._api_key)
params = {}
for k in self._unsaved_values:
params[k] = getattr(self, k)
if type(params[k]) is EasyPostObject:
params[k] = params[k].flatten_unsaved()
params = {self.class_name(): params}
url = self.instance_url()
response, api_key = requestor.request('put', url, params)
self.refresh_from(response, api_key)
return self
class DeleteResource(Resource):
def delete(self, **params):
requestor = Requestor(self._api_key)
url = self.instance_url()
response, api_key = requestor.request('delete', url, params)
self.refresh_from(response, api_key)
return self
# specific resources
class Address(AllResource, CreateResource):
@classmethod
def create(cls, api_key=None, verify=None, verify_strict=None, **params):
requestor = Requestor(api_key)
url = cls.class_url()