Add in the abilitiy to run from a local static (as opposed to cached) discovery document. Also added sample for same.

This commit is contained in:
Joe Gregorio
2011-01-12 11:36:11 -05:00
parent a703ec2b8b
commit 292b9b8ccd
4 changed files with 1596 additions and 6 deletions

View File

@@ -83,17 +83,48 @@ def build(serviceName, version,
serviceName, "future.json")
try:
f = file(fn, "r")
d = simplejson.load(f)
future = f.read()
f.close()
future = d['resources']
auth_discovery = d['auth']
except IOError:
future = None
return build_from_document(content, discoveryServiceUrl, future,
http, developerKey, model, requestBuilder)
def build_from_document(
service,
base,
future=None,
http=None,
developerKey=None,
model=JsonModel(),
requestBuilder=HttpRequest):
"""
Args:
service: string, discovery document
base: string, base URI for all HTTP requests, usually the discovery URI
future: string, discovery document with future capabilities
auth_discovery: dict, information about the authentication the API supports
http: httplib2.Http, An instance of httplib2.Http or something that acts like
it that HTTP requests will be made through.
developerKey: string, Key for controlling API usage, generated
from the API Console.
model: Model class instance that serializes and
de-serializes requests and responses.
requestBuilder: Takes an http request and packages it up to be executed.
"""
service = simplejson.loads(service)
base = urlparse.urljoin(base, service['restBasePath'])
resources = service['resources']
if future:
doc = simplejson.loads(future)
future = doc['resources']
auth_discovery = doc.get('auth', {})
else:
future = {}
auth_discovery = {}
base = urlparse.urljoin(discoveryServiceUrl, service['restBasePath'])
resources = service['resources']
class Service(object):
"""Top level interface for a service"""

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,90 @@
#!/usr/bin/python2.4
# -*- coding: utf-8 -*-
#
# Copyright 2010 Google Inc. All Rights Reserved.
"""Simple command-line example for Buzz.
Command-line application that retrieves the users
latest content and then adds a new entry.
"""
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
from apiclient.discovery import build_from_document
import httplib2
import pickle
import pprint
# Uncomment the next line to get very detailed logging
#httplib2.debuglevel = 4
def main():
# Load the credentials and authorize
f = open("buzz.dat", "r")
credentials = pickle.loads(f.read())
f.close()
http = httplib2.Http()
http = credentials.authorize(http)
# Load the local copy of the discovery document
f = file("buzz.json", "r")
discovery = f.read()
f.close()
# Optionally load a futures discovery document
f = file("../../apiclient/contrib/buzz/future.json", "r")
future = f.read()
f.close()
# Construct a service from the local documents
p = build_from_document(discovery,
base="https://www.googleapis.com/",
future=future,
http=http,
developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
activities = p.activities()
# Retrieve the first two activities
activitylist = activities.list(
max_results='2', scope='@self', userId='@me').execute()
print "Retrieved the first two activities"
# Retrieve the next two activities
if activitylist:
activitylist = activities.list_next(activitylist).execute()
print "Retrieved the next two activities"
# Add a new activity
new_activity_body = {
"data": {
'title': 'Testing insert',
'object': {
'content': u'Just a short note to show that insert is working. ☄',
'type': 'note'}
}
}
activity = activities.insert(userId='@me', body=new_activity_body).execute()
print "Added a new activity"
activitylist = activities.list(
max_results='2', scope='@self', userId='@me').execute()
# Add a comment to that activity
comment_body = {
"data": {
"content": "This is a comment"
}
}
item = activitylist['items'][0]
comment = p.comments().insert(
userId=item['actor']['id'], postId=item['id'], body=comment_body
).execute()
print 'Added a comment to the new activity'
pprint.pprint(comment)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,40 @@
# Copyright (C) 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Do the OAuth 1.0a three legged dance.
Do the OAuth 1.0a three legged dance for
a Buzz command line application. Store the generated
credentials in a common file that is used by
other example apps in the same directory.
"""
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
from apiclient.discovery import build
from apiclient.oauth import FlowThreeLegged
from apiclient.ext.authtools import run
buzz_discovery = build("buzz", "v1").auth_discovery()
flow = FlowThreeLegged(buzz_discovery,
consumer_key='anonymous',
consumer_secret='anonymous',
user_agent='google-api-client-python-buzz-cmdline/1.0',
domain='anonymous',
scope='https://www.googleapis.com/auth/buzz',
xoauth_displayname='Google API Client Example App')
run(flow, 'buzz.dat')