8000 Merge pull request #297 from GoogleCloudPlatform/xmpp · scottvw/python-docs-samples@7b253c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b253c2

Browse files
committed
Merge pull request GoogleCloudPlatform#297 from GoogleCloudPlatform/xmpp
Add XMPP Sample
2 parents fbe7dc0 + 2adb1c9 commit 7b253c2

File tree

6 files changed

+169
-0
lines changed

6 files changed

+169
-0
lines changed

appengine/xmpp/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Google App Engine XMPP
2+
3+
This sample includes snippets used in the [App Engine XMPP Docs](https://cloud.google.com/appengine/docs/python/xmpp/#Python_JIDs_and_resources).
4+
5+
<!-- auto-doc-link -->
6+
7+
8+
<!-- end-auto-doc-link -->

appengine/xmpp/app.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
runtime: python27
2+
threadsafe: yes
3+
api_version: 1
4+
5+
handlers:
6+
- url: .*
7+
script: xmpp.app
8+
9+
# [START inbound-services]
10+
inbound_services:
11+
- xmpp_message
12+
- xmpp_presence
13+
- xmpp_subscribe
14+
- xmpp_error
15+
# [END inbound-services]

appengine/xmpp/xmpp.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import logging
18+
19+
# [START xmpp-imports]
20+
from google.appengine.api import xmpp
21+
# [END xmpp-imports]
22+
import mock
23+
import webapp2
24+
25+
# Mock roster of users
26+
roster = mock.Mock()
27+
28+
29+
class SubscribeHandler(webapp2.RequestHandler):
30+
def post(self):
31+
# [START track]
32+
# Split the bare XMPP address (e.g., user@gmail.com)
33+
# from the resource (e.g., gmail), and then add the
34+
# address to the roster.
35+
sender = self.request.get('from').split('/')[0]
36+
roster.add_contact(sender)
37+
# [END track]
38+
39+
40+
class PresenceHandler(webapp2.RequestHandler):
41+
def post(self):
42+
# [START presence]
43+
# Split the bare XMPP address (e.g., user@gmail.com)
44+
# from the resource (e.g., gmail), and then add the
45+
# address to the roster.
46+
sender = self.request.get('from').split('/')[0]
47+
roster.add_contact(sender)
48+
# [END presence]
49+
50+
51+
class SendPresenceHandler(webapp2.RequestHandler):
52+
def post(self):
53+
# [START send-presence]
54+
jid = self.request.get('jid')
55+
xmpp.send_presence(jid, status="My app's status")
56+
# [END send-presence]
57+
58+
59+
class ErrorHandler(webapp2.RequestHandler):
60+
def post(self):
61+
# [START error]
62+
# In the handler for _ah/xmpp/error
63+
# Log an error
64+
error_sender = self.request.get('from')
65+
error_stanza = self.request.get('stanza')
66+
logging.error('XMPP error received from {} ({})'
67+
.format(error_sender, error_stanza))
68+
# [END error]
69+
70+
71+
# [START chat]
72+
class XMPPHandler(webapp2.RequestHandler):
73+
def post(self):
74+
print "REQUEST POST IS %s " % self.request.POST
75+
message = xmpp.Message(self.request.POST)
76+
if message.body[0:5].lower() == 'hello':
77+
message.reply("Greetings!")
78+
# [END chat]
79+
80+
app = webapp2.WSGIApplication([
81+
('/_ah/xmpp/message/chat/', XMPPHandler),
82+
('/_ah/xmpp/subscribe', SubscribeHandler),
83+
('/_ah/xmpp/presence/available', PresenceHa A3E2 ndler),
84+
('/_ah/xmpp/error/', ErrorHandler),
85+
('/send_presence', SendPresenceHandler),
86+
])

appengine/xmpp/xmpp_test.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import mock
16+
import pytest
17+
import webtest
18+
import xmpp
19+
20+
21+
@pytest.fixture
22+
def app(testbed):
23+
return webtest.TestApp(xmpp.app)
24+
25+
26+
@mock.patch('xmpp.xmpp')
27+
def test_chat(xmpp_mock, app):
28+
app.post('/_ah/xmpp/message/chat/', {
29+
'from': 'sender@example.com',
30+
'to': 'recipient@example.com',
31+
'body': 'hello',
32+
})
33+
34+
35+
@mock.patch('xmpp.xmpp')
36+
def test_subscribe(xmpp_mock, app):
37+
app.post('/_ah/xmpp/subscribe')
38+
39+
40+
@mock.patch('xmpp.xmpp')
41+
def test_check_presence(xmpp_mock, app):
42+
43+
app.post('/_ah/xmpp/presence/available', {
44+
'from': 'sender@example.com'
45+
})
46+
47+
48+
@mock.patch('xmpp.xmpp')
49+
def test_send_presence(xmpp_mock, app):
50+
app.post('/send_presence', {
51+
'jid': 'node@domain/resource'
52+
})
53+
54+
55+
@mock.patch('xmpp.xmpp')
56+
def test_error(xmpp_mock, app):
57+
app.post('/_ah/xmpp/error/', {
58+
'from': 'sender@example.com',
59+
'stanza': 'hello world'
60+
})

dataproc/README.md

Whitespace-only changes.

dataproc/create_cluster.py

Whitespace-only changes.

0 commit comments

Comments
 (0)
0