<s> """<STR_LIT>""" <EOL> import base64 <EOL> import json <EOL> import logging <EOL> import re <EOL> import urllib <EOL> from apiclient import errors <EOL> from google . appengine . api import memcache <EOL> from google . appengine . ext import ndb <EOL> import jinja2 <EOL> import webapp2 <EOL> import constants <EOL> import pubsub_utils <EOL> JINJA2 = jinja2 . Environment ( loader = jinja2 . FileSystemLoader ( '<STR_LIT>' ) , <EOL> extensions = [ '<STR_LIT>' ] , <EOL> variable_start_string = '<STR_LIT>' , <EOL> variable_end_string = '<STR_LIT>' , <EOL> autoescape = True ) <EOL> MAX_ITEM = <NUM_LIT:20> <EOL> MESSAGE_CACHE_KEY = '<STR_LIT>' <EOL> class PubSubMessage ( ndb . Model ) : <EOL> """<STR_LIT>""" <EOL> message = ndb . StringProperty ( ) <EOL> created_at = ndb . DateTimeProperty ( auto_now_add = True ) <EOL> class InitHandler ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request = None , response = None ) : <EOL> """<STR_LIT>""" <EOL> super ( InitHandler , self ) . __init__ ( request , response ) <EOL> self . client = pubsub_utils . get_client ( ) <EOL> self . _setup_topic ( ) <EOL> self . _setup_subscription ( ) <EOL> def _setup_topic ( self ) : <EOL> """<STR_LIT>""" <EOL> topic_name = pubsub_utils . get_full_topic_name ( ) <EOL> try : <EOL> self . client . projects ( ) . topics ( ) . get ( <EOL> topic = topic_name ) . execute ( ) <EOL> except errors . HttpError as e : <EOL> if e . resp . status == <NUM_LIT> : <EOL> self . client . projects ( ) . topics ( ) . create ( <EOL> name = topic_name , body = { } ) . execute ( ) <EOL> else : <EOL> logging . exception ( e ) <EOL> raise <EOL> def _setup_subscription ( self ) : <EOL> """<STR_LIT>""" <EOL> subscription_name = pubsub_utils . get_full_subscription_name ( ) <EOL> try : <EOL> self . client . projects ( ) . subscriptions ( ) . get ( <EOL> subscription = subscription_name ) . execute ( ) <EOL> except errors . HttpError as e : <EOL> if e . resp . status == <NUM_LIT> : <EOL> body = { <EOL> '<STR_LIT>' : pubsub_utils . get_full_topic_name ( ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : pubsub_utils . get_app_endpoint_url ( ) <EOL> } <EOL> } <EOL> self . client . projects ( ) . subscriptions ( ) . create ( <EOL> name = subscription_name , body = body ) . execute ( ) <EOL> else : <EOL> logging . exception ( e ) <EOL> raise <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> template = JINJA2 . get_template ( '<STR_LIT>' ) <EOL> endpoint_url = re . sub ( '<STR_LIT>' , '<STR_LIT>' , <EOL> pubsub_utils . get_app_endpoint_url ( ) ) <EOL> context = { <EOL> '<STR_LIT>' : pubsub_utils . get_project_id ( ) , <EOL> '<STR_LIT>' : pubsub_utils . get_app_topic_name ( ) , <EOL> '<STR_LIT>' : pubsub_utils . get_app_subscription_name ( ) , <EOL> '<STR_LIT>' : endpoint_url <EOL> } <EOL> self . response . write ( template . render ( context ) ) <EOL> class FetchMessages ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> messages = memcache . get ( MESSAGE_CACHE_KEY ) <EOL> if not messages : <EOL> messages = PubSubMessage . query ( ) . order ( <EOL> - PubSubMessage . created_at ) . fetch ( MAX_ITEM ) <EOL> memcache . add ( MESSAGE_CACHE_KEY , messages ) <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . response . write ( <EOL> json . dumps ( <EOL> [ message . message for message in messages ] ) ) <EOL> class SendMessage ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> client = pubsub_utils . get_client ( ) <EOL> message = self . request . get ( '<STR_LIT:message>' ) <EOL> if message : <EOL> topic_name = pubsub_utils . get_full_topic_name ( ) <EOL> body = { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:data>' : base64 . b64encode ( message . encode ( '<STR_LIT:utf-8>' ) ) <EOL> } ] <EOL> } <EOL> client . projects ( ) . topics ( ) . publish ( <EOL> topic = topic_name , body = body ) . execute ( ) <EOL> self . response . status = <NUM_LIT> <EOL> class ReceiveMessage ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def post ( self ) : <EOL> if constants . SUBSCRIPTION_UNIQUE_TOKEN != self . request . get ( '<STR_LIT>' ) : <EOL> self . response . status = <NUM_LIT> <EOL> return <EOL> logging . debug ( '<STR_LIT>' . format ( self . request . body ) ) <EOL> message = json . loads ( urllib . unquote ( self . request . body ) . rstrip ( '<STR_LIT:=>' ) ) <EOL> message_body = base64 . b64decode ( str ( message [ '<STR_LIT:message>' ] [ '<STR_LIT:data>' ] ) ) <EOL> pubsub_message = PubSubMessage ( message = message_body ) <EOL> pubsub_message . put ( ) <EOL> memcache . delete ( MESSAGE_CACHE_KEY ) <EOL> self . response . status = <NUM_LIT:200> <EOL> APPLICATION = webapp2 . WSGIApplication ( <EOL> [ <EOL> ( '<STR_LIT:/>' , InitHandler ) , <EOL> ( '<STR_LIT>' , FetchMessages ) , <EOL> ( '<STR_LIT>' , SendMessage ) , <EOL> ( '<STR_LIT>' , ReceiveMessage ) , <EOL> ] , debug = True ) </s>
