Gobble is the Python client for Open-Spending
In [1]:
from gobble import pull, validate, push
In [2]:
help(push)
In [2]:
batch = push('/home/loic/repos/gobble/assets/datapackage/datapackage.json')
In [19]:
batch.in_progress
Out[19]:
In [37]:
batch.name
Out[37]:
In [39]:
batch.filepath
Out[39]:
In [11]:
batch.files
Out[11]:
In [13]:
batch.os_url
Out[13]:
In [16]:
len(batch)
Out[16]:
In [22]:
resource = batch[0]
resource
Out[22]:
In [1]:
for row in resource.iter():
for column in row.items():
print(column)
break
This is almost equivalent to:
In [61]:
resource.data[0]
Out[61]:
In [44]:
batch.save('/home/loic/test.zip')
In [4]:
push({'foo': 'bar'})
To return a list of error messages, use the validate function with the raise_error set to False.
In [40]:
help(validate)
In [41]:
errors = validate({'foo': 'bar'}, raise_error=False)
errors
Out[41]:
In [47]:
help(pull)
In [51]:
mexican_packages = {'countryCode': 'MX'}
results = pull(mexican_packages)
len(results)
Out[51]:
In [55]:
results[0]['package']['author']
Out[55]:
In [6]:
ls
In [58]:
cat gobble.log | tail -n 5
Basically you have your logs, your user information and the last snapshot of the request for each endpoint:
In [25]:
cat ~/.gobble/GET.user.check.json | jq .
It's possible to communicate with the conductor API at a lower level if you wish. Gobble has a all the API endpoints pre-defined as callable objects:
authenticate_useroauth_callbackauthorize_userupdate_usersearch_packagesrequest_uploadupload_packagetoggle_packageFor example, let's look at the endpoint to get user permissions, represented by authorize_user.
In [15]:
from gobble.api import authorize_user
authorize_user.info
Out[15]:
To make a request, call the object. You can use the same keyword arguments as you would for the generic requests.Request function, i.e headers, json and params and data and you will get back a standard requests.Response.
In [16]:
payload = {'jwt':'token'}
authorize_user(params=payload)
Out[16]:
In [21]:
from gobble.api import request_upload, handle
payload = {'bad_payload': 'I am bad'}
response = request_upload(params=payload)
handle(response)
In [22]:
from json import dumps
print(request_upload.snapshot.json)
In [24]:
from gobble.api import EndPoint
leap_into_the_unknown = EndPoint('GET', 'I', 'do', 'not', 'exist')
leap_into_the_unknown()
Out[24]:
In [5]:
cd ~/.gobble