Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 3 | # Copyright 2011 Google Inc. |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 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 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 24 | |
| 25 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 26 | |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 27 | import httplib2 |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 28 | import inspect |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 29 | import logging |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 30 | import os |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 31 | import pydoc |
| 32 | import re |
| 33 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 34 | import describe |
| 35 | import uritemplate |
| 36 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 37 | from apiclient import discovery |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 38 | from apiclient.errors import HttpError |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 39 | from google.appengine.api import memcache |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 40 | from google.appengine.ext import webapp |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 41 | from google.appengine.ext.webapp import template |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 42 | from google.appengine.ext.webapp import util |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 43 | from oauth2client.anyjson import simplejson |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 44 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 45 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 46 | DISCOVERY_URI = 'https://www.googleapis.com/discovery/v1/apis?preferred=true' |
| 47 | |
| 48 | |
| 49 | def get_directory_doc(): |
| 50 | http = httplib2.Http(memcache) |
| 51 | ip = os.environ.get('REMOTE_ADDR', None) |
| 52 | uri = DISCOVERY_URI |
| 53 | if ip: |
| 54 | uri += ('&userIp=' + ip) |
| 55 | resp, content = http.request(uri) |
| 56 | directory = simplejson.loads(content)['items'] |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 57 | for item in directory: |
| 58 | item['title'] = item.get('title', item.get('description', '')) |
| 59 | item['safe_version'] = describe.safe_version(item['version']) |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 60 | return directory |
| 61 | |
| 62 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 63 | class MainHandler(webapp.RequestHandler): |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 64 | """Handles serving the main landing page. |
| 65 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 66 | |
| 67 | def get(self): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 68 | directory = get_directory_doc() |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 69 | path = os.path.join(os.path.dirname(__file__), 'index.html') |
| 70 | self.response.out.write( |
| 71 | template.render( |
| 72 | path, {'directory': directory, |
| 73 | })) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 74 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 75 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 76 | class GadgetHandler(webapp.RequestHandler): |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 77 | """Handles serving the Google Gadget.""" |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 78 | |
| 79 | def get(self): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 80 | directory = get_directory_doc() |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 81 | path = os.path.join(os.path.dirname(__file__), 'gadget.html') |
| 82 | self.response.out.write( |
| 83 | template.render( |
| 84 | path, {'directory': directory, |
| 85 | })) |
| 86 | self.response.headers.add_header('Content-Type', 'application/xml') |
| 87 | |
| 88 | |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 89 | class EmbedHandler(webapp.RequestHandler): |
| 90 | """Handles serving a front page suitable for embedding.""" |
| 91 | |
| 92 | def get(self): |
| 93 | directory = get_directory_doc() |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 94 | path = os.path.join(os.path.dirname(__file__), 'embed.html') |
| 95 | self.response.out.write( |
| 96 | template.render( |
| 97 | path, {'directory': directory, |
| 98 | })) |
| 99 | |
| 100 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 101 | class ResourceHandler(webapp.RequestHandler): |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 102 | """Handles serving the PyDoc for a given collection. |
| 103 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 104 | |
| 105 | def get(self, service_name, version, collection): |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 106 | |
| 107 | real_version = describe.unsafe_version(version) |
| 108 | |
| 109 | logging.info('%s %s %s', service_name, version, collection) |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 110 | http = httplib2.Http(memcache) |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 111 | try: |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 112 | resource = discovery.build(service_name, real_version, http=http) |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 113 | except: |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 114 | logging.error('Failed to build service.') |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 115 | return self.error(404) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 116 | |
| 117 | DISCOVERY_URI = ('https://www.googleapis.com/discovery/v1/apis/' |
| 118 | '{api}/{apiVersion}/rest') |
| 119 | response, content = http.request( |
| 120 | uritemplate.expand( |
| 121 | DISCOVERY_URI, { |
| 122 | 'api': service_name, |
| 123 | 'apiVersion': real_version}) |
| 124 | ) |
| 125 | root_discovery = simplejson.loads(content) |
| 126 | collection_discovery = root_discovery |
| 127 | |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 128 | # descend the object path |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 129 | if collection: |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 130 | try: |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 131 | path = collection.split('.') |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 132 | if path: |
| 133 | for method in path: |
| 134 | resource = getattr(resource, method)() |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 135 | collection_discovery = collection_discovery['resources'][method] |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 136 | except: |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 137 | logging.error('Failed to parse the collections.') |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 138 | return self.error(404) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 139 | logging.info('Built everything successfully so far.') |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 140 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 141 | path = '%s_%s.' % (service_name, version) |
| 142 | if collection: |
| 143 | path += '.'.join(collection.split('/')) |
| 144 | path += '.' |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 145 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 146 | page = describe.document_collection( |
| 147 | resource, path, root_discovery, collection_discovery) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 148 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 149 | self.response.out.write(page) |
| 150 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 151 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 152 | def main(): |
| 153 | application = webapp.WSGIApplication( |
| 154 | [ |
| 155 | (r'/', MainHandler), |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 156 | (r'/_gadget/', GadgetHandler), |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 157 | (r'/_embed/', EmbedHandler), |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 158 | (r'/([^_]+)_([^\.]+)(?:\.(.*))?\.html$', ResourceHandler), |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 159 | ], |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 160 | debug=True) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 161 | util.run_wsgi_app(application) |
| 162 | |
| 163 | |
| 164 | if __name__ == '__main__': |
| 165 | main() |