Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
berrytern committed Oct 3, 2024
1 parent c02d91d commit 05603dc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 84 deletions.
6 changes: 3 additions & 3 deletions examples/async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def handle(body):


async def handle2(body):
print(f"body: {body}")
print(f"body: {body}", flush=True)
return b"here"


Expand Down Expand Up @@ -55,10 +55,10 @@ async def run():
try:
count += 1
result = await eventbus.rpc_client(
rpc_exchange, "user.find", ["message_content"]
rpc_exchange, "user.find", "message_content"
)
print("returned:", result)
await eventbus.publish(rpc_exchange, "user.find3", ["message_content"])
await eventbus.publish(rpc_exchange, "user.find3", "message_content")
except BaseException as err:
print(f"err: {err}")

Expand Down
20 changes: 4 additions & 16 deletions examples/publish.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
from amqp_client_python import (
EventbusRabbitMQ,
Config, Options
)
from amqp_client_python.event import IntegrationEvent
from amqp_client_python import EventbusWrapperRabbitMQ, Config, Options
from examples.default import queue, rpc_queue, rpc_exchange, rpc_routing_key


class ExampleEvent(IntegrationEvent):
EVENT_NAME: str = "ExampleEvent"
def __init__(self, event_type: str, message = []) -> None:
super().__init__(self.EVENT_NAME, event_type)
self.message = message


config = Config(Options(queue, rpc_queue, rpc_exchange))
eventbus = EventbusRabbitMQ(config=config)
eventbus = EventbusWrapperRabbitMQ(config=config)


publish_event = ExampleEvent(rpc_exchange, ["message"])
print(eventbus.publish(publish_event, rpc_routing_key, "direct"))
# run this example with subscribe.py to see the complete cicle of sending and receiving messages.
print(eventbus.publish(rpc_exchange, rpc_routing_key, "direct").result())
# run this example with subscribe.py to see the complete cicle of sending and receiving messages.
11 changes: 6 additions & 5 deletions examples/rpc_publish.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# DEPRECATED EVENTBUS
from amqp_client_python import (
EventbusRabbitMQ,
Config, Options,
EventbusWrapperRabbitMQ,
Config,
Options,
)
from examples.default import queue, rpc_queue, rpc_exchange, rpc_routing_key


config = Config(Options(queue, rpc_queue, rpc_exchange))
eventbus = EventbusRabbitMQ(config=config)
eventbus = EventbusWrapperRabbitMQ(config=config)


print(eventbus.rpc_client(rpc_exchange, rpc_routing_key, ["hello2"]))
# run this example with rpc_subscribe.py to see the complete cicle of sending and receiving messages.
print(eventbus.rpc_client(rpc_exchange, rpc_routing_key, "hello2").result())
# run this example with rpc_subscribe.py to see the complete cicle of sending and receiving messages.
20 changes: 10 additions & 10 deletions examples/rpc_subscribe.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# DEPRECATED EVENTBUS
from amqp_client_python import (
EventbusRabbitMQ,
Config, Options
)
from typing import Union
from amqp_client_python import EventbusWrapperRabbitMQ, Config, Options
from examples.default import queue, rpc_queue, rpc_exchange, rpc_routing_key


config = Config(Options(queue, rpc_queue, rpc_exchange))
eventbus = EventbusRabbitMQ(config=config)
eventbus = EventbusWrapperRabbitMQ(config=config)

def handle(*body):
print(body)

def handle(body) -> Union[str, bytes]:
print(f"{body}", flush=True)
return b"result"

eventbus.provide_resource(rpc_routing_key, handle)
# run this example with rpc_publish.py to see the complete cicle of sending and receiving messages.


eventbus.provide_resource(rpc_routing_key, handle).result()
# run this example with rpc_publish.py to see the complete cicle of sending and receiving messages.
16 changes: 10 additions & 6 deletions examples/sanic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@

eventbus = None
app = Sanic("app")


@app.listener("before_server_start")
async def before_server_start(_, loop):
print("before_server_start")
global eventbus
eventbus = AsyncEventbusRabbitMQ(config, loop)
async def handle(*body):

async def handle(body):
print(f"body: {body}")
await eventbus.rpc_client(rpc_exchange, "user.find2", ["content_message"])
await eventbus.rpc_client(rpc_exchange, "user.find2", "content_message")
print("...")
return b"here"

async def handle2(*body):
async def handle2(body):
print(f"body: {body}")
return b"here"

await eventbus.provide_resource("user.find", handle)
await eventbus.provide_resource("user.find2", handle2)



@app.get(uri="/")
async def get(request):
global eventbus
result = await eventbus.rpc_client(rpc_exchange, "user.find", ["content_message"])
result = await eventbus.rpc_client(rpc_exchange, "user.find", "content_message")
return response.json({"message": str(result)})


if __name__ == "__main__":
app.run()
app.run()
36 changes: 22 additions & 14 deletions examples/ssl_connection.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
# DEPRECATED EVENTBUS
from amqp_client_python import (
EventbusRabbitMQ,
Config, Options,
SSLOptions
)
from examples.default import ( queue, rpc_queue, rpc_exchange, rpc_routing_key,
certfile_path, keyfile_path, ca_certs_path, port
from typing import Union
from amqp_client_python import EventbusWrapperRabbitMQ, Config, Options, SSLOptions
from examples.default import (
queue,
rpc_queue,
rpc_exchange,
rpc_routing_key,
certfile_path,
keyfile_path,
ca_certs_path,
port,
)


config = Config(Options(queue, rpc_queue, rpc_exchange, port=port), SSLOptions(certfile_path, keyfile_path, ca_certs_path))
eventbus = EventbusRabbitMQ(config=config)
config = Config(
Options(queue, rpc_queue, rpc_exchange, port=port),
SSLOptions(certfile_path, keyfile_path, ca_certs_path),
)
eventbus = EventbusWrapperRabbitMQ(config=config)


def handle(body):
print(body)
def handle(body) -> Union[str, bytes]:
print(f"{body}", flush=True)
return "result"
eventbus.provide_resource(rpc_routing_key, handle)
eventbus.start_rpc_server()


eventbus.provide_resource(rpc_routing_key, handle).result()
34 changes: 7 additions & 27 deletions examples/subscribe.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
# DEPRECATED
from amqp_client_python import (
EventbusRabbitMQ,
Config, Options
)
from amqp_client_python.event import IntegrationEvent, IntegrationEventHandler
from amqp_client_python import EventbusWrapperRabbitMQ, Config, Options
from examples.default import queue, rpc_queue, rpc_exchange, rpc_routing_key


class ExampleEvent(IntegrationEvent):
EVENT_NAME: str = "ExampleEvent"
ROUTING_KEY: str = rpc_routing_key

def __init__(self, event_type: str, message = []) -> None:
super().__init__(self.EVENT_NAME, event_type)
self.message = message
self.routing_key = self.ROUTING_KEY


class ExampleEventHandler(IntegrationEventHandler):
def handle(self, body) -> None:
print(body)
config = Config(Options(queue, rpc_queue, rpc_exchange))
eventbus = EventbusWrapperRabbitMQ(config=config)


config = Config(Options(queue, rpc_queue, rpc_exchange))
eventbus = EventbusRabbitMQ(config=config)
def handler(body) -> None:
print(f"{body}", flush=True)

def handle(*body):
print(body)
return "result"

subscribe_event = ExampleEvent(rpc_exchange)
subscribe_event_handle = ExampleEventHandler()
eventbus.subscribe(subscribe_event, subscribe_event_handle, rpc_routing_key)
# run this example with publish.py to see the complete cicle of sending and receiving messages.
eventbus.subscribe(rpc_exchange, rpc_routing_key, handler).result()
# run this example with publish.py to see the complete cicle of sending and receiving messages.
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ Source = "https://github.com/nutes-uepb/amqp-client-python"
python = "^3.7"
pika = "^1.3.0"

[tool.poetry.package-data]
amqp_client_python = ["py.typed"]

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
dependency-injector = "^4.40.0"
Expand Down

0 comments on commit 05603dc

Please sign in to comment.