-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from GoogleCloudPlatform/xmpp
Add XMPP Sample
- Loading branch information
Showing
6 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.