Library to be a Client and Server using event protocol
Project description
events-protocol
Como usar
Client
As informações essenciais para enviar o evento são: url, name, version e payload.
Apenas com estas informações já é possivel enviar um evento.
from events_protocol.client import EventClient
# Instancia o client
client = EventClient(url="http://example.com/events/")
# Exemplo passando apenas as informações essenciais
response = client.send_event(
name="event:example",
version=1,
payload={
"example": "example"
},
)
# Exemplo passando todas as informações
response = client.send_event(
name="event:example",
version=1,
id="9230c47c-3bcf-11ea-b77f-2e728ce88125",
flow_id="a47830ca-3bcf-11ea-a232-2e728ce88125",
payload={
"example": "example"
},
identity={
"userId": "USER_ID",
},
metadata={
"date": "00-00-0000",
},
timeout=1000,
)
Server
Um server é composto por handler, register e EventSchema.
Abaixo se encontra um exemplo de utilização.
from events_protocol.server.handler.event_handler_registry import EventRegister
from events_protocol.core.builder import EventBuilder, Event
from events_protocol.core.model.base import CamelPydanticMixin
from events_protocol.core.model.event import Event, ResponseEvent
from events_protocol.server.handler.event_handler import EventHandler
from events_protocol.server.parser.event_processor import EventProcessor
class MyEventSchema(CamelPydanticMixin):
example: str
class MyHandler(EventHandler):
_SCHEMA = MyEventSchema
@classmethod
def handle(cls, event: Event) -> ResponseEvent:
payload = cls.parse_event(event)
response = {"MyEventPayload": payload.example}
return EventBuilder.response_for(event, response)
class MyEventRegister(EventRegister):
event_name = "get:event:example"
event_version = 1
event_handler = MyHandler
MyEventRegister.register_event()
event_input = Event(
name="get:event:example",
version=1,
id="9230c47c-3bcf-11ea-b77f-2e728ce88125",
flow_id="a47830ca-3bcf-11ea-a232-2e728ce88125",
payload={"example": "example"},
identity={"userId": "USER_ID",},
metadata={"date": "00-00-0000",},
)
input_body = event_input.to_json()
## Apos todos eventos registrados, registre uma rota "/events" no seu framework web de preferência e processe o body utilizando o seguinte comando
response = EventProcessor.process_event(input_body)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
events-protocol-0.2.4.tar.gz
(18.1 kB
view details)
Built Distribution
File details
Details for the file events-protocol-0.2.4.tar.gz
.
File metadata
- Download URL: events-protocol-0.2.4.tar.gz
- Upload date:
- Size: 18.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7942df078455adb3b7f8b291dcd804cd569d7b3413854628c6dd3e5f9b1bec7 |
|
MD5 | 2f72854072e0b4cea3cb5a0840390715 |
|
BLAKE2b-256 | cce4d0545af76c06d0ee76b37b0f998dc92d6061b9282d162c10ff41b87637cb |
File details
Details for the file events_protocol-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: events_protocol-0.2.4-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52fb50b6b6423d45096cb69ccbe7e8282e212ffeda24a23b101c143f64ac4013 |
|
MD5 | ee02fe1fd55cef007f68f250b55a9bf9 |
|
BLAKE2b-256 | de32761820b8f1d9a3b8b89a7be727e80159d0b536f3a8e3b97928b3e941d113 |