Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame^] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 5 | |
| 6 | """One-line documentation for discovery module. |
| 7 | |
| 8 | A detailed description of discovery. |
| 9 | """ |
| 10 | |
| 11 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 12 | |
| 13 | # TODO |
| 14 | # - Add normalize_ that converts max-results into MaxResults |
| 15 | # |
| 16 | # - Each 'resource' should be its own object accessible |
| 17 | # from the service object returned from discovery. |
| 18 | # |
| 19 | # - Methods can either execute immediately or return |
| 20 | # RestRequest objects which can be batched. |
| 21 | # |
| 22 | # - 'Body' parameter for non-GET requests |
| 23 | # |
| 24 | # - 2.x and 3.x compatible |
| 25 | |
| 26 | # JS also has the idea of a TransportRequest and a Transport. |
| 27 | # The Transport has a doRequest() method that takes a request |
| 28 | # and a callback function. |
| 29 | # |
| 30 | |
| 31 | |
| 32 | # Discovery doc notes |
| 33 | # - Which parameters are optional vs mandatory |
| 34 | # - Is pattern a regex? |
| 35 | # - Inconsistent naming max-results vs userId |
| 36 | |
| 37 | |
| 38 | from apiclient.discovery import build |
| 39 | |
| 40 | import httplib2 |
| 41 | import simplejson |
| 42 | import re |
| 43 | |
| 44 | import oauth2 as oauth |
| 45 | |
| 46 | def oauth_wrap(consumer, token, http): |
| 47 | """ |
| 48 | Args: |
| 49 | http - An instance of httplib2.Http |
| 50 | or something that acts like it. |
| 51 | |
| 52 | Returns: |
| 53 | A modified instance of http that was passed in. |
| 54 | |
| 55 | Example: |
| 56 | |
| 57 | h = httplib2.Http() |
| 58 | h = oauth_wrap(h) |
| 59 | |
| 60 | Grumble. You can't create a new OAuth |
| 61 | subclass of httplib2.Authenication because |
| 62 | it never gets passed the absolute URI, which is |
| 63 | needed for signing. So instead we have to overload |
| 64 | 'request' with a closure that adds in the |
| 65 | Authorization header and then calls the original version |
| 66 | of 'request()'. |
| 67 | """ |
| 68 | request_orig = http.request |
| 69 | signer = oauth.SignatureMethod_HMAC_SHA1() |
| 70 | |
| 71 | def new_request(uri, method="GET", body=None, headers=None, redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None): |
| 72 | """Modify the request headers to add the appropriate |
| 73 | Authorization header.""" |
| 74 | req = oauth.Request.from_consumer_and_token( |
| 75 | consumer, token, http_method=method, http_url=uri) |
| 76 | req.sign_request(signer, consumer, token) |
| 77 | if headers == None: |
| 78 | headers = {} |
| 79 | headers.update(req.to_header()) |
| 80 | headers['user-agent'] = 'jcgregorio-test-client' |
| 81 | return request_orig(uri, method, body, headers, redirections, connection_type) |
| 82 | |
| 83 | http.request = new_request |
| 84 | return http |
| 85 | |
| 86 | def get_wrapped_http(): |
| 87 | f = open("oauth_token.dat", "r") |
| 88 | oauth_params = simplejson.loads(f.read()) |
| 89 | |
| 90 | consumer = oauth.Consumer(oauth_params['consumer_key'], oauth_params['consumer_secret']) |
| 91 | token = oauth.Token(oauth_params['oauth_token'], oauth_params['oauth_token_secret']) |
| 92 | |
| 93 | # Create a simple monkeypatch for httplib2.Http.request |
| 94 | # just adds in the oauth authorization header and then calls |
| 95 | # the original request(). |
| 96 | http = httplib2.Http() |
| 97 | return oauth_wrap(consumer, token, http) |
| 98 | |
| 99 | |
| 100 | def main(): |
| 101 | http = get_wrapped_http() |
| 102 | p = build("buzz", "v1", http = http) |
| 103 | activities = p.activities() |
| 104 | activitylist = activities.list(scope='@self', userId='@me') |
| 105 | print activitylist['items'][0]['title'] |
| 106 | activities.insert(userId='@me', body={ |
| 107 | 'title': 'Testing insert', |
| 108 | 'object': { |
| 109 | 'content': u'Just a short note to show that insert is working. ☄', |
| 110 | 'type': 'note'} |
| 111 | } |
| 112 | ) |
| 113 | |
| 114 | if __name__ == '__main__': |
| 115 | main() |