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:

  1. import contextIO2
  6. context_io = c.ContextIO(
  7.   consumer_key=CONSUMER_KEY, 
  8.   consumer_secret=CONSUMER_SECRET
  9. )

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.

  1. accounts = context_io.get_accounts(email='')
  2. # since we return a list, let's be sure we have a result
  3. if accounts:
  4.   account = accounts[0]

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

  1. 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!

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

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