Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added basic API to create/join games and get channel IDs back

  • Loading branch information...
commit 822aafd7dec38573ff036e45f518e33dd5c033fc 1 parent f233e5c
@Arachnid authored
Showing with 77 additions and 0 deletions.
  1. +2 −0  app.yaml
  2. +41 −0 main.py
  3. +34 −0 models.py
View
2  app.yaml
@@ -6,6 +6,8 @@ api_version: 1
handlers:
- url: /static
static_dir: static
+- url: /.*
+ script: main.application
libraries:
- name: webob
View
41 main.py
@@ -0,0 +1,41 @@
+import uuid
+import webapp2
+from webapp2_extras import jinja2
+
+import models
+
+try:
+ import json as simplejson
+except ImportError:
+ from django.utils import simplejson
+
+
+class BaseHandler(webapp2.RequestHandler):
+ @webapp2.cached_property
+ def jinja2(self):
+ return jinja2.get_jinja2(app=self.app)
+
+ def render_template(self, filename, **template_args):
+ body = self.jinja2.render_template(filename, **template_args)
+ self.response.write(body)
+
+ def render_jsonp(self, response):
+ self.response.write("%s(%s);" % (self.request.GET['callback'],
+ simplejson.dumps(response)))
+
+
+class NewGameHandler(BaseHandler):
+ def get(self):
+ hangout_id = self.request.GET['hangout_id']
+ game = models.Game.get_or_insert(hangout_id)
+ plus_id = self.request.GET['plus_id']
+ participant = models.Participant.get_participant(game, plus_id)
+ response = {
+ 'channel_token': participant.channel_token,
+ }
+ self.render_jsonp(response)
+
+
+application = webapp2.WSGIApplication([
+ ('/api/join_game', NewGameHandler),
+], debug=True)
View
34 models.py
@@ -0,0 +1,34 @@
+from google.appengine.api import channel
+from ndb import model
+
+class Game(model.Model):
+ @property
+ def hangout_id(self):
+ return self.key().name()
+
+
+class Participant(model.Model):
+ # Child entity of the Game in which they are participating
+
+ @property
+ def plus_id(self):
+ """The user's Google+ ID."""
+ return self.key().name()
+
+ @classmethod
+ def get_participant(cls, game_key, plus_id):
+ if isinstance(game_key, Game):
+ game_key = game_key.key
+ def _tx():
+ participant = cls.get_by_id(plus_id, parent=game_key)
+ if not participant:
+ participant = cls(id=plus_id, parent=game_key)
+ participant.channel_id = str(participant.key)
+ participant.channel_token = channel.create_channel(
+ participant.channel_id)
+ participant.put()
+ return participant
+ return model.transaction(_tx)
+
+ channel_id = model.StringProperty(indexed=False)
+ channel_token = model.StringProperty(indexed=False)
Please sign in to comment.
Something went wrong with that request. Please try again.