Skip to content

Commit

Permalink
Merge pull request #12 from noisyboiler/0.7.6
Browse files Browse the repository at this point in the history
0.7.6
  • Loading branch information
noisyboiler authored Feb 10, 2017
2 parents 4c35eac + 8a0bbbf commit 561dbc6
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 28 deletions.
65 changes: 62 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ For the quickeststart I suggest that you use Crossbar.io and start it up on the

$ pip install --editable .[dev]

$ crossbar start --config ./test/crossbar.config.json
$ crossbar start --config ./wampy/testing/configs/crossbar.config.json

By default a **wampy** ``WebClient`` connects to localhost on port 8080, but this is of course configurable, and is done so on client initialisation.

Now open your preferred text editor and we'll write a few lies of Python constructing a simple WAMP service that takes a decimal number and returns the binary representation of it - fantastic stuff!

Wampy RPC
~~~~~~~~~

::

from wampy.peers.clients import Client
Expand Down Expand Up @@ -85,13 +88,69 @@ Note that the `Client` here is connecting to `localhost` and `8080`, but you cou

In [2]: from wampy.peers.routers import Crossbar

In [2]: with Client(router=Crossbar()) as client:
In [3]: with Client(router=Crossbar()) as client:
result = client.rpc.get_binary_number(number=100)

Please check out the full documentation at ReadTheDocs_ for more patterns.
Publishing and Subscribing is equally as simple
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To demonstrate, first of all you need a Subscriber. You can either create one yourself in a python module or use the example client in `docs.examples.services`. Here we use the given example service, but all a Subscriber is is a wampy Client with a method decorated by ``subscribe``.

Let's start up that example service.

::
$ wampy run docs.examples.services:SubscribingService --router http://localhost:8080

Now we have a service running that subscribes to the topic "foo".

In another terminal, with a wampy virtualenv, you can create a Publihser - which is no different to any other Client.

::

In [1]: from wampy.peers.clients import Client

In [2]: from wampy.peers.routers import Crossbar

In [3]: with Client(router=Crossbar()) as client:
client.publish(topic="foo", message="spam")

Hopefully you'll see any message you send printed to the screen where the example service is running. You'll also see the meta data that wampy chooses to send.

TLS Support (alpha)
~~~~~~~~~~~~~~~~~~

When you instantiate your Client, over-ride the default ``ws`` transport with "wss", e.g.

::

In [1]: from wampy.peers.clients import Client

In [2]: from wampy.peers.routers import Crossbar

In [3]: client = Client(router=Crossbar(), transport="wss")

Your certificates must be configured in your Crossbar.io config. For an example see ``crossbar.config.tls.json`` in the `testing` namespace. Also see ``test.test_transports.py``.


Thank you.


Testing
~~~~~~~

wampy provides some ``pytest`` fixtures and helpers for you to run a crossbar server. These are ``router``, ``tls_router`` and ``session_maker``.


Running the tests
~~~~~~~~~~~~~~~~~

::

$ pip install --editable .[dev]
$ py.test ./test -v


Build the docs
~~~~~~~~~~~~~~

Expand Down
39 changes: 27 additions & 12 deletions docs/examples/services.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import datetime

from wampy.peers.clients import DefaultClient
from wampy.roles import register_rpc
from wampy.peers.clients import Client
from wampy.roles.callee import register_rpc
from wampy.roles.subscriber import subscribe


class DateService(DefaultClient):
""" A web service that returns the current date.
class DateService(Client):
""" A service that returns the current date.
usage ::
$ wampy run docs.examples.services:DateService --router http://example.com:port
This service can then be called as such ::
e.g. ::
In [1]: from wampy.peers import WebClient
In [2]: with WebClient(name="wampy", host="examaple.com", port=port) as client:
...: date = client.rpc.get_todays_date()
In [3]: date
Out[3]: u'2016-11-26'
$ crossbar start --config ./wampy/testing/configs/crossbar.config.json
$ wampy run docs.examples.services:SubscribingService --router http://localhost:8080
"""
@register_rpc
def get_todays_date(self):
return datetime.date.today().isoformat()


class SubscribingService(Client):
""" A service that prints out "foo" topic messages
usage ::
$ wampy run docs.examples.services:SubscribingService --router http://example.com:port
e.g. ::
$ crossbar start --config ./wampy/testing/configs/crossbar.config.json
$ wampy run docs.examples.services:SubscribingService --router http://localhost:8080
"""

@subscribe(topic="foo")
def foo_handler(self, **kwargs):
print("foo message received: {}".format(kwargs))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='wampy',
version='0.7.5',
version='0.7.6',
description='WAMP RPC and Pub/Sub for python apps and microservices',
long_description=long_description,
url='https://github.com/noisyboiler/wampy',
Expand Down
4 changes: 4 additions & 0 deletions test/test_publishing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import pytest
from mock import ANY

Expand All @@ -8,6 +9,9 @@
from test.helpers import assert_stops_raising


logger = logging.getLogger('wampy.testing')


class SubscribingClient(Client):

call_count = 0
Expand Down
40 changes: 40 additions & 0 deletions test/test_transports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import datetime
from datetime import date

from wampy.peers.clients import Client
from wampy.roles.callee import rpc


class DateService(Client):

@rpc
def get_todays_date(self):
return datetime.date.today().isoformat()


def test_connection(router):
service = DateService(router=router, transport="ws")
with service:

client = Client(router=router, transport="ws")

with client:
result = client.rpc.get_todays_date()

today = date.today()

assert result == today.isoformat()


def test_secure_connection(tls_router):
service = DateService(router=tls_router, transport="wss")
with service:

client = Client(router=tls_router, transport="wss")

with client:
result = client.rpc.get_todays_date()

today = date.today()

assert result == today.isoformat()
2 changes: 1 addition & 1 deletion wampy/peers/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Client(object):

def __init__(
self, roles=DEFAULT_ROLES, realm=DEFAULT_REALM, router=Router(),
transport="websocket", id=None
transport="ws", id=None
):
self.roles = roles
self.realm = realm
Expand Down
2 changes: 1 addition & 1 deletion wampy/roles/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TopicSubscriber(object):

def __init__(
self, router, realm, topics, message_handler,
roles=None, transport="websocket",
roles=None, transport="ws",
):
""" Subscribe to a single topic.
Expand Down
22 changes: 14 additions & 8 deletions wampy/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@
from wampy.messages.handlers.default import DefaultMessageHandler
from wampy.messages.hello import Hello
from wampy.messages.goodbye import Goodbye
from wampy.transports.websocket.connection import WebSocket
from wampy.transports.websocket.connection import WebSocket, TLSWebSocket

from wampy.messages import MESSAGE_TYPE_MAP


logger = logging.getLogger('wampy.session')


def session_builder(client, router, realm, transport="websocket"):
if transport == "websocket":
transport = WebSocket(host=router.host, port=router.port)
return Session(
client=client, router=router, realm=realm, transport=transport
)
raise WampError("transport not supported: {}".format(transport))
def session_builder(client, router, realm, transport="ws"):
if transport == "ws":
transport = WebSocket(
host=router.host, port=router.port, websocket_location="ws")
elif transport == "wss":
transport = TLSWebSocket(
host=router.host, port=router.port, websocket_location="ws")
else:
raise WampError("transport not supported: {}".format(transport))

return Session(
client=client, router=router, realm=realm, transport=transport
)


class Session(object):
Expand Down
File renamed without changes.
59 changes: 59 additions & 0 deletions wampy/testing/configs/crossbar.config.tls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"version":2,
"workers":[
{
"type":"router",
"realms":[
{
"name":"realm1",
"roles":[
{
"name":"anonymous",
"permissions":[
{
"uri":"",
"match":"prefix",
"allow":{
"call":true,
"register":true,
"publish":true,
"subscribe":true
},
"disclose":{
"caller":false,
"publisher":false
},
"cache":true
}
]
}
]
}
],
"transports":[
{
"type":"web",
"endpoint":{
"type":"tcp",
"port":8080,
"tls":{
"key":"./wampy/testing/keys/server_key.pem",
"certificate":"./wampy/testing/keys/server_cert.pem",
"dhparam":"./wampy/testing/keys/dhparam.pem",
"ciphers":"ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!ADH:!AECDH:!MD5:!DSS"
}
},
"paths":{
"/":{
"type":"static",
"directory":"./wampy/testing/"
},
"ws":{
"type":"websocket"
}
}
}
]
}
]
}
5 changes: 5 additions & 0 deletions wampy/testing/keys/dhparam.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN DH PARAMETERS-----
MIGHAoGBALOSAAEXXAZLxNbZOw92qfwJtToq1eiVr8vF7jhVESvZXviE/nwHVYw3
IkbjFJDK+6kwH+KGpMGf4DYDraRpiArLOoveL8swh3WouKAEl0o5yoLwUi072W8j
SSPEGqRh+19r2HNo25/mgxF9ddMKFalGzURM2oTl4y+ZXaIt6GsTAgEC
-----END DH PARAMETERS-----
20 changes: 20 additions & 0 deletions wampy/testing/keys/server_cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDNjCCAh4CCQCP5LnGD/PkTDANBgkqhkiG9w0BAQUFADBdMQswCQYDVQQGEwJE
RTEQMA4GA1UECAwHQmF2YXJpYTERMA8GA1UEBwwIRXJsYW5nZW4xFTATBgNVBAoM
DFRhdmVuZG8gR21iSDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTE0MTEwNjIxMjIx
MloXDTI0MTEwMzIxMjIxMlowXTELMAkGA1UEBhMCREUxEDAOBgNVBAgMB0JhdmFy
aWExETAPBgNVBAcMCEVybGFuZ2VuMRUwEwYDVQQKDAxUYXZlbmRvIEdtYkgxEjAQ
BgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AK54iBXPWPyCGlsrGhBV1xkrfubttugoqADvfYP9eptzqOCzcAqtlWLFYf1tDGbT
mkwNsgV/lJKVzGMyosSAuRBmolRTQeyBcypsX9QJt6s/PJ2vh3v2QQ7DWlokZGhx
sCX6p6GEUbcBo61Yc+AvhAmJ+olYyLqp8nCIJFy0F+P7qvxx1B6pg08J/d75/m9A
BiGgZ0WjYLrDj0yERWX3WljuYIT4e34WMPofaBeiMTQEpqdgTbuSgpXdNdlzuWaq
gn8ITIHMnf5mlnCeU1rsHMJwJAUzRR9S40Km4WNxIP4/j+uVwMQLcJg73ni43n5V
xza6fgCQGfe8HQLxItwoU0MCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAEsiNnmRU
O4UW2AjI5EIr1Na7Xae8NZyIGrH+ONJUxaQxrpj1Q/bHmN7ZPFIDkZ2NX0rZBWwC
gbLSG0HIK2CdA+ghW0RaOnQgzLoJKJCERMSYDL26AXBJ4L0h5U43rsgXTD/zaWid
9B90pxSCXvSnA+yw1U67Lgm1x9LEkgpGWpqPlHQBMvwi4gkkdUsPxBLCie4TfX+p
gB51+Rjh8HaLl9tc0Z1JcjhK7ih1kz6A6XF7GaMvW0ipZYX/xIkUQNMTKhfdrrms
qr2GdKmJpFKJhrvYpbx560R0ZT2Sxic8EOLjfWfvxD56xTU6ssosw1oYdRB2W9i7
hsK8dBzPGj1ZcQ==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions wampy/testing/keys/server_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEArniIFc9Y/IIaWysaEFXXGSt+5u226CioAO99g/16m3Oo4LNw
Cq2VYsVh/W0MZtOaTA2yBX+UkpXMYzKixIC5EGaiVFNB7IFzKmxf1Am3qz88na+H
e/ZBDsNaWiRkaHGwJfqnoYRRtwGjrVhz4C+ECYn6iVjIuqnycIgkXLQX4/uq/HHU
HqmDTwn93vn+b0AGIaBnRaNgusOPTIRFZfdaWO5ghPh7fhYw+h9oF6IxNASmp2BN
u5KCld012XO5ZqqCfwhMgcyd/maWcJ5TWuwcwnAkBTNFH1LjQqbhY3Eg/j+P65XA
xAtwmDveeLjeflXHNrp+AJAZ97wdAvEi3ChTQwIDAQABAoIBAD0R+7CGr8NTVx5q
a+kj4vLwgq8arld9Q7FwIyM8atpXFdnxdVqHgC7eoHow3ZJwpyXP9WxvR/Y3cR+X
7dmSpTTUeCXELuM2PLWw0apK7HuI2xLnCimd/Q/J2aqL6omUoe/pWRv0URYaAM0A
lS738uPT5FqHNVwBeNdjEDdS4rnuHei1wYwWKlZ5aTRaO21g0Ya67fY7AKRFuKEn
TxGmgA/cKT+RCoztBSys1vY4vuBBhsZXBzSWjkQE/nh5FiZ/rDvmtPeBn2hjqKK2
IE3k6LOvkpwSRfzbR26lQv3PdEL0SFbGiqwfCm2Et77scL73uXS5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