convio.py

Python

Public Domain

Class for interacting with Convio REST API

Download (right click, save as, rename as appropriate)

Embed

Tags:

api class convio
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Python class for interacting with Convio RESTful API. This probably won't work with the entire API, but seems to thus far.
# Requires JSON, which is in the standard lib as of Python 2.6
# I'm gonna go ahead and call this an alpha. Example follows. Naturally, replace the asterisks with your credentials.

#	import convio


#	values = {'member_id':	'ga_17759452'}
#	c = convio.Convio(site='**********', api_key='********************', login_name='*********', login_password='******')
#	stuff = c.convio(method='getUser',dict=values)
#	pp.pprint(stuff)



import urllib, urllib2, json

class Convio:
    def __init__(self, site, api_key, login_name, login_password, v="1.0", suppress_response_codes="false"):
        self.site                       = 'https://secure3.convio.net/%s/site/SRConsAPI' % site
        self.api_key                    = api_key
        self.login_name                 = login_name
        self.login_password             = login_password
        self.v                          = v
        self.response_format            = "json"
        self.suppress_response_codes    = suppress_response_codes

    class Error(Exception):
        pass

    class APIError(Error):
        pass


    def convio(self, method, dict):
        url = self.site

        dict['method']                      = method
        dict['api_key']                     = self.api_key
        dict['v']                           = self.v
        dict['login_password']              = self.login_password
        dict['login_name']                  = self.login_name
        dict['response_format']             = self.response_format
        dict['suppress_response_codes']     = self.suppress_response_codes

        data                                = urllib.urlencode(dict)
        req                                 = urllib2.Request(url, data)
        try:
            response    =        urllib2.urlopen(req)
            response = json.loads(response.read())
            return response[response.keys()[0]]
        except urllib2.HTTPError, w:
            error = json.loads(w.read())['errorResponse']
            raise Convio.APIError(error['message'])