|
1 |
| -# sdk-python |
2 |
| -Python SDK for CloudEvents |
| 1 | +# Python SDK for [CloudEvents](https://github.com/cloudevents/spec) |
| 2 | + |
| 3 | +**NOTE: This SDK is still considered work in progress, things might (and will) break with every update.** |
| 4 | + |
| 5 | +Package **cloudevents** provides primitives to work with CloudEvents specification: https://github.com/cloudevents/spec. |
| 6 | + |
| 7 | +Parsing upstream Event from HTTP Request: |
| 8 | +```python |
| 9 | +from cloudevents.sdk.event import upstream |
| 10 | +from cloudevents.sdk import marshaller |
| 11 | + |
| 12 | +data = "<this is where your CloudEvent comes from>" |
| 13 | +m = marshaller.NewDefaultHTTPMarshaller(upstream.Event) |
| 14 | +event = m.FromRequest({"Content-Type": "application/cloudevents+json"}, data) |
| 15 | + |
| 16 | +``` |
| 17 | + |
| 18 | +Creating a minimal CloudEvent in version 0.1: |
| 19 | +```python |
| 20 | +from cloudevents.sdk.event import v01 |
| 21 | + |
| 22 | +event = ( |
| 23 | + v01.Event(). |
| 24 | + WithContentType("application/json"). |
| 25 | + WithData('{"name":"john"}'). |
| 26 | + WithEventID("my-id"). |
| 27 | + WithSource("from-galaxy-far-far-away"). |
| 28 | + WithEventTime("tomorrow"). |
| 29 | + WithEventType("cloudevent.greet.you") |
| 30 | +) |
| 31 | + |
| 32 | +``` |
| 33 | + |
| 34 | +Creating HTTP request from CloudEvent: |
| 35 | +```python |
| 36 | +from cloudevents.sdk import converters |
| 37 | +from cloudevents.sdk import marshaller |
| 38 | +from cloudevents.sdk.converters import structured |
| 39 | +from cloudevents.sdk.event import v01 |
| 40 | + |
| 41 | +event = ( |
| 42 | + v01.Event(). |
| 43 | + WithContentType("application/json"). |
| 44 | + WithData('{"name":"john"}'). |
| 45 | + WithEventID("my-id"). |
| 46 | + WithSource("from-galaxy-far-far-away"). |
| 47 | + WithEventTime("tomorrow"). |
| 48 | + WithEventType("cloudevent.greet.you") |
| 49 | +) |
| 50 | +m = marshaller.NewHTTPMarshaller( |
| 51 | + [ |
| 52 | + structured.NewJSONHTTPCloudEventConverter(type(event)) |
| 53 | + ] |
| 54 | +) |
| 55 | + |
| 56 | +headers, body = m.ToRequest(event, converters.TypeStructured, lambda x: x) |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +The goal of this package is to provide support for all released versions of CloudEvents, ideally while maintaining |
| 61 | +the same API. It will use semantic versioning with following rules: |
| 62 | +* MAJOR version increments when backwards incompatible changes is introduced. |
| 63 | +* MINOR version increments when backwards compatible feature is introduced INCLUDING support for new CloudEvents version. |
| 64 | +* PATCH version increments when a backwards compatible bug fix is introduced. |
0 commit comments