Skip to content

Commit b8a6085

Browse files
author
Marshall Jones
committed
initial commit. kinda, sorta works
1 parent 5ba27b6 commit b8a6085

29 files changed

+369
-3008
lines changed

balanced/__init__.py

+18-52
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,34 @@
1-
__version__ = '0.11.14'
2-
from collections import defaultdict
3-
import contextlib
1+
from __future__ import unicode_literals
42

5-
from balanced._http_client import HTTPClient
3+
__version__ = '1.1.0pre'
4+
5+
from balanced.config import configure
66
from balanced.resources import (
7-
Resource, Marketplace, Account, APIKey,
8-
Hold, Credit, Debit, Refund,
9-
Merchant, Transaction, BankAccount, Card,
7+
Resource, Marketplace, APIKey,
8+
CardHold, Credit, Debit, Refund, Reversal,
9+
Transaction, BankAccount, Card,
1010
Callback, Event, EventCallback, EventCallbackLog,
1111
BankAccountVerification, Customer,
1212
)
1313
from balanced import exc
1414

15-
1615
__all__ = [
17-
Resource.__name__,
18-
Marketplace.__name__,
19-
Account.__name__,
2016
APIKey.__name__,
21-
Hold.__name__,
22-
Credit.__name__,
23-
Debit.__name__,
24-
Refund.__name__,
25-
Merchant.__name__,
26-
Transaction.__name__,
27-
Card.__name__,
2817
BankAccount.__name__,
18+
BankAccountVerification.__name__,
2919
Callback.__name__,
20+
Card.__name__,
21+
CardHold.__name__,
22+
Credit.__name__,
23+
Customer.__name__,
24+
Debit.__name__,
3025
Event.__name__,
3126
EventCallback.__name__,
3227
EventCallbackLog.__name__,
33-
BankAccountVerification.__name__,
34-
Customer.__name__,
28+
Marketplace.__name__,
29+
Resource.__name__,
30+
Refund.__name__,
31+
Reversal.__name__,
32+
Transaction.__name__,
3533
exc.__name__.partition('.')[-1],
3634
]
37-
38-
# See https://github.com/balanced/balanced-python/issues/44 re: naming.
39-
http_client = HTTPClient()
40-
config = http_client.config
41-
42-
43-
CACHE = defaultdict(dict)
44-
45-
46-
def bust_cache():
47-
CACHE.clear()
48-
49-
50-
def configure(api_key_secret):
51-
config.api_key_secret = api_key_secret
52-
53-
54-
def is_configured():
55-
return bool(config.api_key_secret)
56-
57-
58-
Resource.http_client = http_client
59-
60-
61-
@contextlib.contextmanager
62-
def key_switcher(the_new_api_key_secret):
63-
old_api_key = config.api_key_secret
64-
config.api_key_secret = the_new_api_key_secret
65-
try:
66-
yield
67-
finally:
68-
config.api_key_secret = old_api_key

balanced/_http_client.py

-186
This file was deleted.

balanced/config.py

+84-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,87 @@
1-
from balanced import __version__
2-
from balanced.utils import urljoin
3-
4-
5-
def _make_user_agent():
6-
return 'balanced-python/' + __version__
7-
8-
9-
class Config(object):
10-
11-
def __init__(self):
12-
super(Config, self).__init__()
13-
self.api_key_secret = None
14-
self.api_version = '1'
15-
self.root_uri = 'https://api.balancedpayments.com'
16-
# this is requests' config that get passed down on
17-
# every http operation.
18-
#
19-
# see: http://docs.python-requests.org/en/v0.10.4/api/#configurations
20-
self.requests = {
21-
'base_headers': {
22-
'Accept': 'application/json',
23-
'Content-Type': 'application/json',
24-
'User-agent': _make_user_agent(),
25-
},
1+
from __future__ import unicode_literals
2+
from datetime import datetime
3+
4+
import simplejson as json
5+
from iso8601 import iso8601
6+
import wac
7+
8+
from balanced import exc
9+
from . import __version__
10+
11+
12+
API_ROOT = 'https://api.balancedpayments.com'
13+
14+
# config
15+
def configure(
16+
user=None,
17+
root_url=API_ROOT,
18+
api_revision='1.1',
19+
user_agent='balanced-python/' + __version__,
20+
**kwargs
21+
):
22+
# http
23+
kwargs['client_agent'] = 'knox-client/' + __version__
24+
if 'headers' not in kwargs:
25+
kwargs['headers'] = {
26+
'accept': 'application/vnd.api+json;revision=' + api_revision
2627
}
28+
kwargs['headers']['Accept-Type'] = 'application/json'
29+
if 'error_cls' not in kwargs:
30+
kwargs['error_cls'] = exc.HTTPError
31+
if user:
32+
kwargs['auth'] = (user, None)
33+
# apply
34+
Client.config = Config(root_url, user_agent=user_agent, **kwargs)
35+
36+
37+
class Config(wac.Config):
38+
39+
api_revision = None
40+
41+
user_agent = None
42+
43+
44+
default_config = Config(API_ROOT)
45+
46+
47+
# client
48+
49+
class Client(wac.Client):
50+
51+
config = default_config
52+
53+
@staticmethod
54+
def _default_serialize(o):
55+
if isinstance(o, datetime):
56+
return o.isoformat() + 'Z'
57+
raise TypeError(
58+
'Object of type {} with value of {} is not JSON serializable'
59+
.format(type(o), repr(o)))
60+
61+
def _serialize(self, data):
62+
data = json.dumps(data, default=self._default_serialize)
63+
return 'application/json', data
64+
65+
@staticmethod
66+
def _parse_deserialized(e):
67+
if isinstance(e, dict):
68+
for k in e.iterkeys():
69+
if k.endswith('_at') and isinstance(e[k], basestring):
70+
e[k] = iso8601.parse_date(e[k])
71+
return e
72+
73+
def _deserialize(self, response):
74+
if response.headers['Content-Type'] != 'application/json':
75+
raise Exception("Unsupported content-type '{}'".format(
76+
response.headers['Content-Type']
77+
))
78+
if not response.content:
79+
return None
80+
data = json.loads(response.content)
81+
return self._parse_deserialized(data)
82+
83+
84+
configure()
2785

28-
@property
29-
def uri(self):
30-
return urljoin(self.root_uri, self.version)
86+
client = Client()
3187

32-
@property
33-
def version(self):
34-
return 'v' + self.api_version

0 commit comments

Comments
 (0)