From 46b0ff63e7579ab4e8bf50a37a56818efad0e52e Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Sat, 9 Oct 2010 22:13:12 -0400 Subject: [PATCH] Expanded Moderator sample. Also made JsonBody more robust, allowing the external 'data' wrapper to be passed in from the caller or added, depending on whether it is present or not. Also handle adding user-agents better, by concatenating them. --- apiclient/contrib/moderator/future.json | 2 +- apiclient/discovery.py | 5 ++- apiclient/oauth.py | 7 ++-- samples/moderator/moderator.py | 44 ++++++++++++++++++++++++- samples/moderator/three_legged_dance.py | 1 + 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/apiclient/contrib/moderator/future.json b/apiclient/contrib/moderator/future.json index 7e3d978..87d525b 100644 --- a/apiclient/contrib/moderator/future.json +++ b/apiclient/contrib/moderator/future.json @@ -2,7 +2,7 @@ "data": { "moderator": { "v1": { - "baseUrl": "https://www.googleapis.com/", + "baseUrl": "https://www.googleapis.com/", "auth": { "request": { "url": "https://www.google.com/accounts/OAuthGetRequestToken", diff --git a/apiclient/discovery.py b/apiclient/discovery.py index e71e990..db17cea 100644 --- a/apiclient/discovery.py +++ b/apiclient/discovery.py @@ -94,7 +94,10 @@ class JsonModel(object): if body_value is None: return (headers, path_params, query, None) else: - model = {'data': body_value} + if len(body_value) == 1 and 'data' in body_value: + model = body_value + else: + model = {'data': body_value} headers['content-type'] = 'application/json' return (headers, path_params, query, simplejson.dumps(model)) diff --git a/apiclient/oauth.py b/apiclient/oauth.py index 2016dea..de20336 100644 --- a/apiclient/oauth.py +++ b/apiclient/oauth.py @@ -130,8 +130,11 @@ class OAuthCredentials(Credentials): if headers == None: headers = {} headers.update(req.to_header()) - if 'user-agent' not in headers: - headers['user-agent'] = self.user_agent + if 'user-agent' in headers: + headers['user-agent'] += ' ' + else: + headers['user-agent'] = '' + headers['user-agent'] += self.user_agent return request_orig(uri, method, body, headers, redirections, connection_type) diff --git a/samples/moderator/moderator.py b/samples/moderator/moderator.py index 36f354a..3d742e8 100644 --- a/samples/moderator/moderator.py +++ b/samples/moderator/moderator.py @@ -17,6 +17,8 @@ from apiclient.discovery import build import httplib2 import pickle +# Uncomment to get detailed logging +# httplib2.debuglevel = 4 def main(): f = open("moderator.dat", "r") @@ -27,7 +29,47 @@ def main(): http = credentials.authorize(http) p = build("moderator", "v1", http=http) - print p.submissions().list(seriesId="7035", topicId="64").execute() + + series_body = { + "description": "Share and rank tips for eating healthily on the cheaps!", + "name": "Eating Healthy & Cheap", + "videoSubmissionAllowed": False + } + series = p.series().insert(body=series_body).execute() + print "Created a new series" + + topic_body = { + "data": { + "description": "Share your ideas on eating healthy!", + "name": "Ideas", + "presenter": "liz" + } + } + topic = p.topics().insert(seriesId=series['id']['seriesId'], body=topic_body).execute() + print "Created a new topic" + + submission_body = { + "data": { + "attachmentUrl": "http://www.youtube.com/watch?v=1a1wyc5Xxpg", + "attribution": { + "displayName": "Bashan", + "location": "Bainbridge Island, WA" + }, + "text": "Charlie Ayers @ Google" + } + } + submission = p.submissions().insert(seriesId=topic['id']['seriesId'], + topicId=topic['id']['topicId'], body=submission_body).execute() + print "Inserted a new submisson on the topic" + + vote_body = { + "data": { + "vote": "PLUS" + } + } + p.votes().insert(seriesId=topic['id']['seriesId'], submissionId=submission['id']['submissionId'], body=vote_body) + print "Voted on the submission" + if __name__ == '__main__': main() diff --git a/samples/moderator/three_legged_dance.py b/samples/moderator/three_legged_dance.py index f09410d..fbc90ec 100644 --- a/samples/moderator/three_legged_dance.py +++ b/samples/moderator/three_legged_dance.py @@ -35,6 +35,7 @@ flow = FlowThreeLegged(moderator_discovery, user_agent='google-api-client-python-mdrtr-cmdline/1.0', domain='anonymous', scope='https://www.googleapis.com/auth/moderator', + #scope='tag:google.com,2010:auth/moderator', xoauth_displayname='Google API Client Example App') authorize_url = flow.step1_get_authorize_url()