Added callbacks, registered stanzas, added features, etc.

This commit is contained in:
Sangeeth Saravanaraj 2015-01-28 14:52:15 +05:30
parent 0fe057b5c3
commit e1f25604ec
5 changed files with 107 additions and 15 deletions

View File

@ -11,6 +11,6 @@
from sleekxmpp.plugins.base import register_plugin
from sleekxmpp.plugins.xep_0332.http import XEP_0332
from sleekxmpp.plugins.xep_0332 import stanza
# from sleekxmpp.plugins.xep_0332 import stanza
register_plugin(XEP_0332)

View File

@ -10,8 +10,20 @@
import logging
from sleekxmpp import Iq
from sleekxmpp.xmlstream import register_stanza_plugin
from sleekxmpp.xmlstream.handler import Callback
from sleekxmpp.xmlstream.matcher import StanzaPath
from sleekxmpp.plugins.base import BasePlugin
from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE
from sleekxmpp.plugins.xep_0332.stanza.request import Request
from sleekxmpp.plugins.xep_0332.stanza.response import Response
from sleekxmpp.plugins.xep_0131.stanza import Headers
log = logging.getLogger(__name__)
@ -29,9 +41,37 @@ class XEP_0332(BasePlugin):
def plugin_init(self):
log.debug("XEP_0332:: plugin_init()")
self.xmpp.register_handler(Callback(
'HTTP Request', StanzaPath('iq/req'), self._handle_request
))
self.xmpp.register_handler(Callback(
'HTTP Response', StanzaPath('iq/resp'), self._handle_response
))
register_stanza_plugin(Iq, Request)
register_stanza_plugin(Iq, Response)
register_stanza_plugin(Request, Headers)
register_stanza_plugin(Response, Headers)
def plugin_end(self):
log.debug("XEP_0332:: plugin_end()")
self.xmpp.remove_handler('HTTP Request')
self.xmpp.remove_handler('HTTP Response')
self.xmpp['xep_0030'].del_feature(NAMESPACE)
def session_bind(self, jid):
log.debug("XEP_0332:: session_bind()")
self.xmpp['xep_0030'].add_feature(NAMESPACE)
def _handle_request(self):
log.debug("XEP_0332:: _handle_request()")
def _handle_response(self):
log.debug("XEP_0332:: _handle_response()")
def send_request(self, method=None, resource=None, headers=None, data=None, **kwargs):
log.debug("XEP_0332:: send_request()")
def send_response(self, status_code=None, headers=None, data=None, **kwargs):
log.debug("XEP_0332:: send_response()")

View File

@ -8,5 +8,4 @@
See the file LICENSE for copying permission.
"""
from sleekxmpp.plugins.xep_0332.stanza.request import *
from sleekxmpp.plugins.xep_0332.stanza.response import *
NAMESPACE = 'urn:xmpp:http'

View File

@ -8,14 +8,42 @@
See the file LICENSE for copying permission.
"""
from sleekxmpp import Iq
from sleekxmpp.xmlstream import ElementBase, register_stanza_plugin
from sleekxmpp.plugins.xep_0131.stanza import Headers
from sleekxmpp.xmlstream import ElementBase
from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE
class Request(ElementBase):
pass
"""
All HTTP communication is done using the `Request`/`Response` paradigm.
Each HTTP Request is made sending an `iq` stanza containing a `req` element
to the server. Each `iq` stanza sent is of type `set`.
Examples:
<iq type='set' from='a@b.com/browser' to='x@y.com' id='1'>
<req xmlns='urn:xmpp:http' method='GET' resource='/api/user' version='1.1'>
<headers xmlns='http://jabber.org/protocol/shim'>
<header name='Host'>b.com</header>
</headers>
</req>
</iq>
<iq type='set' from='a@b.com/browser' to='x@y.com' id='2'>
<req xmlns='urn:xmpp:http' method='PUT' resource='/api/users' version='1.1'>
<headers xmlns='http://jabber.org/protocol/shim'>
<header name='Host'>b.com</header>
<header name='Content-Type'>text/html</header>
<header name='Content-Length'>...</header>
</headers>
<data>
<text>&lt;html&gt;&lt;header/&gt;&lt;body&gt;&lt;p&gt;Beautiful home page.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</text>
</data>
</req>
</iq>
"""
name = 'request'
namespace = NAMESPACE
interfaces = set(('method', 'resource', 'version'))
plugin_attrib = 'req'
register_stanza_plugin(Iq, Request)
register_stanza_plugin(Request, Headers)

View File

@ -8,14 +8,39 @@
See the file LICENSE for copying permission.
"""
from sleekxmpp import Iq
from sleekxmpp.xmlstream import ElementBase, register_stanza_plugin
from sleekxmpp.plugins.xep_0131.stanza import Headers
from sleekxmpp.xmlstream import ElementBase
from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE
class Response(ElementBase):
pass
"""
When the HTTP Server responds, it does so by sending an `iq` stanza
response (type=`result`) back to the client containing the `resp` element.
Since response are asynchronous, and since multiple requests may be active
at the same time, responses may be returned in a different order than the
in which the original requests were made.
register_stanza_plugin(Iq, Response)
register_stanza_plugin(Response, Headers)
Examples:
<iq type='result' from='httpserver@clayster.com' to='httpclient@clayster.com/browser' id='2'>
<resp xmlns='urn:xmpp:http' version='1.1' statusCode='200' statusMessage='OK'>
<headers xmlns='http://jabber.org/protocol/shim'>
<header name='Date'>Fri, 03 May 2013 16:39:54GMT-4</header>
<header name='Server'>Clayster</header>
<header name='Content-Type'>text/turtle</header>
<header name='Content-Length'>...</header>
<header name='Connection'>Close</header>
</headers>
<data>
<text>
...
</text>
</data>
</resp>
</iq>
"""
name = 'response'
namespace = NAMESPACE
interfaces = set(('statusCode', 'statusMessage', 'version'))
plugin_attrib = 'resp'