@@ -6,77 +6,106 @@ This SDK is still considered a work in progress, therefore things might (and
66will) break with every update.
77
88This SDK current supports the following versions of CloudEvents:
9+ - v1.0
10+ - v0.3
911- v0.2
1012- v0.1
1113
1214## Python SDK
1315
1416Package ** cloudevents** provides primitives to work with CloudEvents specification: https://github.com/cloudevents/spec .
1517
16- Parsing upstream Event from HTTP Request:
18+ Parsing upstream structured Event from HTTP request:
19+
1720``` python
1821import io
1922
20- from cloudevents.sdk.event import v02
23+ from cloudevents.sdk.event import v1
2124from cloudevents.sdk import marshaller
2225
2326m = marshaller.NewDefaultHTTPMarshaller()
27+
2428event = m.FromRequest(
25- v02.Event(),
29+ v1.Event(),
30+ {" content-type" : " application/cloudevents+json" },
31+ io.StringIO(
32+ """
33+ {
34+ "specversion": "1.0",
35+ "datacontenttype": "application/json",
36+ "type": "word.found.name",
37+ "id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
38+ "time": "2018-10-23T12:28:22.4579346Z",
39+ "source": "<source-url>"
40+ }
41+ """
42+ ),
43+ lambda x : x.read(),
44+ )
45+ ```
46+
47+ Parsing upstream binary Event from HTTP request:
48+
49+ ``` python
50+ import io
51+
52+ from cloudevents.sdk.event import v1
53+ from cloudevents.sdk import marshaller
54+
55+ m = marshaller.NewDefaultHTTPMarshaller()
56+
57+ event = m.FromRequest(
58+ v1.Event(),
2659 {
27- " content-type " : " application/cloudevents+json " ,
28- " ce-specversion " : " 0.2 " ,
29- " ce-time " : " 2018-10-23T12:28:22.4579346Z " ,
60+ " ce-specversion " : " 1.0 " ,
61+ " content-type " : " application/json " ,
62+ " ce-type " : " word.found.name " ,
3063 " ce-id" : " 96fb5f0b-001e-0108-6dfe-da6e2806f124" ,
64+ " ce-time" : " 2018-10-23T12:28:22.4579346Z" ,
3165 " ce-source" : " <source-url>" ,
32- " ce-type" : " word.found.name" ,
3366 },
34- io.BytesIO(b " this is where your CloudEvent data" ),
35- lambda x : x.read()
67+ io.BytesIO(b " this is where your CloudEvent data" ),
68+ lambda x : x.read(),
3669)
37-
3870```
3971
4072Creating a minimal CloudEvent in version 0.1:
73+
4174``` python
42- from cloudevents.sdk.event import v01
75+ from cloudevents.sdk.event import v1
4376
4477event = (
45- v01 .Event().
46- SetContentType(" application/json" ).
47- SetData(' {"name":"john"}' ).
48- SetEventID(" my-id" ).
49- SetSource(" from-galaxy-far-far-away" ).
50- SetEventTime(" tomorrow" ).
51- SetEventType(" cloudevent.greet.you" )
78+ v1 .Event()
79+ . SetContentType(" application/json" )
80+ . SetData(' {"name":"john"}' )
81+ . SetEventID(" my-id" )
82+ . SetSource(" from-galaxy-far-far-away" )
83+ . SetEventTime(" tomorrow" )
84+ . SetEventType(" cloudevent.greet.you" )
5285)
53-
5486```
5587
5688Creating HTTP request from CloudEvent:
89+
5790``` python
5891from cloudevents.sdk import converters
5992from cloudevents.sdk import marshaller
6093from cloudevents.sdk.converters import structured
61- from cloudevents.sdk.event import v01
94+ from cloudevents.sdk.event import v1
6295
6396event = (
64- v01.Event().
65- SetContentType(" application/json" ).
66- SetData(' {"name":"john"}' ).
67- SetEventID(" my-id" ).
68- SetSource(" from-galaxy-far-far-away" ).
69- SetEventTime(" tomorrow" ).
70- SetEventType(" cloudevent.greet.you" )
71- )
72- m = marshaller.NewHTTPMarshaller(
73- [
74- structured.NewJSONHTTPCloudEventConverter()
75- ]
97+ v1.Event()
98+ .SetContentType(" application/json" )
99+ .SetData(' {"name":"john"}' )
100+ .SetEventID(" my-id" )
101+ .SetSource(" from-galaxy-far-far-away" )
102+ .SetEventTime(" tomorrow" )
103+ .SetEventType(" cloudevent.greet.you" )
76104)
77105
78- headers, body = m.ToRequest(event, converters.TypeStructured, lambda x : x )
106+ m = marshaller.NewHTTPMarshaller([structured.NewJSONHTTPCloudEventConverter()] )
79107
108+ headers, body = m.ToRequest(event, converters.TypeStructured, lambda x : x)
80109```
81110
82111## HOWTOs with various Python HTTP frameworks
@@ -85,7 +114,7 @@ In this topic you'd find various example how to integrate an SDK with various HT
85114
86115### Python requests
87116
88- One of popular framework is [ 0.2-force-improvements ] ( http://docs.python-requests.org/en/master/ ) .
117+ One of popular framework is [ ` requests ` ] ( http://docs.python-requests.org/en/master/ ) .
89118
90119
91120#### CloudEvent to request
0 commit comments