Skip to content

Commit 6cceabb

Browse files
author
Juan Rossi
committed
Initial commit
0 parents  commit 6cceabb

File tree

11 files changed

+706
-0
lines changed

11 files changed

+706
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Mango
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
PyMango
2+
=======
3+
4+
This is a Python library that allows interaction with `Mango API <https://developers.getmango.com/en/api/?platform=python>`_.
5+
6+
7+
Install
8+
=======
9+
10+
You can get the library from our GitHub account and copy the ``pymango`` folder into your project::
11+
12+
$ git clone https://github.com/Mango/mango-python.git
13+
14+
If you want to use ``pip``, you can point it directly into the repository::
15+
16+
$ pip install -e https://github.com/Mango/mango-python.git#egg=pymango
17+
18+
We are working out to include ``pymango`` into `PyPI <https://pypi.python.org/pypi>`_.
19+
20+
21+
Documentation
22+
=============
23+
24+
Documentation is available at https://developers.getmango.com/en/api/?platform=python
25+
26+
Usage
27+
=====
28+
29+
Import the library and set your secret API key::
30+
31+
>>> import pymango as mango
32+
>>> mango.api_key = "your Mango secret API key"
33+
34+
35+
Create a charge
36+
---------------
37+
38+
In order to create a Charge, you must call the ``create()`` method with
39+
the required arguments (see `API documentation <https://developers.getmango.com/en/api/charges/?platform=python#arguments>`_)::
40+
41+
>>> charge = mango.Charges.create(amount=2000, email="[email protected]", token="token_mwhushs06o62aruq9n3pmvu7f0ia696y")
42+
>>> print charge.get("uid")
43+
charge_72t1r7vmb9pknrl4otg6xy3wrkwrrpzt
44+
>>> print charge.get("paid")
45+
True
46+
47+
48+
Get single Charge
49+
-----------------
50+
51+
When you have a Charge ``uid``, you can get a full detail using the ``get()`` method::
52+
53+
>>> mango.Charges.get("charge_72t1r7vmb9pknrl4otg6xy3wrkwrrpzt")
54+
{
55+
u'customer': u'',
56+
u'deposit_eta': u'2013-04-09',
57+
u'queue_uid': u'',
58+
u'fee': 12,
59+
u'annual_interest_pct': u'',
60+
u'refunded': False,
61+
u'created_at': u'2013-09-04 20:23:28',
62+
u'live': False,
63+
u'amount_gross': 1212,
64+
u'paid': False,
65+
u'failure_message': u'',
66+
u'origin': u'api',
67+
u'amount': 1212,
68+
u'installments': 1,
69+
u'plastic': {
70+
u'last4': u'4242',
71+
u'exp_year': 2015,
72+
u'exp_month': 11,
73+
u'type': u'visa',
74+
u'holdername': u'Test Test'
75+
},
76+
u'uid': u'charge_72t1r7vmb9pknrl4otg6xy3wrkwrrpzt',
77+
u'email': u'[email protected]',
78+
u'card': u'',
79+
u'description': u''
80+
}
81+
82+
You can also work with all the other resources authenticated with a secret API Key:
83+
84+
* `Charges <https://developers.getmango.com/en/api/charges/?platform=python>`_
85+
* `Refunds <https://developers.getmango.com/en/api/refunds/?platform=python>`_
86+
* `Customers <https://developers.getmango.com/en/api/customers/?platform=python>`_
87+
* `Cards <https://developers.getmango.com/en/api/cards/?platform=python>`_
88+
* `Queue <https://developers.getmango.com/en/api/queue/?platform=python>`_
89+
* `Installments <https://developers.getmango.com/en/api/installments/?platform=python>`_
90+
91+
92+
Tests
93+
=====
94+
95+
Install testing dependencies::
96+
97+
$ pip install nose
98+
99+
To run the tests you'll need Mango API keys (mode Sandbox)::
100+
101+
$ export MANGO_SECRET_TEST_KEY=secret_test_qawsedrftgyhujikolp
102+
$ export MANGO_PUBLIC_TEST_KEY=public_test_aqswdefrgthyjukilon
103+
104+
105+
Run the tests
106+
-------------
107+
108+
Use ``nosetests`` to run the complete tests suite::
109+
110+
$ nosetests pymango/tests/
111+
112+
113+
License
114+
=======
115+
116+
`MIT <http://opensource.org/licenses/MIT>`_, see LICENSE file.

pymango/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Mango Python Library
3+
"""
4+
from resources import Charge as Charges
5+
from resources import Refund as Refunds
6+
from resources import Customer as Customers
7+
from resources import Card as Cards
8+
from resources import Queue
9+
from resources import Installment as Installments
10+
11+
api_key = ""
12+
version = 1
13+

pymango/client.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Mango Python Library Client
3+
"""
4+
import json
5+
6+
import requests
7+
from requests.exceptions import ConnectionError
8+
9+
from .error import UnableToConnect, AuthenticationError, NotFound, \
10+
InputValidationError, InputValidationGenericError, \
11+
UnhandledError, MethodNotAllowed
12+
13+
14+
BASE_URL = "https://api.getmango.com"
15+
HEADERS = {"content-type": "application/json"}
16+
17+
18+
def req(api_key, method, endpoint, data=None, params=None):
19+
"""
20+
Make request and return a Python object from the JSON response. If
21+
HTTP method is DELETE return True for 204 response, false otherwise.
22+
23+
:param api_key: String with the API key
24+
:param method: String with the HTTP method
25+
:param endpoint: String with the URL
26+
:param data: Dictionary with data that will be sent
27+
:param params: Dictionary with query strings
28+
:return: Native python object resulting of the JSON deserialization of the API response
29+
"""
30+
if data:
31+
data = json.dumps(data)
32+
33+
try:
34+
response = requests.request(
35+
method,
36+
build_url(endpoint),
37+
data=data,
38+
params=params,
39+
auth=(api_key, ""),
40+
headers=HEADERS
41+
)
42+
except ConnectionError:
43+
raise UnableToConnect
44+
45+
# Success
46+
if 200 <= response.status_code <= 206:
47+
if response.request.method == "DELETE":
48+
return response.status_code == 204 or response.status_code == 200
49+
50+
return response.json()
51+
52+
# Error handling
53+
if response.status_code == 400:
54+
try:
55+
input_validation_error = response.json()
56+
errors = input_validation_error.get("errors")[0]
57+
error_code, error_message = errors.items()[0]
58+
except:
59+
raise InputValidationGenericError("{status_code}: {text}".format(
60+
status_code=response.status_code,
61+
text=response.text
62+
))
63+
raise InputValidationError(error_code, error_message)
64+
elif response.status_code == 404:
65+
raise NotFound
66+
elif response.status_code == 401:
67+
raise AuthenticationError
68+
elif response.status_code == 404:
69+
raise NotFound
70+
elif response.status_code == 405:
71+
raise MethodNotAllowed
72+
73+
raise UnhandledError("{status_code}: {text}".format(
74+
status_code=response.status_code,
75+
text=response.text
76+
))
77+
78+
79+
def build_url(endpoint):
80+
"""
81+
Build complete URL from API endpoint
82+
83+
:param endpoint: String with the endpoint, ex: /v1/charges/
84+
:return: String with complete URL, ex: https://api.getmango.com/v1/charges/
85+
"""
86+
return "{base_url}/{endpoint}".format(
87+
base_url=BASE_URL,
88+
endpoint=endpoint
89+
)

pymango/error.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Mango Library Exceptions
3+
"""
4+
5+
6+
class MangoException(Exception):
7+
"""Base exception"""
8+
pass
9+
10+
11+
class InvalidApiKey(MangoException):
12+
"""Invalid API Key"""
13+
pass
14+
15+
16+
class UnableToConnect(MangoException):
17+
"""Unable to connect into Mango API"""
18+
pass
19+
20+
21+
class NotFound(MangoException):
22+
"""Resource not found"""
23+
pass
24+
25+
26+
class MethodNotAllowed(MangoException):
27+
"""Method not allowed"""
28+
pass
29+
30+
31+
class AuthenticationError(MangoException):
32+
"""Authentication error"""
33+
pass
34+
35+
36+
class UnhandledError(MangoException):
37+
"""Unhandled error"""
38+
pass
39+
40+
41+
class InputValidationError(MangoException):
42+
"""Input validation error"""
43+
def __init__(self, error_code, error_message):
44+
self.code = error_code
45+
self.message = error_message
46+
47+
def __str__(self):
48+
return repr("{code}: {message}".format(
49+
code=self.code,
50+
message=self.message
51+
))
52+
53+
54+
class InputValidationGenericError(MangoException):
55+
"""Input validation generic error"""
56+
pass

0 commit comments

Comments
 (0)