Changeset 10:a5e3c32d23ca


Ignore:
Timestamp:
Jan 3, 2008, 10:58:54 AM (14 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@34
Message:

Add unsubscribe method to PubSubClient?.

Author: ralphm
Fixes #4.

Location:
wokkel
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • wokkel/iwokkel.py

    r6 r10  
    1 # Copyright (c) 2003-2007 Ralph Meijer
     1# Copyright (c) 2003-2008 Ralph Meijer
    22# See LICENSE for details.
    33
     
    191191        """
    192192
     193    def unsubscribe(service, nodeIdentifier, subscriber):
     194        """
     195        Unsubscribe from a node with a given JID.
     196
     197        @param service: The publish-subscribe service entity.
     198        @type service: L{jid.JID}
     199        @param nodeIdentifier: Identifier of the node to unsubscribe from.
     200        @type nodeIdentifier: L{unicode}
     201        @param subscriber: JID to unsubscribe from the node.
     202        @type subscriber: L{jid.JID}
     203        @rtype: L{defer.Deferred}
     204        """
     205
    193206    def publish(service, nodeIdentifier, items=[]):
    194207        """
  • wokkel/pubsub.py

    r7 r10  
    11# -*- test-case-name: wokkel.test.test_pubsub -*-
    22#
    3 # Copyright (c) 2003-2007 Ralph Meijer
     3# Copyright (c) 2003-2008 Ralph Meijer
    44# See LICENSE for details.
    55
     
    7878
    7979
    80 
    8180class SubscriptionPending(Exception):
    8281    """
     
    146145                self.addChild(payload)
    147146
     147
    148148class PubSubRequest(xmlstream.IQ):
    149149    """
     
    168168
    169169    def send(self, to):
    170         destination = unicode(to)
     170        """
     171        Send out request.
     172
     173        Extends L{xmlstream.IQ.send} by requiring the C{to} parameter to be
     174        a L{JID} instance.
     175
     176        @param to: Entity to send the request to.
     177        @type to: L{JID}
     178        """
     179        destination = to.full()
    171180        return xmlstream.IQ.send(self, destination)
     181
    172182
    173183class CreateNode(PubSubRequest):
     
    178188        if node:
    179189            self.command["node"] = node
     190
    180191
    181192class DeleteNode(PubSubRequest):
     
    185196        self.command["node"] = node
    186197
     198
    187199class Subscribe(PubSubRequest):
    188200    verb = 'subscribe'
     
    193205        self.command["jid"] = subscriber.full()
    194206
     207
     208class Unsubscribe(PubSubRequest):
     209    verb = 'unsubscribe'
     210
     211    def __init__(self, xs, node, subscriber):
     212        PubSubRequest.__init__(self, xs)
     213        self.command["node"] = node
     214        self.command["jid"] = subscriber.full()
     215
     216
    195217class Publish(PubSubRequest):
    196218    verb = 'publish'
     
    208230
    209231        return item
     232
    210233
    211234class PubSubClient(XMPPHandler):
     
    270293        return request.send(service).addCallback(cb)
    271294
     295    def unsubscribe(self, service, nodeIdentifier, subscriber):
     296        request = Unsubscribe(self.xmlstream, nodeIdentifier, subscriber)
     297        return request.send(service)
     298
    272299    def publish(self, service, nodeIdentifier, items=[]):
    273300        request = Publish(self.xmlstream, nodeIdentifier)
     
    276303
    277304        return request.send(service)
     305
    278306
    279307class PubSubService(XMPPHandler, IQHandlerMixin):
  • wokkel/test/test_pubsub.py

    r6 r10  
    1 # Copyright (c) 2003-2007 Ralph Meijer
     1# Copyright (c) 2003-2008 Ralph Meijer
    22# See LICENSE for details.
    33
     
    1212
    1313from wokkel import pubsub
     14from wokkel.test.helpers import XmlStreamStub
     15
     16try:
     17    from twisted.words.protocols.jabber.xmlstream import toResponse
     18except ImportError:
     19    from wokkel.compat import toResponse
     20
     21NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
     22NS_PUBSUB_ERRORS = 'http://jabber.org/protocol/pubsub#errors'
     23
     24class PubSubClientTest(unittest.TestCase):
     25
     26    def setUp(self):
     27        self.stub = XmlStreamStub()
     28        self.protocol = pubsub.PubSubClient()
     29        self.protocol.xmlstream = self.stub.xmlstream
     30
     31    def test_unsubscribe(self):
     32        """
     33        Test sending unsubscription request.
     34        """
     35        d = self.protocol.unsubscribe(JID('pubsub.example.org'), 'test',
     36                                      JID('user@example.org'))
     37
     38        iq = self.stub.output[-1]
     39        self.assertEquals('pubsub.example.org', iq.getAttribute('to'))
     40        self.assertEquals('set', iq.getAttribute('type'))
     41        self.assertEquals('pubsub', iq.pubsub.name)
     42        self.assertEquals(NS_PUBSUB, iq.pubsub.uri)
     43        children = list(domish.generateElementsQNamed(iq.pubsub.children,
     44                                                      'unsubscribe', NS_PUBSUB))
     45        self.assertEquals(1, len(children))
     46        child = children[0]
     47        self.assertEquals('test', child['node'])
     48        self.assertEquals('user@example.org', child['jid'])
     49
     50        self.stub.send(toResponse(iq, 'result'))
     51        return d
     52
    1453
    1554class PubSubServiceTest(unittest.TestCase):
     
    2867        iq['to'] = 'pubsub.example.org'
    2968        iq['type'] = 'set'
    30         iq.addElement(('http://jabber.org/protocol/pubsub', 'pubsub'))
     69        iq.addElement((NS_PUBSUB, 'pubsub'))
    3170        iq.pubsub.addElement('publish')
    3271        handler.handleRequest(iq)
     
    4786        iq['from'] = 'user@example.org'
    4887        iq['to'] = 'pubsub.example.org'
    49         iq.addElement(('http://jabber.org/protocol/pubsub', 'pubsub'))
     88        iq.addElement((NS_PUBSUB, 'pubsub'))
    5089        iq.pubsub.addElement('publish')
    5190        iq.pubsub.publish['node'] = 'test'
     
    62101        iq['to'] = 'pubsub.example.org'
    63102        iq['type'] = 'get'
    64         iq.addElement(('http://jabber.org/protocol/pubsub', 'pubsub'))
     103        iq.addElement((NS_PUBSUB, 'pubsub'))
    65104        iq.pubsub.addElement('options')
    66105        handler.handleRequest(iq)
     
    69108        self.assertEquals('feature-not-implemented', e.condition)
    70109        self.assertEquals('unsupported', e.appCondition.name)
    71         self.assertEquals('http://jabber.org/protocol/pubsub#errors',
    72                           e.appCondition.uri)
     110        self.assertEquals(NS_PUBSUB_ERRORS, e.appCondition.uri)
Note: See TracChangeset for help on using the changeset viewer.