Skip to main content

Pydiator

Project description

Installation

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

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

Examples

Console : 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?

  • Is testable
  • Has Use case support
  • Has Aspect programming (Authorization, Validation, Cache, Logging, Tracer etc.) support
  • Has Clean architecture support
  • Expandable architecture via pipeline
  • Is independent framework
  • Has SOLID principles

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 I 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 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.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

pydiator_core-1.0.3-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydiator-core-1.0.3.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for pydiator-core-1.0.3.tar.gz
Algorithm Hash digest
SHA256 a1cc68abc01441b9da3c401d3167a4e090aed8c06bb5d834a8921210209e19fd
MD5 b367a2a8fda425c20c2285ae5947b1fb
BLAKE2b-256 480670b378a0fe801e568f45b2fc69e2aeef9d5e625b790e86924d0c7d150bc0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydiator_core-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for pydiator_core-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4b3e572a7172383deec530a12a2b65c4fc2fab47ea0725eb6c79823ace052a5b
MD5 756c2010b15f3e31f463663d02cb9f6f
BLAKE2b-256 d563dc3103abc97e11bf0baabf04c41de21afe2db5aa0fa147b0bf818f15dee5

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