Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. |
| 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 | # |
| 17 | |
| 18 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 19 | |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 20 | import httplib2 |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 21 | import inspect |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 22 | import os |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 23 | import pydoc |
| 24 | import re |
| 25 | |
| 26 | from apiclient.discovery import build |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 27 | from apiclient.anyjson import simplejson |
| 28 | from google.appengine.api import memcache |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 29 | from google.appengine.ext import webapp |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 30 | from google.appengine.ext.webapp import template |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 31 | from google.appengine.ext.webapp import util |
| 32 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 33 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 34 | class MainHandler(webapp.RequestHandler): |
| 35 | |
| 36 | def get(self): |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 37 | http = httplib2.Http(memcache) |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame^] | 38 | resp, content = http.request('https://www.googleapis.com/discovery/v1/apis?preferred=true') |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 39 | directory = simplejson.loads(content)['items'] |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame^] | 40 | for item in directory: |
| 41 | item['title'] = item.get('title', item.get('description', '')) |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 42 | path = os.path.join(os.path.dirname(__file__), 'index.html') |
| 43 | self.response.out.write( |
| 44 | template.render( |
| 45 | path, {'directory': directory, |
| 46 | })) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 47 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 48 | |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 49 | def render(resource): |
| 50 | obj, name = pydoc.resolve(type(resource)) |
| 51 | return pydoc.html.page( |
| 52 | pydoc.describe(obj), pydoc.html.document(obj, name)) |
| 53 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 54 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 55 | class ResourceHandler(webapp.RequestHandler): |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 56 | |
| 57 | def get(self, service_name, version, collection): |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 58 | resource = build(service_name, version) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 59 | # descend the object path |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 60 | if collection: |
| 61 | path = collection.split('/') |
| 62 | if path: |
| 63 | for method in path: |
| 64 | resource = getattr(resource, method)() |
| 65 | page = render(resource) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 66 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 67 | collections = [] |
| 68 | for name in dir(resource): |
| 69 | if not "_" in name and callable(getattr(resource, name)) and hasattr( |
| 70 | getattr(resource, name), '__is_resource__'): |
| 71 | collections.append(name) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 72 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 73 | if collection is None: |
| 74 | collection_path = '' |
| 75 | else: |
| 76 | collection_path = collection + '/' |
| 77 | for name in collections: |
| 78 | page = re.sub('strong>(%s)<' % name, |
| 79 | r'strong><a href="/%s/%s/%s">\1</a><' % ( |
| 80 | service_name, version, collection_path + name), page) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 81 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 82 | # TODO(jcgregorio) breadcrumbs |
| 83 | # TODO(jcgregorio) sample code? |
| 84 | page = re.sub('<p>', r'<a href="/">Home</a><p>', page, 1) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 85 | self.response.out.write(page) |
| 86 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 87 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 88 | def main(): |
| 89 | application = webapp.WSGIApplication( |
| 90 | [ |
| 91 | (r'/', MainHandler), |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 92 | (r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler), |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 93 | ], |
| 94 | debug=True) |
| 95 | util.run_wsgi_app(application) |
| 96 | |
| 97 | |
| 98 | if __name__ == '__main__': |
| 99 | main() |