Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """API endpoints to get around javascript's single-origin restriction.""" | 7 """API endpoints to get around javascript's single-origin restriction.""" |
| 8 | 8 |
| 9 import logging | 9 import logging |
| 10 | 10 |
| 11 from django.utils import simplejson as json | 11 from django.utils import simplejson as json |
| 12 | 12 |
| 13 import gdata.client | 13 from google.appengine.api import urlfetch |
| 14 | |
| 15 from google.appengine.ext import webapp | 14 from google.appengine.ext import webapp |
| 16 from google.appengine.ext.webapp import util | 15 from google.appengine.ext.webapp import util |
| 17 from google.appengine.ext.webapp.util import login_required | 16 from google.appengine.ext.webapp.util import login_required |
| 18 import auth | 17 import auth |
| 19 | 18 |
| 20 | 19 |
| 21 class GetXmppTokenHandler(webapp.RequestHandler): | 20 class GetXmppTokenHandler(webapp.RequestHandler): |
| 22 """Retrieves the user's XMPP token.""" | 21 """Retrieves the user's XMPP token.""" |
| 23 @login_required | 22 @login_required |
| 24 def get(self): | 23 def get(self): |
| 25 try: | 24 try: |
| 26 self.response.headers['Content-Type'] = 'application/json' | 25 self.response.headers['Content-Type'] = 'application/json' |
| 27 self.response.out.write( | 26 self.response.out.write( |
| 28 json.dumps({'xmpp_token': auth.GetXmppToken().token})) | 27 json.dumps({'xmpp_token': auth.GetXmppToken().token})) |
| 29 except auth.NotAuthenticated: | 28 except auth.NotAuthenticated: |
| 30 self.response.out.write('User has not authenticated') | 29 self.response.out.write('User has not authenticated') |
| 31 self.set_status(400) | 30 self.set_status(400) |
| 32 | 31 |
| 32 | |
| 33 class GetHostListHandler(webapp.RequestHandler): | 33 class GetHostListHandler(webapp.RequestHandler): |
| 34 """Proxies the host-list handlers on the Chromoting directory.""" | 34 """Proxies the host-list handlers on the Chromoting directory.""" |
| 35 @login_required | 35 @login_required |
| 36 def get(self): | 36 def get(self): |
| 37 try: | 37 result = urlfetch.fetch( |
| 38 client = gdata.client.GDClient() | 38 url = 'https://www.googleapis.com/chromoting/v1/@me/hosts', |
| 39 host_list_json = client.Request( | 39 method = urlfetch.GET, |
| 40 method='GET', | 40 headers = {'Authorization': 'OAuth ' + auth.GetAccessToken()}) |
| 41 uri="https://www.googleapis.com/chromoting/v1/@me/hosts", | 41 self.response.headers['Content-Type'] = 'application/json' |
|
Jamie
2011/05/20 16:46:47
In the previous code, the content-type was only se
awong
2011/05/20 20:37:38
Good catch. Actually, I'm just relaying the full
| |
| 42 converter=None, | 42 self.response.out.write(result.content) |
|
Jamie
2011/05/20 16:46:47
Does |write| write the data to the client immediat
awong
2011/05/20 20:37:38
I'm pretty sure it's not a streaming response. Bu
| |
| 43 desired_class=None, | 43 for i in result.headers: |
| 44 auth_token=auth.GetChromotingToken()) | 44 self.response.headers[i] = result.headers[i] |
| 45 self.response.headers['Content-Type'] = 'application/json' | 45 self.response.set_status(result.status_code) |
| 46 self.response.out.write(host_list_json.read()) | 46 |
| 47 except auth.NotAuthenticated: | |
| 48 self.response.out.write('User has not authenticated') | |
| 49 self.response.set_status(400) | |
| 50 except (gdata.client.Unauthorized, gdata.client.RequestError), inst: | |
| 51 self.response.out.write(inst.reason) | |
| 52 self.response.set_status(inst.status) | |
| 53 | 47 |
| 54 def main(): | 48 def main(): |
| 55 application = webapp.WSGIApplication( | 49 application = webapp.WSGIApplication( |
| 56 [ | 50 [ |
| 57 ('/api/get_xmpp_token', GetXmppTokenHandler), | 51 ('/api/get_xmpp_token', GetXmppTokenHandler), |
| 58 ('/api/get_host_list', GetHostListHandler) | 52 ('/api/get_host_list', GetHostListHandler) |
| 59 ], | 53 ], |
| 60 debug=True) | 54 debug=True) |
| 61 util.run_wsgi_app(application) | 55 util.run_wsgi_app(application) |
| 62 | 56 |
| 63 | 57 |
| 64 if __name__ == '__main__': | 58 if __name__ == '__main__': |
| 65 main() | 59 main() |
| OLD | NEW |