Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/voltane-eu/djpykafka
https://github.com/voltane-eu/djpykafka
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/voltane-eu/djpykafka
- Owner: Voltane-EU
- License: lgpl-2.1
- Created: 2022-05-04T15:21:14.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-12-11T17:18:22.000Z (about 1 year ago)
- Last Synced: 2024-11-06T09:03:43.633Z (3 months ago)
- Language: Python
- Size: 60.5 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# djpykafka
## 🚧 This project is WIP and is subject to change at any time
This project is currently in the alpha state, even though it can be used in production with some caution. Make sure to fix the version in your requirements.txt and review changes frequently.
## Installation
`pip install djpykafka`
## Examples
Add the `KafkaPublishMixin` to your model class.
```python
from django.db import models
from djpykafka.models import KafkaPublishMixinclass MyModel(KafkaPublishMixin, models.Model):
...
```Create a publisher class, e.g. at `events/publish/{modelname_plural}.py`
```python
from djpykafka.events.publish import EventPublisher, DataChangePublisher
from ... import models
from ...schemas import response
from . import connectionclass MyModelPublisher(
DataChangePublisher,
EventPublisher,
orm_model=models.MyModel,
event_schema=response.MyModel,
connection=connection,
topic='bizberry.access.users',
data_type='access.user',
):
pass
```## AsyncAPI Docs
This feature is WIP.
Install djpykafka with the extra `docs` (`pip install djpykafka[docs]`) and add the following snippets to your `asgi.py`:
```python
from starlette.responses import HTMLResponse, JSONResponse
from starlette.routing import Route
from fastapi.encoders import jsonable_encoder
from asyncapi_docgen.docs import get_asyncapi_ui_html# ! Import after django's get_asgi_application()
from djpykafka.docs.asyncapi import get_asyncapi
def asyncapi_html(request):
return HTMLResponse(get_asyncapi_ui_html(asyncapi_url='/asyncapi.json'))def asyncapi_json(request):
return JSONResponse(jsonable_encoder(get_asyncapi(), by_alias=True, exclude_none=True))app = FastAPI(
...,
routes=[
Route('/asyncapi', asyncapi_html),
Route('/asyncapi.json', asyncapi_json),
],
)
```