Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a124d61
Moved fields out of base & structured support
Jun 24, 2020
a704f9e
testing structured
Jun 25, 2020
fff8d4d
added tests for structured events
Jun 25, 2020
38ef53c
Added test valid structured cloudevents
Jun 25, 2020
09d9eb3
Created default headers arg in CloudEvent
Jun 25, 2020
919d162
Added http_events.py sample code
Jun 25, 2020
3b4441f
removed ../python-event-requests
Jun 25, 2020
78616b1
README.md nit
Jun 25, 2020
fa91be9
client.py nit
Jun 25, 2020
138a786
comment nits
Jun 25, 2020
72662ba
created __getitem__ in CloudEvent
Jun 25, 2020
dec013c
sample nits
Jun 25, 2020
2a09062
fixed structured empty data issue
Jun 25, 2020
d32da95
Added CloudEvent to README
Jun 25, 2020
431b15c
added http_msg to CloudEvent
Jun 25, 2020
98f9af5
implemented ToRequest in CloudEvent
Jun 25, 2020
b6bef8e
testing more specversions
Jun 25, 2020
b93ed54
Added sample code to README.md
Jun 26, 2020
88dff83
modified sample code
Jun 26, 2020
ac25f46
added datavalidation to changelog
Jun 26, 2020
f1565e5
updated README
Jun 26, 2020
6f3ca78
README adjustment
Jun 26, 2020
a050785
ruler 80 adjustment on http_events
Jun 26, 2020
50cedf5
style and renamed ToRequest to to_request
Jun 26, 2020
e8e875e
lint fix
Jun 26, 2020
e09bb1a
fixed self.binary typo
Jun 26, 2020
7595ab2
CHANGELOG adjustment
Jun 26, 2020
6623756
rollback CHANGELOG
Jun 26, 2020
590d33e
Added documentation to to_request
Jun 26, 2020
e2a1177
README.md adjustment
Jun 26, 2020
baa84fa
renamed event_handler to event_version
Jun 26, 2020
8bfa5d1
inlined field_name_modifier
Jun 26, 2020
8b10172
renamed test body data
Jun 26, 2020
68aed6c
removed unnecessary headers from test
Jun 26, 2020
3a763c1
removed field_name_modifier and fixed e.g. in client.py
Jun 26, 2020
84992e5
pylint fix
Jun 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added sample code to README.md
Signed-off-by: Curtis Mason <[email protected]>
  • Loading branch information
Curtis Mason committed Jun 26, 2020
commit b93ed54680d693fb0b30a9a94032b376e4295a27
75 changes: 49 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,55 @@ This SDK current supports the following versions of CloudEvents:

Package **cloudevents** provides primitives to work with CloudEvents specification: https://github.com/cloudevents/spec.

Creating minimal CloudEvents with data in version 1.0.0:

### Binary HTTP CloudEvent

```python
from cloudevents.sdk.http_events import CloudEvent
import requests


# This data defines a binary cloudevent
headers = {
"Content-Type": "application/json",
"ce-specversion": "1.0",
"ce-type": "README.sample.binary",
"ce-id": "binary-event",
"ce-time": "2018-10-23T12:28:22.4579346Z",
"ce-source": "README",
}
data = {"message": "Hello World!"}

event = CloudEvent(data, headers=headers)
headers, body = event.ToRequest()

# POST
requests.post("<some-url>", json=body, headers=headers)
```

### Structured HTTP CloudEvent

```python
from cloudevents.sdk.http_events import CloudEvent
import requests


# This data defines a structured cloudevent
data = {
"specversion": "1.0",
"type": "README.sample.structured",
"id": "structured-event",
"source": "README",
"data": {"message": "Hello World!"}
}
event = CloudEvent(data)
headers, body = event.ToRequest()

# POST
requests.post("<some-url>", json=structured_body, headers=structured_headers)
```

Parsing upstream structured Event from HTTP request:

```python
Expand Down Expand Up @@ -68,32 +117,6 @@ event = m.FromRequest(
)
```

Creating minimal CloudEvents with data in version 1.0.0:

```python
from cloudevents.sdk.http_events import CloudEvent

data = {
"specversion": "1.0",
"type": "word.found.name",
"id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
"source": "<source-url>",
"data": {"message": "Hello World!"}
}
structured_event = CloudEvent(data)

headers = {
"Content-Type": "application/cloudevents+json",
"ce-specversion": "1.0",
"ce-type": "word.found.name",
"ce-id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
"ce-time": "2018-10-23T12:28:22.4579346Z",
"ce-source": "<source-url>",
}
data = {"message": "Hello World!"}
binary_event = CloudEvent(data, headers=headers)
```

Creating HTTP request from CloudEvent:

```python
Expand Down
25 changes: 10 additions & 15 deletions cloudevents/sdk/http_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ def __init__(
self,
data: typing.Union[dict,None],
headers: dict = {},
data_unmarshaller: typing.Callable = lambda x: json.loads(
x.read()
.decode('utf-8')
),
data_marshaller: typing.Callable = lambda x: io.BytesIO(
json.dumps(x)
.encode()
)
data_unmarshaller= lambda x: x,
):
"""
Event HTTP Constructor
Expand All @@ -63,8 +56,6 @@ def __init__(
"""
self.required_attribute_values = {}
self.optional_attribute_values = {}
self.data_unmarshaller = data_unmarshaller
self.data_marshaller = data_marshaller
if data is None:
data = {}

Expand All @@ -81,8 +72,8 @@ def __init__(
self.__event = self.marshall.FromRequest(
self.event_handler,
headers,
self.data_marshaller(data),
self.data_unmarshaller
io.BytesIO(json.dumps(data).encode()),
data_unmarshaller
)

# headers validation for binary events
Expand Down Expand Up @@ -135,15 +126,19 @@ def __init__(
}


def ToRequest(self):
def ToRequest(
self,
data_unmarshaller: typing.Callable = lambda x: json.loads(x.read().decode('utf-8'))
) -> (dict, dict):
converter_type = converters.TypeBinary if self.isbinary else \
converters.TypeStructured

return self.marshall.ToRequest(
headers, data = self.marshall.ToRequest(
self.__event,
converter_type,
lambda x: x
data_unmarshaller
)
return headers, (data if self.isbinary else data_unmarshaller(data)['data'])

def __getitem__(self, key):
return self.data if key == 'data' else self.headers[key]
Expand Down
9 changes: 7 additions & 2 deletions cloudevents/tests/test_http_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import io

import json

import copy
Expand Down Expand Up @@ -217,11 +219,12 @@ def test_structured_ToRequest(specversion):
}
event = CloudEvent(data)
headers, body = event.ToRequest()
decoded_body = event.data_unmarshaller(body)['data']
assert isinstance(body, dict)
print(body)

assert headers['content-type'] == 'application/cloudevents+json'
for key in data:
assert decoded_body[key] == data[key]
assert body[key] == data[key]


@pytest.mark.parametrize("specversion", ['1.0', '0.3'])
Expand All @@ -237,6 +240,8 @@ def test_binary_ToRequest(specversion):
}
event = CloudEvent(data, headers=test_headers)
headers, body = event.ToRequest()
assert isinstance(body, dict)
print("LOOK", body)

for key in data:
assert body[key] == data[key]
Expand Down