From 160016e55f9f59c6ded028490b777a1fb3a3e20e Mon Sep 17 00:00:00 2001 From: Diego Tolentino Date: Thu, 16 Mar 2017 18:54:26 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Atualiza=C3=A7=C3=A3o=20para=20suportar=20p?= =?UTF-8?q?ython=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cieloApi3/objectJSON.py | 2 +- example/saleBoletoComplete.py | 14 +++++++------- example/saleBoletoSimple.py | 14 +++++++------- example/saleCard.py | 18 +++++++++--------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cieloApi3/objectJSON.py b/cieloApi3/objectJSON.py index 49bcc04..aa3d3be 100644 --- a/cieloApi3/objectJSON.py +++ b/cieloApi3/objectJSON.py @@ -58,7 +58,7 @@ def remove_none_dict(obj): retorno = {} for chave in obj: valor = obj[chave] - if valor or isinstance(valor, (int, long, float, complex)): + if valor or isinstance(valor, (int, float, complex)): retorno[chave] = remove_none(valor) return retorno diff --git a/example/saleBoletoComplete.py b/example/saleBoletoComplete.py index 3a7ef35..d0b5a0f 100644 --- a/example/saleBoletoComplete.py +++ b/example/saleBoletoComplete.py @@ -39,9 +39,9 @@ # Criar a venda e imprime o retorno response_create_sale = cielo_ecommerce.create_sale(sale) -print '----------------------response_create_sale----------------------' -print json.dumps(response_create_sale, indent=2) -print '----------------------response_create_sale----------------------' +print('----------------------response_create_sale----------------------') +print(json.dumps(response_create_sale, indent=2)) +print('----------------------response_create_sale----------------------') # Com a venda criada na Cielo, já temos o ID do pagamento, TID e demais # dados retornados pela Cielo @@ -49,8 +49,8 @@ # Com o ID do pagamento, podemos fazer uma consulta do pagamento response_get_sale = cielo_ecommerce.get_sale(payment_id) -print '----------------------response_get_sale----------------------' -print json.dumps(response_get_sale, indent=2) -print '----------------------response_get_sale----------------------' +print('----------------------response_get_sale----------------------') +print(json.dumps(response_get_sale, indent=2)) +print('----------------------response_get_sale----------------------') -print '\r\nLink Boleto:', sale.payment.url, '\r\n' +print('\r\nLink Boleto:', sale.payment.url, '\r\n') diff --git a/example/saleBoletoSimple.py b/example/saleBoletoSimple.py index e3f4c1d..6f0fba5 100644 --- a/example/saleBoletoSimple.py +++ b/example/saleBoletoSimple.py @@ -30,9 +30,9 @@ # Criar a venda e imprime o retorno response_create_sale = cielo_ecommerce.create_sale(sale) -print '----------------------response_create_sale----------------------' -print json.dumps(response_create_sale, indent=2) -print '----------------------response_create_sale----------------------' +print('----------------------response_create_sale----------------------') +print(json.dumps(response_create_sale, indent=2)) +print('----------------------response_create_sale----------------------') # Com a venda criada na Cielo, já temos o ID do pagamento, TID e demais # dados retornados pela Cielo @@ -40,8 +40,8 @@ # Com o ID do pagamento, podemos fazer uma consulta do pagamento response_get_sale = cielo_ecommerce.get_sale(payment_id) -print '----------------------response_get_sale----------------------' -print json.dumps(response_get_sale, indent=2) -print '----------------------response_get_sale----------------------' +print('----------------------response_get_sale----------------------') +print(json.dumps(response_get_sale, indent=2)) +print('----------------------response_get_sale----------------------') -print '\r\nLink Boleto:', sale.payment.url, '\r\n' +print('\r\nLink Boleto:', sale.payment.url, '\r\n') diff --git a/example/saleCard.py b/example/saleCard.py index 1b5d17e..eb9ce3a 100644 --- a/example/saleCard.py +++ b/example/saleCard.py @@ -35,9 +35,9 @@ # Criar a venda e imprime o retorno response_create_sale = cielo_ecommerce.create_sale(sale) -print '----------------------response_create_sale----------------------' -print json.dumps(response_create_sale, indent=2) -print '----------------------response_create_sale----------------------' +print('----------------------response_create_sale----------------------') +print(json.dumps(response_create_sale, indent=2)) +print('----------------------response_create_sale----------------------') # Com a venda criada na Cielo, já temos o ID do pagamento, TID e demais # dados retornados pela Cielo @@ -46,12 +46,12 @@ # Com o ID do pagamento, podemos fazer sua captura, # se ela não tiver sido capturada ainda response_capture_sale = cielo_ecommerce.capture_sale(payment_id, 15700, 0) -print '----------------------response_capture_sale----------------------' -print json.dumps(response_capture_sale, indent=2) -print '----------------------response_capture_sale----------------------' +print('----------------------response_capture_sale----------------------') +print(json.dumps(response_capture_sale, indent=2)) +print('----------------------response_capture_sale----------------------') # E também podemos fazer seu cancelamento, se for o caso response_cancel_sale = cielo_ecommerce.cancel_sale(payment_id, 15700) -print '---------------------response_cancel_sale---------------------' -print json.dumps(response_cancel_sale, indent=2) -print '---------------------response_cancel_sale---------------------' +print('---------------------response_cancel_sale---------------------') +print(json.dumps(response_cancel_sale, indent=2)) +print('---------------------response_cancel_sale---------------------') From b941c8f1263514850e5a3b1673066bfda2795884 Mon Sep 17 00:00:00 2001 From: Diego Tolentino Date: Thu, 6 Apr 2017 15:59:55 -0300 Subject: [PATCH 2/2] Melhorado a forma de retornar erros da api --- cieloApi3/__init__.py | 1 + cieloApi3/request/base.py | 18 +++++++----------- cieloApi3/request/exceptions.py | 28 ++++++++++++++++++++++++++++ setup.py | 2 +- 4 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 cieloApi3/request/exceptions.py diff --git a/cieloApi3/__init__.py b/cieloApi3/__init__.py index 5168708..0a5b0d8 100644 --- a/cieloApi3/__init__.py +++ b/cieloApi3/__init__.py @@ -9,3 +9,4 @@ from .recurrentPayment import * from .cieloEcommerce import * +from .request.exceptions import CieloRequestException diff --git a/cieloApi3/request/base.py b/cieloApi3/request/base.py index 3b8cd4d..cdd7a39 100644 --- a/cieloApi3/request/base.py +++ b/cieloApi3/request/base.py @@ -1,10 +1,12 @@ -import uuid, json +import uuid from future.utils import raise_with_traceback from requests import Request, Session -class Base(object): +from .exceptions import CieloRequestException + +class Base(object): def __init__(self, merchant): self.merchant = merchant @@ -36,7 +38,7 @@ def send_request(self, method, uri, data=None, params=None): response = s.send(prep) - if 'json' in response.headers['Content-Type'].lower(): + if 'json' in response.headers.get('content-type', '').lower(): answers = response.json() else: answers = [{ @@ -45,13 +47,7 @@ def send_request(self, method, uri, data=None, params=None): }] if response.status_code >= 400: - errors = [] - - for answer in answers: - errors.append('\r\n * [%s] %s\r\n' % (answer['Code'], answer['Message'])) - - data_send = json.loads(body or 'null') - - raise_with_traceback(Exception('\r\n%s\r\nMethod: %s\r\nUri: %s\r\nData: %s' % (''.join(errors), method, response.url, json.dumps(data_send, indent=2)))) + raise_with_traceback(CieloRequestException(response=response, + **answers[0])) return answers diff --git a/cieloApi3/request/exceptions.py b/cieloApi3/request/exceptions.py new file mode 100644 index 0000000..99e9e7a --- /dev/null +++ b/cieloApi3/request/exceptions.py @@ -0,0 +1,28 @@ +import json + + +class CieloRequestException(Exception): + Code = None + Message = None + request = None + response = None + + def __init__(self, *args, **kwargs): + response = kwargs.pop('response', None) + self.response = response + self.request = kwargs.pop('request', None) + if (response is not None and not self.request and + hasattr(response, 'request')): + self.request = self.response.request + self.Code = int(kwargs.pop('Code', None)) + self.Message = kwargs.pop('Message', None) + + def __str__(self): + data_send = json.loads(self.request.body or 'null') + + errors = [] + errors.append('* [%s] %s\r\n' % (self.Code, self.Message)) + errors.append('Method: %s\r\n' % self.request.method) + errors.append('Uri: %s\r\n' % self.response.url) + errors.append('Data: %s\r\n' % json.dumps(data_send, indent=2)) + return ''.join(errors) diff --git a/setup.py b/setup.py index 759f6c6..0761a49 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ url='https://github.com/thiago-Malaca/API-3.0-Python', keywords='api3.0 cielo python sdk ecommerce', packages=find_packages(), - install_requires=['requests', 'future'], + install_requires=['requests==2.13.*', 'future'], license='MIT', classifiers=( # 'Development Status :: 5 - Production/Stable',