Update Python pub_sub fastapi example (#635)

Signed-off-by: Michał Klich <michal@klichx.dev>
This commit is contained in:
Michael Klich 2022-05-06 22:05:55 +02:00 committed by GitHub
parent 3aea5f8ed5
commit 57fc9655cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 7 deletions

View File

@ -41,9 +41,8 @@ background: true
sleep: 10
-->
```bash
dapr run --app-id order-processor --components-path ../../../components/ --app-port 5001 -- python3 app.py
dapr run --app-id order-processor --components-path ../../../components/ --app-port 5001 -- uvicorn app:app --port 5001
```
<!-- END_STEP -->
@ -76,7 +75,7 @@ working_dir: ./checkout
background: true
sleep: 10
-->
```bash
dapr run --app-id checkout --components-path ../../../components/ -- python3 app.py
```

View File

@ -1,12 +1,26 @@
from fastapi import FastAPI
# from cloudevents.http import from_http
from dapr.ext.fastapi import DaprApp
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
dapr_app = DaprApp(app)
class CloudEvent(BaseModel):
datacontenttype: str
source: str
topic: str
pubsubname: str
data: dict
id: str
specversion: str
tracestate: str
type: str
traceid: str
# Dapr subscription routes orders topic to this route
@dapr_app.subscribe(pubsub='orderpubsub', topic='orders')
def orders_subscriber(detail):
print('got here')
def orders_subscriber(event: CloudEvent):
print('Subscriber received : %s' % event.data['orderId'], flush=True)
return {'success': True}

View File

@ -1,3 +1,4 @@
fastapi
dapr-ext-fastapi==1.5.0
cloudevents
uvicorn

View File

@ -1,3 +1,4 @@
Flask
dapr
cloudevents
uvicorn