Skip to content

Commit

Permalink
Merge pull request #10 from breathe/upgrade-to-pydantic-v2
Browse files Browse the repository at this point in the history
Make changes to be compatible with pydantic v2
  • Loading branch information
sasha-tkachev authored Oct 7, 2023
2 parents a8dbb04 + ca883ec commit 1bdea5f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Automatically parses CloudEvents both in the binary and structured format and
provides an interface very similar to the regular FastAPI interface. No more
hustling with `to_structured` and `from_http` function calls!

```python
```python
@app.post("/")
async def on_event(event: CloudEvent) -> CloudEvent:
pass
pass
```

See more examples below
Expand Down Expand Up @@ -93,12 +93,16 @@ ce-time: 2022-08-05T23:50:52.809697+00:00
from typing import Literal, Union

import uvicorn
from fastapi import FastAPI
from fastapi import FastAPI, Body
from pydantic import Field
from typing_extensions import Annotated

from fastapi_cloudevents import (CloudEvent, CloudEventSettings, ContentMode,
install_fastapi_cloudevents)
from fastapi_cloudevents import (
CloudEvent,
CloudEventSettings,
ContentMode,
install_fastapi_cloudevents,
)

app = FastAPI()
app = install_fastapi_cloudevents(
Expand All @@ -114,7 +118,7 @@ class YourEvent(CloudEvent):
type: Literal["your.type.v1"]


OurEvent = Annotated[Union[MyEvent, YourEvent], Field(discriminator="type")]
OurEvent = Annotated[Union[MyEvent, YourEvent], Body(discriminator="type")]

_source = "dummy:source"

Expand Down Expand Up @@ -142,7 +146,7 @@ if __name__ == "__main__":
### [Structured Response Example](examples/structured_response_server)

To send the response in the http CloudEvent structured format, you MAY use the
`BinaryCloudEventResponse` class
`BinaryCloudEventResponse` class

```python
import uvicorn
Expand Down Expand Up @@ -190,5 +194,5 @@ content-type: application/json

## More Examples

* [Custom Default Source](examples/custom_default_source)
* [Mixed Usage of events and regular models](examples/events_and_basemodels_mixed)
- [Custom Default Source](examples/custom_default_source)
- [Mixed Usage of events and regular models](examples/events_and_basemodels_mixed)
5 changes: 3 additions & 2 deletions examples/events_and_basemodels_mixed/example_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class MyModel(BaseModel):
async def on_event(value: MyModel) -> CloudEvent:
return CloudEvent(
type="my.model-acknowledged.v1",
data=value,
data=value.model_dump(),
)


@app.post("/model-response")
async def on_event(value: MyModel) -> MyModel:
return value
return value.model_dump()


if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8004)
12 changes: 8 additions & 4 deletions examples/type_routing/example_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from typing import Literal, Union

import uvicorn
from fastapi import FastAPI
from fastapi import FastAPI, Body
from pydantic import Field
from typing_extensions import Annotated

from fastapi_cloudevents import (CloudEvent, CloudEventSettings, ContentMode,
install_fastapi_cloudevents)
from fastapi_cloudevents import (
CloudEvent,
CloudEventSettings,
ContentMode,
install_fastapi_cloudevents,
)

app = FastAPI()
app = install_fastapi_cloudevents(
Expand All @@ -22,7 +26,7 @@ class YourEvent(CloudEvent):
type: Literal["your.type.v1"]


OurEvent = Annotated[Union[MyEvent, YourEvent], Field(discriminator="type")]
OurEvent = Annotated[Union[MyEvent, YourEvent], Body(discriminator="type")]

_source = "dummy:source"

Expand Down
8 changes: 5 additions & 3 deletions fastapi_cloudevents/settings.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from enum import Enum
from typing import Optional

from pydantic import BaseSettings, Field
from pydantic.types import constr
from pydantic import StringConstraints, Field

from typing_extensions import Annotated
from pydantic_settings import BaseSettings


class ContentMode(Enum):
Expand All @@ -11,7 +13,7 @@ class ContentMode(Enum):


class CloudEventSettings(BaseSettings):
default_source: Optional[constr(min_length=1)]
default_source: Optional[Annotated[str, StringConstraints(min_length=1)]] = None
default_response_mode: ContentMode = ContentMode.binary
allow_non_cloudevent_models: bool = Field(
default=True,
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
fastapi>=0.62.0
cloudevents[pydantic]
cloudevents[pydantic]
pydantic_settings

0 comments on commit 1bdea5f

Please sign in to comment.