Skip to content

Commit 9c8588a

Browse files
committed
Update api-python-client-doc to also serve up a gadget
1 parent b9e63ff commit 9c8588a

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Module>
3+
<ModulePrefs title="Google APIs" />
4+
<Content type="html">
5+
<![CDATA[
6+
<table>
7+
{% for item in directory %}
8+
<tr>
9+
<td><img style="width: 16px; height: 16px" src="{{ item.icons.x16 }}"/> {{ item.name }} </td>
10+
<td><a href="{{ item.documentationLink }}">Documentation</a></td>
11+
<td><a href="/{{ item.name }}/{{ item.version }}">PyDoc</a></td>
12+
</tr>
13+
{% endfor %}
14+
</table>
15+
]]>
16+
</Content>
17+
</Module>

samples/api-python-client-doc/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ <h1> Google API Client for Python Documentation </h1>
2222
<table>
2323
<tr>
2424
<th>API</th>
25+
<th>Documentation</th>
26+
<th>PyDoc</th>
2527
<th>Name</th>
2628
<th>Version</th>
2729
</tr>
2830
{% for item in directory %}
2931
<tr>
30-
<td><a href="/{{ item.name }}/{{ item.version }}"><img src="{{ item.icons.x16 }}"/> {{ item.title }} </a></td>
32+
<td><img src="{{ item.icons.x16 }}"/> {{ item.title }}</td>
33+
<td><a href="{{ item.documentationLink }}">Documentation</a></td>
34+
<td><a href="/{{ item.name }}/{{ item.version }}">PyDoc</a></td>
3135
<td>{{ item.name }}</td>
3236
<td>{{ item.version}}</td>
3337
</tr>

samples/api-python-client-doc/main.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
#
3-
# Copyright 2007 Google Inc.
3+
# Copyright 2011 Google Inc.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -14,6 +14,13 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17+
"""Sample application for Python documentation of APIs.
18+
19+
This is running live at http://api-python-client-doc.appspot.com where it
20+
provides a list of APIs and PyDoc documentation for all the generated API
21+
surfaces as they appear in the google-api-python-client. In addition it also
22+
provides a Google Gadget.
23+
"""
1724

1825
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
1926

@@ -23,15 +30,17 @@
2330
import pydoc
2431
import re
2532

26-
from apiclient.discovery import build
2733
from apiclient.anyjson import simplejson
34+
from apiclient import discovery
2835
from google.appengine.api import memcache
2936
from google.appengine.ext import webapp
3037
from google.appengine.ext.webapp import template
3138
from google.appengine.ext.webapp import util
3239

3340

3441
class MainHandler(webapp.RequestHandler):
42+
"""Handles serving the main landing page.
43+
"""
3544

3645
def get(self):
3746
http = httplib2.Http(memcache)
@@ -46,23 +55,45 @@ def get(self):
4655
}))
4756

4857

49-
def render(resource):
58+
class GadgetHandler(webapp.RequestHandler):
59+
"""Handles serving the Google Gadget.
60+
"""
61+
62+
def get(self):
63+
http = httplib2.Http(memcache)
64+
resp, content = http.request('https://www.googleapis.com/discovery/v1/apis?preferred=true')
65+
directory = simplejson.loads(content)['items']
66+
for item in directory:
67+
item['title'] = item.get('title', item.get('description', ''))
68+
path = os.path.join(os.path.dirname(__file__), 'gadget.html')
69+
self.response.out.write(
70+
template.render(
71+
path, {'directory': directory,
72+
}))
73+
self.response.headers.add_header('Content-Type', 'application/xml')
74+
75+
76+
def _render(resource):
77+
"""Use pydoc helpers on an instance to generate the help documentation.
78+
"""
5079
obj, name = pydoc.resolve(type(resource))
5180
return pydoc.html.page(
5281
pydoc.describe(obj), pydoc.html.document(obj, name))
5382

5483

5584
class ResourceHandler(webapp.RequestHandler):
85+
"""Handles serving the PyDoc for a given collection.
86+
"""
5687

5788
def get(self, service_name, version, collection):
58-
resource = build(service_name, version)
89+
resource = discovery.build(service_name, version)
5990
# descend the object path
6091
if collection:
6192
path = collection.split('/')
6293
if path:
6394
for method in path:
6495
resource = getattr(resource, method)()
65-
page = render(resource)
96+
page = _render(resource)
6697

6798
collections = []
6899
for name in dir(resource):
@@ -89,6 +120,7 @@ def main():
89120
application = webapp.WSGIApplication(
90121
[
91122
(r'/', MainHandler),
123+
(r'/_gadget/', GadgetHandler),
92124
(r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler),
93125
],
94126
debug=True)

0 commit comments

Comments
 (0)