|
1 | | -#OX3-Python-API-Client - Python class to access OpenX Enterprise API |
2 | | -OX3-Python-API-Client is a small class to help demonstrate how to connect to the OpenX Enterprise API. It depends on the [simplegeo/python-oauth2](https://github.com/simplegeo/python-oauth2) module. |
| 1 | +# ox3apiclient |
3 | 2 |
|
4 | | -It currently supports Python 2.7 and will be updated to support Python 3.x. |
| 3 | +A small class to help connect to the OpenX Enterprise API. While it uses [oauth2](https://github.com/simplegeo/python-oauth2), |
| 4 | +it does not use [httplib2](http://code.google.com/p/httplib2/) as the transport due to issues with headers created by |
| 5 | +httplib2. Instead it uses urllib2 as the HTTP transport. |
| 6 | + |
| 7 | +It currently supports Python 2.4 - 2.7, with 3.x support comming in the future. |
| 8 | + |
| 9 | +Basic usage: |
5 | 10 |
|
6 | 11 | ````python |
7 | | -import datetime |
8 | 12 | import ox3apiclient |
9 | 13 |
|
10 | | -# User credentials |
11 | | -email_address = '' |
12 | | -password = '' |
13 | | - |
14 | | -# OAuth credentials. These will be supplied by your Account Manager or Support. |
15 | | -domain = '' |
16 | | -realm = '' |
17 | | -consumer_key = '' |
18 | | -consumer_secret = '' |
19 | | - |
20 | | - |
21 | | -ox = ox3apiclient.OX3APIClient(domain, realm, consumer_key, consumer_secret) |
22 | | - |
23 | | -# Step 1. Fetch temporary request token. |
24 | | -ox.fetch_request_token() |
25 | | - |
26 | | -# Step 2. Log in to SSO server and authorize token. |
27 | | -ox.authorize_token(email_address, password) |
28 | | - |
29 | | -# Step 3. Swap temporary request token for permanent access token. |
30 | | -# If you need to store the access token yourself you can do so with something |
31 | | -# similar to: |
32 | | -# token_str = ox.fetch_access_token() |
33 | | -# access_token = urlparse.parse_qs(token_str)['oauth_token'][0] |
34 | | -ox.fetch_access_token() |
35 | | - |
36 | | -# Step 4. Validate your access token. |
37 | | -# You'll more than likely want to call the validate_session method, but you can |
38 | | -# manually validate your access token if needed. You will be resonpsible for |
39 | | -# passing the requisite openx3_access_token for all successive API requests. A |
40 | | -# method might look like the following: |
41 | | -# token_str = ox.fetch_access_token() |
42 | | -# access_token = urlparse.parse_qs(token_str)['oauth_token'][0] |
43 | | -# cookie_header = {'Cookie': 'openx3_access_token=' + access_token} |
44 | | -# ox.request(url='http://youruidomain.com/ox/3.0/a/session/validate', |
45 | | -# method='PUT', |
46 | | -# headers=cookie_header) |
47 | | -ox.validate_session() |
48 | | - |
49 | | - |
50 | | -# Now that we have connected let's try making a few API requests. |
51 | | -# Print out account names. We use overload=medium to get more than just a |
52 | | -# listing of ids. |
53 | | -accounts = ox.get('/a/account?overload=medium') |
54 | | -for account in accounts: |
55 | | - msg = 'Account ID: %s, Account Name: %s' |
56 | | - print(msg % (account['id'], account['name'])) |
57 | | - |
58 | | -# We won't test object creation with accounts because they can't be deleted |
59 | | -# currently. Instead, we will create an order under an advertiser account. |
60 | | -account_id = 0 #<= Replace with a valid advertiser account id for your instance. |
61 | | - |
62 | | -if account_id: |
63 | | - |
64 | | - # You can check to see what fields are required for the create action. |
65 | | - # required_fields = ox.get('/a/order/requiredFields?action=create') |
66 | | - # print(required_fields) #=> {u'status': u'string', u'name': u'string', u'account_id': u'int', u'start_date': u'datetime'} |
67 | | - |
68 | | - # OX3APIClient methods accept Python dicts for data parameters, so we can |
69 | | - # define an order as a normal dict. |
70 | | - order = { |
71 | | - 'status': 'Active', |
72 | | - 'name': 'OX3APIClient Object Creation Test', |
73 | | - 'account_id': account_id, |
74 | | - 'start_date': datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'), |
75 | | - } |
76 | | - |
77 | | - new_order = ox.post('/a/order', data=order) |
78 | | - # print(order) #=> {u'id': 12345} |
79 | | - print('Created order id %s' % new_order['id']) |
80 | | - |
81 | | - # Let's get all the details on the order we just created. |
82 | | - existing_order = ox.get('/a/order/%s' % new_order['id']) |
83 | | - print(existing_order) |
84 | | - |
85 | | -# Log out. |
86 | | -ox.delete('/a/session') |
| 14 | +ox = ox3apiclient.client_from_file().logon() |
| 15 | + |
| 16 | +account_ids = ox.get('/a/account') |
| 17 | + |
| 18 | +order = { |
| 19 | + 'status': 'Active', |
| 20 | + 'name': 'OX3APIClient Object Creation Test', |
| 21 | + 'account_id': account_ids[0], |
| 22 | + 'start_date': '2012-08-22 00:00:00'} |
| 23 | + |
| 24 | +new_order = ox.post('/a/order', data=order) |
| 25 | + |
| 26 | +ox.delete('/a/order/%s' % new_order['id']) |
| 27 | + |
| 28 | +ox.logoff() |
| 29 | +```` |
| 30 | + |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +Install from [PyPi](http://pypi.python.org/pypi) with [pip](http://www.pip-installer.org/en/latest/index.html) |
| 35 | + |
| 36 | +```` |
| 37 | +$ pip install ox3apiclient |
| 38 | +```` |
| 39 | +This should install the [oauth2](https://github.com/simplegeo/python-oauth2) dependency, but you can manually install if needed. |
| 40 | +```` |
| 41 | +$ pip install oauth2 |
| 42 | +```` |
| 43 | + |
| 44 | +Note that Python 2.4 and 2.5 support requires simplejson. You will need |
| 45 | +simplejson 2.1.0 specifically for Python 2.4. You can install this version with: |
| 46 | +```` |
| 47 | +$ pip install simplejson==2.1.0 |
| 48 | +```` |
| 49 | + |
| 50 | + |
| 51 | +## Authentication |
| 52 | + |
| 53 | +The recommended method of authentication is to use `ox3apiclient.client_from_file`. |
| 54 | +By default this will look for a file named `.ox3rc` in the current current |
| 55 | +directory, but this can be overwritten by specifying a `file_path` parameter. The |
| 56 | +file should be in the following format: |
| 57 | + |
| 58 | +```` |
| 59 | +[ox3apiclient] |
| 60 | +envs= |
| 61 | + dev |
| 62 | + prod |
| 63 | +
|
| 64 | +[dev] |
| 65 | +email: you@example.com |
| 66 | +password: password123 |
| 67 | +domain: dev.uidomain.com |
| 68 | +realm: dev.uidomain_realm |
| 69 | +consumer_key: 1fc5c9ae... |
| 70 | +consumer_secret: 7c664d68... |
| 71 | +authorization_url: http://custom_sso.uidomain.com/api/index/initiate |
| 72 | +
|
| 73 | +[prod] |
| 74 | +email: you@example.com |
| 75 | +password: password123 |
| 76 | +domain: uidomain.com |
| 77 | +realm: uidomain_realm |
| 78 | +consumer_key: 1fc5c9ae... |
| 79 | +consumer_secret: 7c664d68... |
| 80 | +```` |
| 81 | + |
| 82 | +`ox3apiclient.client_from_file` will use the first `env` by default but this can |
| 83 | +be overwritten by setting the `env` parameter. If your email and password are set |
| 84 | +in `.ox3rc` you can simply chain a call to `logon()`. |
| 85 | + |
| 86 | +Alternatively you can set everything in your code. |
| 87 | +````python |
| 88 | +email = 'you@example.com' |
| 89 | +password = 'password123' |
| 90 | +domain = 'uidomain.com' |
| 91 | +realm = 'uidomain_realm' |
| 92 | +consumer_key = '1fc5c9ae...' |
| 93 | +consumer_secret = '7c664d68...' |
| 94 | + |
| 95 | +ox = ox3apiclient.Client( |
| 96 | + email=email, |
| 97 | + password=password, |
| 98 | + domain=domain, |
| 99 | + realm=realm, |
| 100 | + consumer_key=consumer_key, |
| 101 | + consumer_secret=consumer_secret) |
| 102 | + |
| 103 | +ox.logon(email, password) |
87 | 104 | ```` |
0 commit comments