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 | |
| 20 | |
| 21 | import httplib2 |
| 22 | import logging |
| 23 | import os |
| 24 | import pydoc |
| 25 | import re |
| 26 | |
| 27 | from apiclient.discovery import build |
| 28 | |
| 29 | from google.appengine.api import users |
| 30 | from google.appengine.ext import webapp |
| 31 | from google.appengine.ext.webapp import template |
| 32 | from google.appengine.ext.webapp import util |
| 33 | |
| 34 | STEP2_URI = 'http://%s.appspot.com/auth_return' % os.environ['APPLICATION_ID'] |
| 35 | |
| 36 | class MainHandler(webapp.RequestHandler): |
| 37 | |
| 38 | def get(self): |
| 39 | self.response.out.write(""" |
| 40 | <ul> |
| 41 | <li><a href='/buzz/v1'>buzz</a> |
| 42 | <li><a href='/moderator/v1'>moderator</a> |
| 43 | <li><a href='/latitude/v1'>latitude</a> |
| 44 | </ul> |
| 45 | """) |
| 46 | |
| 47 | class ServiceHandler(webapp.RequestHandler): |
| 48 | |
| 49 | def get(self, service_name, version): |
| 50 | service = build(service_name, version) |
| 51 | page = "<pre>%s</pre>" % pydoc.plain(pydoc.render_doc(service)) |
| 52 | |
| 53 | collections = [] |
| 54 | for name in dir(service): |
| 55 | if not "_" in name and callable(getattr(service, name)): |
| 56 | collections.append(name) |
| 57 | |
| 58 | for name in collections: |
| 59 | page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, name), page) |
| 60 | |
| 61 | self.response.out.write(page) |
| 62 | |
| 63 | class CollectionHandler(webapp.RequestHandler): |
| 64 | |
| 65 | def get(self, service_name, version, collection): |
| 66 | service = build(service_name, version) |
| 67 | page = "<pre>%s</pre>" % pydoc.plain(pydoc.render_doc(getattr(service, collection)())) |
| 68 | self.response.out.write(page) |
| 69 | |
| 70 | def main(): |
| 71 | application = webapp.WSGIApplication( |
| 72 | [ |
| 73 | (r'/', MainHandler), |
| 74 | (r'/(\w*)/(\w*)', ServiceHandler), |
| 75 | (r'/(\w*)/(\w*)/(\w*)', CollectionHandler), |
| 76 | ], |
| 77 | debug=True) |
| 78 | util.run_wsgi_app(application) |
| 79 | |
| 80 | |
| 81 | if __name__ == '__main__': |
| 82 | main() |