from gobble import pull, validate, push

"""
Explanation: Welcome to Gobble
Gobble is the Python client for Open-Spending
End of explanation
"""


help(push)

batch = push('/home/loic/repos/gobble/assets/datapackage/datapackage.json')

batch.in_progress

batch.name

batch.filepath

batch.files

batch.os_url

len(batch)

resource = batch[0]
resource

for row in resource.iter():
    for column in row.items():
        print(column)
        break

"""
Explanation: Pushing a datapackage to Open-Spending
End of explanation
"""


resource.data[0]

batch.save('/home/loic/test.zip')

"""
Explanation: This is almost equivalent to:
End of explanation
"""


push({'foo': 'bar'})

"""
Explanation: Validating a fiscal-datapackage
Pushing a package with an invalid schema will fail.
End of explanation
"""


help(validate)

errors = validate({'foo': 'bar'}, raise_error=False)
errors

"""
Explanation: To return a list of error messages, use the validate function with the raise_error set to False.
End of explanation
"""


help(pull)

mexican_packages = {'countryCode': 'MX'}
results = pull(mexican_packages)

len(results)

results[0]['package']['author']

"""
Explanation: Pulling packages from Open-Spending
You can search for specific packages in the database, with the pull function.
End of explanation
"""


ls

cat gobble.log | tail -n 5

"""
Explanation: What's inside the user folder?
End of explanation
"""


cat ~/.gobble/GET.user.check.json | jq .

"""
Explanation: Basically you have your logs, your user information and the last snapshot of the request for each endpoint:
End of explanation
"""


from gobble.api import authorize_user

authorize_user.info

"""
Explanation: Low level usage
API endpoints
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_user
oauth_callback
authorize_user
update_user
search_packages
request_upload
upload_package
toggle_package

For example, let's look at the endpoint to get user permissions, represented by authorize_user.
End of explanation
"""


payload = {'jwt':'token'}
authorize_user(params=payload)

"""
Explanation: 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.
End of explanation
"""


from gobble.api import request_upload, handle

payload = {'bad_payload': 'I am bad'}
response = request_upload(params=payload)

handle(response)

"""
Explanation: Handle responses
You can use the handle wrapper function to handle your requests properly.
End of explanation
"""


from json import dumps

print(request_upload.snapshot.json)

"""
Explanation: Snapshots of responses
You can also inpect the transaction in more detail by looking at the snapshot attribute, which stores the last request. The SnapShot class is a subclass of collections.OrderedDict and has a json attribute for easy formatting.
End of explanation
"""


from gobble.api import EndPoint

leap_into_the_unknown = EndPoint('GET', 'I', 'do', 'not', 'exist')
leap_into_the_unknown()

cd ~/.gobble

"""
Explanation: Create new endpoints
Let's try and hit an endpoint that doesn't exist.
End of explanation
"""


