Composing request and response.

This commit is contained in:
Sangeeth Saravanaraj 2015-01-29 08:33:40 +05:30
parent e1f25604ec
commit a96f608469
6 changed files with 144 additions and 25 deletions

View file

@ -10,7 +10,8 @@
from sleekxmpp.plugins.base import register_plugin
from sleekxmpp.plugins.xep_0332 import stanza
from sleekxmpp.plugins.xep_0332.http import XEP_0332
# from sleekxmpp.plugins.xep_0332 import stanza
register_plugin(XEP_0332)

View file

@ -17,11 +17,7 @@ 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_0332.stanza import Request, Response, Data
from sleekxmpp.plugins.xep_0131.stanza import Headers
@ -35,8 +31,23 @@ class XEP_0332(BasePlugin):
name = 'xep_0332'
description = 'XEP-0332: HTTP over XMPP transport'
dependencies = set(['xep_0030', 'xep_0047', 'xep_0131']) #: xep 1, 137 and 166 are missing
default_config = {}
#: xep_0047 not included.
#: xep_0001, 0137 and 0166 are missing
dependencies = set(['xep_0030', 'xep_0131'])
#: TODO: Do we really need to mention the supported_headers?!
default_config = {
'supported_headers': set([
'Content-Length', 'Transfer-Encoding', 'DateTime',
'Accept-Charset', 'Location', 'Content-ID', 'Description',
'Content-Language', 'Content-Transfer-Encoding', 'Timestamp',
'Expires', 'User-Agent', 'Host', 'Proxy-Authorization', 'Date',
'WWW-Authenticate', 'Accept-Encoding', 'Server', 'Error-Info',
'Identifier', 'Content-Location', 'Content-Encoding', 'Distribute',
'Accept', 'Proxy-Authenticate', 'ETag', 'Expect', 'Content-Type'
])
}
def plugin_init(self):
log.debug("XEP_0332:: plugin_init()")
@ -48,30 +59,75 @@ class XEP_0332(BasePlugin):
'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)
register_stanza_plugin(Iq, Request, iterable=True)
register_stanza_plugin(Iq, Response, iterable=True)
register_stanza_plugin(Request, Headers, iterable=True)
register_stanza_plugin(Request, Data, iterable=True)
register_stanza_plugin(Response, Headers, iterable=True)
register_stanza_plugin(Response, Data, iterable=True)
# TODO: Should we register any api's here? self.api.register()
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)
self.xmpp['xep_0030'].del_feature('urn:xmpp:http')
for header in self.supported_headers:
self.xmpp['xep_0030'].del_feature(
feature='%s#%s' % (Headers.namespace, header)
)
def session_bind(self, jid):
log.debug("XEP_0332:: session_bind()")
self.xmpp['xep_0030'].add_feature(NAMESPACE)
self.xmpp['xep_0030'].add_feature('urn:xmpp:http')
for header in self.supported_headers:
self.xmpp['xep_0030'].add_feature(
'%s#%s' % (Headers.namespace, header)
)
# TODO: Do we need to add the supported headers to xep_0131?
# self.xmpp['xep_0131'].supported_headers.add(header)
def _handle_request(self):
def _handle_request(self, iq):
log.debug("XEP_0332:: _handle_request()")
print iq
def _handle_response(self):
def _handle_response(self, iq):
log.debug("XEP_0332:: _handle_response()")
print iq
def send_request(self, method=None, resource=None, headers=None, data=None, **kwargs):
def send_request(self, to=None, method=None, resource=None, headers=None,
data=None, **kwargs):
log.debug("XEP_0332:: send_request()")
iq = self.xmpp.Iq()
iq['from'] = self.xmpp.boundjid
iq['to'] = to
iq['type'] = 'set'
iq['req']['headers'] = headers
iq['req']['method'] = method
iq['req']['resource'] = resource
iq['req']['version'] = '1.1' # TODO: set this implicitly
iq['req']['data'] = data
print iq
# return iq.send(timeout=kwargs.get('timeout', None),
# block=kwargs.get('block', True),
# callback=kwargs.get('callback', None),
# timeout_callback=kwargs.get('timeout_callback', None))
def send_response(self, status_code=None, headers=None, data=None, **kwargs):
def send_response(self, to=None, code=None, headers=None, data=None,
**kwargs):
log.debug("XEP_0332:: send_response()")
iq = self.xmpp.Iq()
iq['from'] = self.xmpp.boundjid
iq['to'] = to
iq['type'] = 'result'
iq['resp']['headers'] = headers
iq['resp']['code'] = code
iq['resp']['version'] = '1.1' # TODO: set this implicitly
iq['resp']['data'] = data
print iq
# return iq.send(timeout=kwargs.get('timeout', None),
# block=kwargs.get('block', True),
# callback=kwargs.get('callback', None),
# timeout_callback=kwargs.get('timeout_callback', None))

View file

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

View file

@ -0,0 +1,29 @@
"""
SleekXMPP: The Sleek XMPP Library
Implementation of HTTP over XMPP transport
http://xmpp.org/extensions/xep-0332.html
Copyright (C) 2015 Riptide IO, sangeeth@riptideio.com
This file is part of SleekXMPP.
See the file LICENSE for copying permission.
"""
from sleekxmpp.xmlstream import ElementBase
class Data(ElementBase):
"""
The data element.
"""
name = 'data'
namespace = ''
interfaces = set(['data'])
plugin_attrib = 'data'
def get_data(self):
print "Data:: get_data()"
def set_data(self, data, encoding='text'):
print "Data:: set_data()"
self._set_sub_text(encoding, text=data)

View file

@ -9,7 +9,6 @@
"""
from sleekxmpp.xmlstream import ElementBase
from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE
class Request(ElementBase):
@ -43,7 +42,28 @@ class Request(ElementBase):
"""
name = 'request'
namespace = NAMESPACE
interfaces = set(('method', 'resource', 'version'))
namespace = 'urn:xmpp:http'
interfaces = set(['method', 'resource', 'version'])
plugin_attrib = 'req'
def get_method(self):
print "Request:: get_method()"
def set_method(self, method):
print "Request:: set_method()"
self._set_attr('method', method)
def get_resource(self):
print "Request:: get_resource()"
def set_resource(self, resource):
print "Request:: set_resource()"
self._set_attr('resource', resource)
def get_version(self):
print "Request:: get_version()"
def set_version(self, version='1.1'):
print "Request:: set_version()"
self._set_attr('version', version)

View file

@ -9,7 +9,6 @@
"""
from sleekxmpp.xmlstream import ElementBase
from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE
class Response(ElementBase):
@ -41,6 +40,18 @@ class Response(ElementBase):
"""
name = 'response'
namespace = NAMESPACE
interfaces = set(('statusCode', 'statusMessage', 'version'))
namespace = 'urn:xmpp:http'
interfaces = set(['code', 'version'])
plugin_attrib = 'resp'
def get_code(self):
print "Response:: get_code()"
def set_code(self, code):
print "Response:: set_code()"
self._set_attr('statusCode', str(code))
self._set_attr('statusMessage', str(code))
def set_version(self, version='1.1'):
print "Response:: set_version()"
self._set_attr('version', version)