All Hail The New Python Library

One of the first things I wanted to do after joining the Context.IO team is to update the Python library to have 100% coverage of version 2.0 of the API. You can find it on our Github here. I encourage you to check it out and let me know what you think.

The only non-standard library that it’s dependent on is the Oauth2 lib. You can grab that here. Here’s how you instantiate the main object with your credentials:

import contextIO2

CONSUMER_KEY = 'YOUR_API_KEY'
CONSUMER_SECRET = 'YOUR_API_SECRET'

context_io = c.ContextIO(
  consumer_key=CONSUMER_KEY, 
  consumer_secret=CONSUMER_SECRET
)

The module is fully docstringed out, so you should be able to play around in the Python interpreter and help() objects and methods to see what all you can do. Here’s how you can query the API for an account.

accounts = context_io.get_accounts(email='johndoe@gmail.com')
# since we return a list, let's be sure we have a result
if accounts:
  account = accounts[0]

If you store the account id in your database, you can instantiate an account object without tapping the API this way:

account = c.Account(context_io, {'id': 'ACCOUNT_ID_HERE'})

You need to pass in the parent, so the url endpoint gets built correctly and the objects can figure out how to authenticate. For some of the resource objects, the parent will be the main context_io object. Most others take an account object as a parent. You can tell what the parent should be by checking out the documentation. You can pass in any parameters via a dictionary.

Most resource objects have a .get() method that’s used for updating itself. This is handy if you store things like message ids and only want to pull specific message bodies out. Check it out!

message = c.Message(account, {'message_id': 'MESSAGE_ID_GOES_HERE'})
# populate the message object with data from the API
message.get()
# pull the message body
message.get_body()
print message.body

That’s a fast introduction to the new library. Check it out and let me know what you think! Happy hacking.