Commit 869121cd authored by Raul Granados's avatar Raul Granados

return object on create, retrieve and update

parent a2b1dd0a
......@@ -52,6 +52,8 @@ class Mienvio:
Build request mienvio API
"""
first_key = None
@classmethod
def build_http_request(cls, method, path, payload=None, params=None):
"""
......@@ -62,7 +64,6 @@ class Mienvio:
:param params: query params by url
:return:
"""
api_base = 'http://pruebas.mienvio.mx/api/' if sandbox else 'https://app.mienvio.mx/api/'
headers = {
......@@ -82,7 +83,6 @@ class Mienvio:
if body.status_code == 400:
raise MalformedRequestError(body.json())
elif body.status_code == 401:
print(body.text)
raise AuthenticationError(body.json())
elif body.status_code == 402:
raise ProcessingError(body.json())
......@@ -96,8 +96,9 @@ class Mienvio:
raise MienvioError(body.json())
@classmethod
def to_object(cls, response):
for key, value in response.items():
def to_object(cls, response, first_key=None):
data = response[first_key] if first_key else response
for key, value in data.items():
setattr(cls, key, value)
return cls
......@@ -108,7 +109,7 @@ class Mienvio:
:param data: dict with data for create object
:return: object with data from response
"""
return cls.to_object(cls.build_http_request('post', cls.__name__.lower(), data))
return cls.to_object(cls.build_http_request('post', cls.__name__.lower(), data), cls.first_key)
@classmethod
def retrieve(cls, oid, params=None):
......@@ -117,7 +118,9 @@ class Mienvio:
:params oid: id of object retrieve
:return: object with data from response
"""
return cls.to_object(cls.build_http_request('get', '{}/{}'.format(cls.__name__.lower(), oid), params=params))
return cls.to_object(
cls.build_http_request('get', '{}/{}'.format(cls.__name__.lower(), oid), params=params), cls.first_key
)
@classmethod
def all(cls, params=None):
......@@ -142,7 +145,9 @@ class Mienvio:
:type data: data
:return: object with data from response
"""
return cls.to_object(cls.build_http_request('put', '{}/{}'.format(cls.__name__.lower(), oid), data))
return cls.to_object(
cls.build_http_request('put', '{}/{}'.format(cls.__name__.lower(), oid), data), cls.first_key
)
@classmethod
def delete(cls, oid):
......@@ -157,21 +162,25 @@ class Addresses(Mienvio):
"""
CRUD Addresses
"""
first_key = 'address'
class Shipments(Mienvio):
"""
CRUD Shipments
"""
first_key = 'shipment'
class Purchases(Mienvio):
"""
CRUD Purchases
"""
first_key = 'purchase'
class Orders(Mienvio):
"""
CRUD orders
"""
first_key = 'order'
......@@ -7,7 +7,7 @@ class AddressesEndPintTestCase(BaseEndpointTestCase):
self.client.api_key = 'set_api_key'
self.client.sandbox = True
address = self.client.Addresses.create(self.address_object)
assert address.address['object_id']
assert address.object_id
def test_get_all_address(self):
self.client.api_key = 'set_api_key'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment