Skip to content

Commit

Permalink
Add XMPP Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Prin committed Apr 28, 2016
1 parent 2cdc3d6 commit f98ba0e
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
8 changes: 8 additions & 0 deletions appengine/xmpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Google App Engine XMPP

This sample includes snippets used in the [App Engine XMPP Docs](https://cloud.google.com/appengine/docs/python/xmpp/#Python_JIDs_and_resources).

<!-- auto-doc-link -->


<!-- end-auto-doc-link -->
15 changes: 15 additions & 0 deletions appengine/xmpp/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
runtime: python27
threadsafe: yes
api_version: 1

handlers:
- url: .*
script: xmpp.app

# [START inbound-services]
inbound_services:
- xmpp_message
- xmpp_presence
- xmpp_subscribe
- xmpp_error
# [END inbound-services]
86 changes: 86 additions & 0 deletions appengine/xmpp/xmpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python

# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

# [START xmpp-imports]
from google.appengine.api import xmpp
# [END xmpp-imports]
import mock
import webapp2

# Mock roster of users
roster = mock.Mock()


class SubscribeHandler(webapp2.RequestHandler):
def post(self):
# [START track]
# Split the bare XMPP address (e.g., user@gmail.com)
# from the resource (e.g., gmail), and then add the
# address to the roster.
sender = self.request.get('from').split('/')[0]
roster.add_contact(sender)
# [END track]


class PresenceHandler(webapp2.RequestHandler):
def post(self):
# [START presence]
# Split the bare XMPP address (e.g., user@gmail.com)
# from the resource (e.g., gmail), and then add the
# address to the roster.
sender = self.request.get('from').split('/')[0]
roster.add_contact(sender)
# [END presence]


class SendPresenceHandler(webapp2.RequestHandler):
def post(self):
# [START send-presence]
jid = self.request.get('jid')
xmpp.send_presence(jid, status="My app's status")
# [END send-presence]


class ErrorHandler(webapp2.RequestHandler):
def post(self):
# [START error]
# In the handler for _ah/xmpp/error
# Log an error
error_sender = self.request.get('from')
error_stanza = self.request.get('stanza')
logging.error('XMPP error received from {} ({})'
.format(error_sender, error_stanza))
# [END error]


# [START chat]
class XMPPHandler(webapp2.RequestHandler):
def post(self):
print "REQUEST POST IS %s " % self.request.POST
message = xmpp.Message(self.request.POST)
if message.body[0:5].lower() == 'hello':
message.reply("Greetings!")
# [END chat]

app = webapp2.WSGIApplication([
('/_ah/xmpp/message/chat/', XMPPHandler),
('/_ah/xmpp/subscribe', SubscribeHandler),
('/_ah/xmpp/presence/available', PresenceHandler),
('/_ah/xmpp/error/', ErrorHandler),
('/send_presence', SendPresenceHandler),
])
60 changes: 60 additions & 0 deletions appengine/xmpp/xmpp_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import pytest
import webtest
import xmpp


@pytest.fixture
def app(testbed):
return webtest.TestApp(xmpp.app)


@mock.patch('xmpp.xmpp')
def test_chat(xmpp_mock, app):
app.post('/_ah/xmpp/message/chat/', {
'from': 'sender@example.com',
'to': 'recipient@example.com',
'body': 'hello',
})


@mock.patch('xmpp.xmpp')
def test_subscribe(xmpp_mock, app):
app.post('/_ah/xmpp/subscribe')


@mock.patch('xmpp.xmpp')
def test_check_presence(xmpp_mock, app):

app.post('/_ah/xmpp/presence/available', {
'from': 'sender@example.com'
})


@mock.patch('xmpp.xmpp')
def test_send_presence(xmpp_mock, app):
app.post('/send_presence', {
'jid': 'node@domain/resource'
})


@mock.patch('xmpp.xmpp')
def test_error(xmpp_mock, app):
app.post('/_ah/xmpp/error/', {
'from': 'sender@example.com',
'stanza': 'hello world'
})
Empty file added dataproc/README.md
Empty file.
Empty file added dataproc/create_cluster.py
Empty file.

0 comments on commit f98ba0e

Please sign in to comment.