Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codes in exceptions to be better handled in client #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.md
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Future home of the Python lib.
Future home of the Python lib.

Notes: to run the tests do:

```
MAXIPAGO_ID='<your id>' MAXIPAGO_API_KEY='<your key/secret>' python tests.py
```
3 changes: 1 addition & 2 deletions maxipago/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# :copyright: (c) 2013 by Stored (www.stored.com.br).
# :license: BSD, see LICENSE for more details.

VERSION = (1, 0, 0)
__version__ = '.'.join(map(str, VERSION[0:3])) + ''.join(VERSION[3:])
__version__ = '1.0.1'
__author__ = 'Stored'
__contact__ = '[email protected]'
__docformat__ = 'restructuredtext'
Expand Down
8 changes: 6 additions & 2 deletions maxipago/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@


class MaxipagoException(Exception):
def __init__(self, message):
def __init__(self, message, code=None):
self.message = message
self.code = code

def __str__(self):
return self.message
if self.code:
return u"({}) {}".format(self.code, self.message)
else:
return self.message


class ValidationError(MaxipagoException):
Expand Down
5 changes: 4 additions & 1 deletion maxipago/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def request(self, xml_data, api_type=None):
response = requests.post(url=uri, data=xml_data, headers={'content-type': 'text/xml'})

if not str(response.status_code).startswith('2'):
raise HttpErrorException(u'Error %s: %s' % (response.status_code, response.reason))
raise HttpErrorException(
u'Error %s: %s' % (response.status_code, response.reason),
code=response.status_code
)
return response

def get_uri(self, api_type=None):
Expand Down
14 changes: 7 additions & 7 deletions maxipago/managers/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def add(self, **kwargs):
('expiration_month', {'translated_name': 'expirationMonth'}),
('expiration_year', {'translated_name': 'expirationYear'}),
('billing_name', {'translated_name': 'billingName'}),
('billing_address1', {'translated_name': 'billingAddress1'}),
('billing_address1', {'translated_name': 'billingAddress1', 'required': False}),
('billing_address2', {'translated_name': 'billingAddress2', 'required': False}),
('billing_city', {'translated_name': 'billingCity'}),
('billing_state', {'translated_name': 'billingState'}),
('billing_zip', {'translated_name': 'billingZip'}),
('billing_country', {'translated_name': 'billingCountry'}),
('billing_phone', {'translated_name': 'billingPhone'}),
('billing_email', {'translated_name': 'billingEmail'}),
('billing_city', {'translated_name': 'billingCity', 'required': False}),
('billing_state', {'translated_name': 'billingState', 'required': False}),
('billing_zip', {'translated_name': 'billingZip', 'required': False}),
('billing_country', {'translated_name': 'billingCountry', 'required': False}),
('billing_phone', {'translated_name': 'billingPhone', 'required': False}),
('billing_email', {'translated_name': 'billingEmail', 'required': False}),
('onfile_end_date', {'translated_name': 'onFileEndDate', 'required': False}),
('onfile_permissions', {'translated_name': 'onFilePermissions', 'required': False}),
('onfile_comment', {'translated_name': 'onFileComment', 'required': False}),
Expand Down
4 changes: 2 additions & 2 deletions maxipago/resources/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def process(self):
if error_code != '0':
error_message = tree.find('errorMessage').text

raise CardException(message=error_message)
raise CardException(message=error_message, code=error_code)

self._meta = {
'command': tree.find('command').text,
Expand All @@ -34,7 +34,7 @@ def process(self):
if error_code != '0':
error_message = tree.find('errorMessage').text

raise CardException(message=error_message)
raise CardException(message=error_message, code=error_code)

self._meta = {
'command': tree.find('command').text,
Expand Down
9 changes: 5 additions & 4 deletions maxipago/resources/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def process(self):
error_message = tree.find('errorMessage').text

if 'already exists' in error_message.lower():
raise CustomerAlreadyExists(message=error_message)
raise CustomerAlreadyExists(message=error_message,
code=error_code)

raise CustomerException(message=error_message)
raise CustomerException(message=error_message, code=error_code)

self._meta = {
'command': tree.find('command').text,
Expand All @@ -38,7 +39,7 @@ def process(self):
if error_code != '0':
error_message = tree.find('errorMessage').text

raise CustomerException(message=error_message)
raise CustomerException(message=error_message, code=error_code)

self._meta = {
'command': tree.find('command').text,
Expand All @@ -58,7 +59,7 @@ def process(self):
if error_code != '0':
error_message = tree.find('errorMessage').text

raise CustomerException(message=error_message)
raise CustomerException(message=error_message, code=error_code)

self._meta = {
'command': tree.find('command').text,
Expand Down
2 changes: 1 addition & 1 deletion maxipago/resources/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def process(self):
error_code = tree.find('errorCode')
if error_code is not None and error_code.text != '0':
error_message = tree.find('errorMsg').text
raise PaymentException(message=error_message)
raise PaymentException(message=error_message, code=error_code)

processor_code = tree.find('processorCode')

Expand Down
58 changes: 36 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
# coding: utf-8
# encoding: utf-8
import re
import os
from setuptools import setup
from codecs import open
from setuptools import setup, find_packages
from os import path

HERE = path.abspath(path.dirname(__file__))

def read_file(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

def read(*names):
with open(path.join(HERE, *names)) as stream:
return stream.read()

def get_version():
meta_filedata = read_file('maxipago/__init__.py')
re_version = re.compile(r'VERSION\s*=\s*\((.*?)\)')
group = re_version.search(meta_filedata).group(1)
version = filter(None, map(lambda s: s.strip(), group.split(',')))
return '.'.join(version)

def find_info(info, *file_paths):
content = read(*file_paths)
matching = re.search(
r'''^__{}__ = ['"]([^'"]*)['"]'''.format(info),
content,
re.M
)
if matching:
return matching.group(1)
raise RuntimeError('Unable to find {} string.'.format(info))


LONG_DESCRIPTION = read('README.md')
VERSION = find_info('version', 'maxipago', '__init__.py')
AUTHOR = find_info('author', 'maxipago', '__init__.py')
AUTHOR_EMAIL = find_info('contact', 'maxipago', '__init__.py')
LICENSE = find_info('license', 'maxipago', '__init__.py')

setup(
name='maxipago',
version=get_version(),
author='Stored',
author_email='[email protected]',
version=VERSION,
author=AUTHOR,
author_email=AUTHOR_EMAIL,
description='',
license='MIT',
license=LICENSE,
keywords='',
url='',
packages=['maxipago'],
long_description=read_file('README.md'),
url='https://github.com/loggi/Python-integration-lib/',
packages=find_packages(),
long_description=LONG_DESCRIPTION,
classifiers=[
"Topic :: Utilities",
],
install_requires=[
'requests==1.1.0',
'lxml==3.1.0',
],
install_requires=(
'requests==2.4.3',
'lxml==3.4.0',
),
)
61 changes: 60 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ def test_add_customer_already_existing(self):
self.assertTrue(hasattr(response, 'id'))

# creating customer with the same id.
with self.assertRaises(exceptions.CustomerAlreadyExists):
with self.assertRaises(exceptions.CustomerAlreadyExists) as exc:
response = self.maxipago.customer.add(
customer_id=CUSTOMER_ID,
first_name='Fulano',
last_name='de Tal',
)
self.assertEqual(exc.exception.code, '1')

def test_delete_customer(self):
CUSTOMER_ID = randint(1, 100000)
Expand Down Expand Up @@ -115,6 +116,64 @@ def test_add_card(self):

self.assertTrue(getattr(response, 'token', False))

def test_add_existent_card(self):
CUSTOMER_ID = randint(1, 100000)

response = self.maxipago.customer.add(
customer_id=CUSTOMER_ID,
first_name=u'Fulano',
last_name=u'de Tal',
)

self.assertTrue(hasattr(response, 'id'))

maxipago_customer_id = response.id

add_card_kwargs = dict(
customer_id=maxipago_customer_id,
number=u'4111111111111111',
expiration_month=u'02',
expiration_year=date.today().year + 3,
billing_name=u'Fulano de Tal',
billing_address1=u'Rua das Alamedas, 123',
billing_city=u'Rio de Janeiro',
billing_state=u'RJ',
billing_zip=u'20123456',
billing_country=u'BR',
billing_phone=u'552140634666',
billing_email=u'[email protected]'
)
response = self.maxipago.card.add(**add_card_kwargs)
self.assertTrue(getattr(response, 'token', False))

with self.assertRaises(exceptions.CardException) as exc:
self.maxipago.card.add(**add_card_kwargs)

self.assertEqual(exc.exception.code, '1')

def test_add_card_minimal_fields(self):
CUSTOMER_ID = randint(1, 100000)

response = self.maxipago.customer.add(
customer_id=CUSTOMER_ID,
first_name=u'Fulano',
last_name=u'de Tal',
)

self.assertTrue(hasattr(response, 'id'))

maxipago_customer_id = response.id

response = self.maxipago.card.add(
customer_id=maxipago_customer_id,
number=u'4111111111111111',
expiration_month=u'02',
expiration_year=date.today().year + 3,
billing_name=u'Fulano de Tal',
)

self.assertTrue(getattr(response, 'token', False))

def test_delete_card(self):
CUSTOMER_ID = randint(1, 100000)

Expand Down