Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/im-cloud-spain-connectors/python-connect-business-transaction-middleware
Provides simple request-response middleware for business transactions.
https://github.com/im-cloud-spain-connectors/python-connect-business-transaction-middleware
business-transaction component connect middleware python
Last synced: 6 days ago
JSON representation
Provides simple request-response middleware for business transactions.
- Host: GitHub
- URL: https://github.com/im-cloud-spain-connectors/python-connect-business-transaction-middleware
- Owner: IM-Cloud-Spain-Connectors
- License: apache-2.0
- Created: 2023-01-13T09:03:32.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-03T11:10:14.000Z (about 1 year ago)
- Last Synced: 2023-11-03T12:23:53.482Z (about 1 year ago)
- Topics: business-transaction, component, connect, middleware, python
- Language: Python
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Python Connect Business Transaction Middleware
[![Test](https://github.com/othercodes/python-connect-business-transaction-middleware/actions/workflows/test.yml/badge.svg)](https://github.com/othercodes/python-connect-business-transaction-middleware/actions/workflows/test.yml)
Provides simple request-response style middleware for business transactions.
## Installation
The easiest way to install the Connect Business Transaction Middleware library is to get the latest version from PyPI:
```bash
# using poetry
poetry add rndi-connect-business-transaction-middleware
# using pip
pip install rndi-connect-business-transaction-middleware
```## Usage
Creating and using a business transaction middleware is very easy, you just need to declare a middleware:
```python
from typing import Optionalfrom connect.eaas.core.responses import BackgroundResponse
from rndi.connect.business_transactions.contracts import FnBackgroundExecutiondef middleware_sample(request: dict, nxt: Optional[FnBackgroundExecution] = None) -> BackgroundResponse:
print('Sample Before')
response = nxt(request)
print('Sample After')
return response
```The signature of a middle must be the following:
```python
Middleware = Callable[[dict, Optional[FnBackgroundExecution]], TBackgroundResponse]
```A middleware must accept a dictionary, commonly the Connect request dictionary, and an optional `FnBackgroundExecution`,
which is the next middleware or the actual transaction to be executed. The return type must be a valid
`BackgroundResponse`.Once we have our middleware ready we can use the `make_middleware_callstack` to wrap the transaction with our
middlewares, as you can see as follows:```python
from rndi.connect.business_transactions.adapters import prepare# Prepare the selected transaction.
transaction = prepare(SomeTransaction())# Instantiate the required middlewares.
middlewares = [
middleware_sample
]# Make the middleware callstack.
transaction = make_middleware_callstack(middlewares, transaction)# Transaction Execution.
response = transaction(request)
```