Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added user-agent and some documentation on resources

  • Loading branch information...
commit b2acb621eacb515f21aa6c5f18759eb73bb90c2e 1 parent b32fdd4
@alexjg alexjg authored
Showing with 28 additions and 1 deletion.
  1. +5 −1 gocardless/request.py
  2. +23 −0 gocardless/resources.py
View
6 gocardless/request.py
@@ -1,3 +1,4 @@
+import gocardless
import json
import requests
@@ -7,7 +8,10 @@ class Request(object):
def __init__(self, method, url):
self._method = method
self._url = url
- self._opts = {'headers': {'Accept': 'application/json'}}
+ headers = {}
+ headers["Accept"] = "application/json"
+ headers["User-Agent"] = "gocardless-python/{0}".format(gocardless.get_version())
+ self._opts = {"headers" : headers }
if not self._valid_method(method):
raise ValueError('Invalid method {}'.format(method))
View
23 gocardless/resources.py
@@ -20,17 +20,40 @@ def __new__(meta, name, bases, attrs):
return type.__new__(meta, name, bases, attrs)
class Resource(object):
+ """A GoCardless resource
+
+ Subclasses of `Resource` define class attributes to specify how
+ the resource is fetched and represented.
+
+ The class attribute `endpoint` is the path to the resource on the server.
+
+ The class attribute `date_fields` names fields which will be converted
+ into `datetime.datetime` objects on construction.
+
+ The class attribute `reference_fields` names fields which are uris to other
+ resources and will be converted into functions which can be called to
+ retrieve those resources.
+ """
__metaclass__ = ResourceMetaClass
date_fields = ["created_at"]
reference_fields = []
def __init__(self, in_attrs, client):
+ """Construct a resource
+
+ :param in_attrs: A dictionary of attributes, usually obtained from a
+ JSON response.
+ :param client: an instance of gocardless.Client
+ """
attrs = in_attrs.copy()
self._raw_attrs = attrs.copy()
self.id = attrs["id"]
self.client = client
if "sub_resource_uris" in attrs:
+ #For each subresource_uri create a method which grabs data
+ #from the URI and uses it to instantiate the relevant class
+ #and return it.
for name, uri in attrs.pop("sub_resource_uris").items():
path = re.sub(".*/api/v1", "", uri)
sub_klass = self._get_klass_from_name(name)
Please sign in to comment.
Something went wrong with that request. Please try again.