GithubHelp home page GithubHelp logo

rahenrique / events-protocol-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from guiabolso/events-protocol-python

0.0 0.0 0.0 203 KB

Library to be a Client and Server using event protocol

Home Page: https://events-protocol.readthedocs.io/en/stable/index.html

License: Apache License 2.0

Python 97.90% Makefile 2.10%

events-protocol-python's Introduction

events-protocol

PyPI version Documentation Status Actions Status License Code style: black PyPI - Python Version

Configurando para desenvolvimento local

Para fazer alterações na biblioteca localmente e passar pelos checks de formatação é necessário executar os seguintes passos:

Primeiramente, deve-se instalar o Homebrew.

Depois, podemos instalar o pipenv e o pyenv.

pip install pipenv
brew install pyenv

Então, deve-se buildar a biblioteca pelo Makefile, e depois de configurado o ambiente, deve-se garantir o funcionamento do pre-commit

make dev
pre-commit install

Com essa configuração aplicada, ao commitar a lib Black será acionada automaticamente para fazer o linting do código.

Caso o código falhe na formatação, será emitido um status "Failed", e a biblioteca é acionada para reformatar.

Então, basta só commitar novamente para registrar as mudanças, dessa vez com a formatação correta.

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)

events-protocol-python's People

Contributors

andretramos avatar gb-jairo avatar gbromulorosa avatar gsvdutra avatar laurybueno avatar mrenno avatar thepabloaguilar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.