|
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 |
26 | 27 | }
|
| 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() |
27 | 85 |
|
28 |
| - @property |
29 |
| - def uri(self): |
30 |
| - return urljoin(self.root_uri, self.version) |
| 86 | +client = Client() |
31 | 87 |
|
32 |
| - @property |
33 |
| - def version(self): |
34 |
| - return 'v' + self.api_version |
|
0 commit comments