Skip to main content

Pydiator

Project description

example event parameter Coverage Status

Installation

https://pypi.org/project/pydiator-core/

add your requirements.txt pydiator-core or run the pip install pydiator-core command

Examples

Directly : https://github.com/ozgurkara/pydiator-core/blob/master/examples/main.py

Fastapi : https://github.com/ozgurkara/fastapi-pydiator

What is the pydiator?

Pydiator is an in-app communication method.

It provides that developing the code as an aspect. Also, it supports clean architecture infrastructure

It is using design patterns such as chain of responsibility, mediator, singleton.

Pydiator provides which advantages to developers and project?

  • Testable
  • Use case support
  • Aspect programming (Authorization, Validation, Cache, Logging, Tracer etc.) support
  • Clean architecture
  • Expandable architecture via pipeline
  • Independent framework
  • SOLID principles
  • Has publisher subscriber infrastructure

pydiator

How it works?

Pydiator knows 4 object types. These are;

1- Request object

  • Is used for calling the use case.
  • It should be inherited from BaseRequest
 class GetSampleByIdRequest(BaseRequest):
     def __init__(self, id: int):
         self.id = id

2- Response object

  • Is used for returning from use case
  • It should be inherited from BaseResponse
class GetSampleByIdResponse(BaseResponse):
     def __init__(self, id: int, title: str):
         self.id = id
         self.title = title 

3- Use Case

  • Includes logic codes
  • It should be inherited from BaseHandler
  • It takes one parameter to handle. The parameter should be inherited BaseRequest
class GetSampleByIdUseCase(BaseHandler):
     async def handle(self, req: GetSampleByIdRequest):
         # related codes are here such as business
         return GetSampleByIdResponse(id=req.id, title="hello pydiatr")     

What is the relation between these 3 object types?

Every use case object only knows a request object

Every request object is only used by one use case object


How is the use case run?

Should be had a particular map between the request object and the use case object.

Mapping example;

    def set_up_pydiator():
        container = MediatrContainer()
        container.register_request(GetSampleByIdRequest, GetSampleByIdUseCase())
        #container.register_request(xRequest, xUseCase())
        pydiator.ready(container=container)

Calling example;

    await pydiator.send(GetByIdRequest(id=1))

or

    loop = asyncio.new_event_loop()
    response: GetByIdResponse = loop.run_until_complete(pydiator.send(GetByIdRequest(id=1)))
    loop.close()
    print(response.to_json())

4- Pipeline

The purpose of the pipeline is to manage the code as an aspect. For instance, you want to write a log for the request and the response of every use case. You can do it via a pipeline easily. You can see the sample log pipeline at this link.

You can create a lot of pipelines such as cache pipeline, validation pipeline, tracer pipeline, authorization pipeline etc.

Also, you can create the pipeline much as you want but you should not forget that every use case will be used in this pipeline.


You can add the pipeline to pipelines such as;

    def set_up_pydiator():
        container = MediatrContainer()        
        container.register_pipeline(LogPipeline())
        #container.register_pipeline(xPipeline())
        pydiator.ready(container=container)

How can you write custom pipeline?

  • Every pipeline should be inherited BasePipeline
  • Sample pipeline
    class SamplePipeline(BasePipeline):
        def __init__(self):
            pass

        async def handle(self, req: BaseRequest) -> object:

            # before executed pipeline and uce case

            response = await self.next().handle(req)

            # after executed next pipeline and use case            

            return response

How to use the publisher subscriber feature

What is the observer feature?

This feature runs as pub-sub design pattern.

What is the pub-sub pattern?

publish-subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead, categorize published messages into classes without knowledge of which subscribers if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.

How to use this pattern with the pydiator?

You can see the details that via this link https://github.com/ozgurkara/pydiator-core/blob/master/examples/pub_sub.py

def set_up_pydiator():
    container = MediatrContainer()
    container.register_notification(SamplePublisherRequest, [Sample1Subscriber(), Sample2Subscriber(),
                                                             Sample3Subscriber()])
    pydiator.ready(container=container)

if __name__ == "__main__":
    set_up_pydiator()
    loop = asyncio.new_event_loop()
    loop.run_until_complete(pydiator.publish(SamplePublisherRequest(id=1)))
    loop.close()

How to run the Unit Tests

install tests/requirements.txt

pytest tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydiator-core-1.0.8.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

pydiator_core-1.0.8-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file pydiator-core-1.0.8.tar.gz.

File metadata

  • Download URL: pydiator-core-1.0.8.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for pydiator-core-1.0.8.tar.gz
Algorithm Hash digest
SHA256 ac7727b0024daf461a0adc48d90c2b1b6234c6ba4d054cd095e02446d1a25a41
MD5 0257119e56c7e61415f2ea1189271c6e
BLAKE2b-256 fc6f296c967489cc1728536c67c72fde7260e3f7b9e8325c6d437ea6b0e6f6ef

See more details on using hashes here.

File details

Details for the file pydiator_core-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: pydiator_core-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for pydiator_core-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 b4bcc6a69f24890a34a135c1420554e4bb7ddddacfdb68162aeaf30d68b49f18
MD5 f3003e649a533268862b09fe2d8070cb
BLAKE2b-256 98ac3d381bffe17751e67b11e65ddb324d854a154728fd5b8609982f7c47dbb9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page