summaryrefslogtreecommitdiffstats
path: root/remoting/client/appengine/api.py
blob: 959aaab9e969787cdc30b802c67c02e116d0fd3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python

# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""API endpoints to get around javascript's single-origin restriction."""

import logging

from django.utils import simplejson as json

from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp.util import login_required
import auth


class GetHostListHandler(webapp.RequestHandler):
  """Proxies the host-list handlers on the Chromoting directory."""
  @login_required
  def get(self):
    if not auth.HasOAuth2Tokens():
      self.response.set_status(403)
      self.response.headers['Content-Type'] = 'application/json'
      self.response.out.write(
          '{"error": { "code": -1, "message": "No OAuth2 token" }}')
      return
    result = urlfetch.fetch(
        url = 'https://www.googleapis.com/chromoting/v1/@me/hosts',
        method = urlfetch.GET,
        headers = {'Authorization': 'OAuth ' + auth.GetOAuth2AccessToken()})
    self.response.set_status(result.status_code)
    for i in result.headers:
      self.response.headers[i] = result.headers[i]
    self.response.out.write(result.content)


def main():
  application = webapp.WSGIApplication(
      [
      ('/api/get_host_list', GetHostListHandler)
      ],
      debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()