Skip to content

Commit

Permalink
Added FastAPI example.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbywater committed Jun 5, 2024
1 parent 6f82524 commit 842786d
Show file tree
Hide file tree
Showing 3 changed files with 1,076 additions and 84 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ https://github.com/pyeventsourcing/eventsourcing-eventstoredb) package.
* [Close](#close)
* [Asyncio client](#asyncio-client)
* [Synopsis](#synopsis-1)
* [FastAPI](#fastapi)
* [Notes](#notes)
* [Regular expression filters](#regular-expression-filters)
* [Reconnect and retry method decorators](#reconnect-and-retry-method-decorators)
Expand Down Expand Up @@ -3354,6 +3355,50 @@ asyncio.run(
)
```

### FastAPI example<a id="fastapi"></a>

The example below shows how to use `AsyncEventStoreDBClient` with FastAPI.

```python
from contextlib import asynccontextmanager

from fastapi import FastAPI

from esdbclient import AsyncEventStoreDBClient


client: AsyncEventStoreDBClient


@asynccontextmanager
async def lifespan(_: FastAPI):
# Construct the client.
global client
client = AsyncEventStoreDBClient(
uri="esdb+discover://localhost:2113?Tls=false",
)
await client.connect()

yield

# Close the client.
await client.close()


app = FastAPI(lifespan=lifespan)


@app.get("/commit_position")
async def commit_position():
commit_position = await client.get_commit_position()
return {"commit_position": commit_position}
```

If you put this code in a file called `fastapi_example.py` and then run command
`uvicorn fastapi_example:app --host 0.0.0.0 --port 80`, then the FastAPI application
will return something like `{"commit_position":628917}` when a browser is pointed
to `http://localhost/commit_position`. Use Ctrl-c to exit the process.

## Notes<a id="notes"></a>

### Regular expression filters<a id="regular-expression-filters"></a>
Expand Down
Loading

0 comments on commit 842786d

Please sign in to comment.