-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add XMPP Sample #297
Merged
Add XMPP Sample #297
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Brilliant.