Skip to content

Commit 390134c

Browse files
cumason123evankandersondigrant
authored
Release v1.0.0 (#89)
* Created CloudEvent class (#36) CloudEvents is a more pythonic interface for using cloud events. It is powered by internal marshallers and cloud event base classes. It performs basic validation on fields, and cloud event type checking. Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * Implemented python properties in base.py (#41) * Added SetCloudEventVersion Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * began adding python properties Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * added pythonic properties to base class Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * began testing for getters/setters Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * added general setter tests Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * fixed spacing in base.py Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * added __eq__ to option and datacontentencoding property to v03 Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * lint fixes Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * testing extensions and old getters Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * removed versions v01 and v02 from test_data_encaps_refs.py Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * fixed inheritance issue in CloudEvent Signed-off-by: Curtis Mason <[email protected]> * added prefixed_headers dict to test Signed-off-by: Curtis Mason <[email protected]> * Http structured cloudevents (#47) * Moved fields out of base & structured support base._ce_required_fields and base._ce_optional_fields were moved into event classes v03 and v1. http_events.CloudEvent class now looks for fieldnames in either headers or data, and can automatically determine whether this is a binary or structured event. Signed-off-by: Curtis Mason <[email protected]> * testing structured Signed-off-by: Curtis Mason <[email protected]> * added tests for structured events Signed-off-by: Curtis Mason <[email protected]> * Added test valid structured cloudevents Signed-off-by: Curtis Mason <[email protected]> * Created default headers arg in CloudEvent Signed-off-by: Curtis Mason <[email protected]> * Added http_events.py sample code Signed-off-by: Curtis Mason <[email protected]> * removed ../python-event-requests Signed-off-by: Curtis Mason <[email protected]> * README.md nit Signed-off-by: Curtis Mason <[email protected]> * client.py nit Signed-off-by: Curtis Mason <[email protected]> * comment nits Signed-off-by: Curtis Mason <[email protected]> * created __getitem__ in CloudEvent Signed-off-by: Curtis Mason <[email protected]> * sample nits Signed-off-by: Curtis Mason <[email protected]> * fixed structured empty data issue Signed-off-by: Curtis Mason <[email protected]> * Added CloudEvent to README Signed-off-by: Curtis Mason <[email protected]> * added http_msg to CloudEvent Signed-off-by: Curtis Mason <[email protected]> * implemented ToRequest in CloudEvent Signed-off-by: Curtis Mason <[email protected]> * testing more specversions Signed-off-by: Curtis Mason <[email protected]> * Added sample code to README.md Signed-off-by: Curtis Mason <[email protected]> * modified sample code Signed-off-by: Curtis Mason <[email protected]> * added datavalidation to changelog Signed-off-by: Curtis Mason <[email protected]> * updated README Signed-off-by: Curtis Mason <[email protected]> * README adjustment Signed-off-by: Curtis Mason <[email protected]> * ruler 80 adjustment on http_events Signed-off-by: Curtis Mason <[email protected]> * style and renamed ToRequest to to_request Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * fixed self.binary typo Signed-off-by: Curtis Mason <[email protected]> * CHANGELOG adjustment Signed-off-by: Curtis Mason <[email protected]> * rollback CHANGELOG Signed-off-by: Curtis Mason <[email protected]> * Added documentation to to_request Signed-off-by: Curtis Mason <[email protected]> * README.md adjustment Signed-off-by: Curtis Mason <[email protected]> * renamed event_handler to event_version Signed-off-by: Curtis Mason <[email protected]> * inlined field_name_modifier Signed-off-by: Curtis Mason <[email protected]> * renamed test body data Signed-off-by: Curtis Mason <[email protected]> * removed unnecessary headers from test Signed-off-by: Curtis Mason <[email protected]> * removed field_name_modifier and fixed e.g. in client.py Signed-off-by: Curtis Mason <[email protected]> * pylint fix Signed-off-by: Curtis Mason <[email protected]> * Update types and handle data_base64 structured. (#34) * Update types and handle data_base64 structured. - Add sane defaults for encoding - Unfortunately, defaults for structured and binary need to be *different* - Push types through interfaces - Make it easy to call 'ToRequest' using Marshaller defaults - Add tests for above Signed-off-by: Evan Anderson <[email protected]> * Fix lint warnings due to changes to W503/W504 See https://gitlab.com/pycqa/flake8/-/issues/466 for details. Signed-off-by: Evan Anderson <[email protected]> * Adopt di's suggestions. Signed-off-by: Evan Anderson <[email protected]> * Fix lint. Signed-off-by: Evan Anderson <[email protected]> * Move types to another package. Signed-off-by: Evan Anderson <[email protected]> * Adjust CloudEvent class in http_events.py to support binary data as well as JSON. Signed-off-by: Evan Anderson <[email protected]> * Apply suggested changes by MacrBoissonneault Signed-off-by: Evan Anderson <[email protected]> * Fix samples as well. Signed-off-by: Evan Anderson <[email protected]> * Fix lint. Apparently, we can complain about formating issues, but a human has to fix them. Signed-off-by: Evan Anderson <[email protected]> * Add test for binary encoding of messages. Fix usability of binary detection in MarshalJSON to support memoryview. Signed-off-by: Evan Anderson <[email protected]> * Fix errors noticed by cumason123 Signed-off-by: Evan Anderson <[email protected]> * Changelog version deprecation (#48) * added changelog Signed-off-by: Curtis Mason <[email protected]> * Created CloudEvent class (#36) CloudEvents is a more pythonic interface for using cloud events. It is powered by internal marshallers and cloud event base classes. It performs basic validation on fields, and cloud event type checking. Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Fix tox configuration for CI (#46) Signed-off-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Implemented python properties in base.py (#41) * Added SetCloudEventVersion Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * began adding python properties Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * added pythonic properties to base class Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * began testing for getters/setters Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * added general setter tests Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * fixed spacing in base.py Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * added __eq__ to option and datacontentencoding property to v03 Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * lint fixes Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * testing extensions and old getters Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * removed versions v01 and v02 from test_data_encaps_refs.py Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Dustin Ingram <[email protected]> * fixed inheritance issue in CloudEvent Signed-off-by: Curtis Mason <[email protected]> * added prefixed_headers dict to test Signed-off-by: Curtis Mason <[email protected]> * CHANGELOG adjustment Signed-off-by: Curtis Mason <[email protected]> * Update CHANGELOG.md Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Update CHANGELOG.md Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Update CHANGELOG.md Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Update CHANGELOG.md Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Removed irrelevant files from commit diff Signed-off-by: Curtis Mason <[email protected]> Co-authored-by: Dustin Ingram <[email protected]> * Black formatter (#51) * black and isort added to precommit Signed-off-by: Curtis Mason <[email protected]> * main renaming Signed-off-by: Curtis Mason <[email protected]> * fixed tox Signed-off-by: Curtis Mason <[email protected]> * linting in tox rename Signed-off-by: Curtis Mason <[email protected]> * fixed tox trailing space Signed-off-by: Curtis Mason <[email protected]> * added reformat tox env Signed-off-by: Curtis Mason <[email protected]> * Reformatting files Signed-off-by: Curtis Mason <[email protected]> * reformatted more files Signed-off-by: Curtis Mason <[email protected]> * documented tox in README Signed-off-by: Curtis Mason <[email protected]> * removed -rc flag Signed-off-by: Curtis Mason <[email protected]> * README and http-cloudevents sample code adjustments to reflect new CloudEvent (#56) * README and http-cloudevents CloudEvent adjustments README no longer shows how to use base event classes to create events. Removed this because users shouldn't be forced to interact with the marshaller class. Additionally, CloudEvent is a simpler interface therefore we are encouraging the CloudEvent class usage. http-cloudevents now has more example usage for the getitem overload. Similarly README shows how to use getitem overload. Signed-off-by: Curtis Mason <[email protected]> * lint reformat Signed-off-by: Curtis Mason <[email protected]> * resolved nits Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * renamed /mycontext to url Signed-off-by: Curtis Mason <[email protected]> * renamed here linlk to in the samples directory Signed-off-by: Curtis Mason <[email protected]> * Separated http methods (#60) * instantiated http path Signed-off-by: Curtis Mason <[email protected]> * moved from_http from CloudEvent to http Signed-off-by: Curtis Mason <[email protected]> * Moved to_http out of CloudEvent Signed-off-by: Curtis Mason <[email protected]> * moved http library into event.py Signed-off-by: Curtis Mason <[email protected]> * testing printable cloudevent Signed-off-by: Curtis Mason <[email protected]> * Adjusted README Signed-off-by: Curtis Mason <[email protected]> * Created EventClass Signed-off-by: Curtis Mason <[email protected]> * reformatted event.py Signed-off-by: Curtis Mason <[email protected]> * from_json definition Signed-off-by: Curtis Mason <[email protected]> * server print changes Signed-off-by: Curtis Mason <[email protected]> * Specversion toggling (#57) * cloudevent now switches specversion types Signed-off-by: Curtis Mason <[email protected]> * removed duplicate marshall instance Signed-off-by: Curtis Mason <[email protected]> * resolved grant requests Signed-off-by: Curtis Mason <[email protected]> * converters now can check headers for fields Signed-off-by: Curtis Mason <[email protected]> * removed print statement Signed-off-by: Curtis Mason <[email protected]> * Fixed marshallers looking at headers for specversion Signed-off-by: Curtis Mason <[email protected]> * lint fixes Signed-off-by: Curtis Mason <[email protected]> * is_binary static method and structured isinstance rework Signed-off-by: Curtis Mason <[email protected]> * testing for is_binary and is_structured Signed-off-by: Curtis Mason <[email protected]> * Image sample code (#65) * added image example Signed-off-by: Curtis Mason <[email protected]> * moved size into headers Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * renamed sample code Signed-off-by: Curtis Mason <[email protected]> * added test to http-image-cloudevents sample Signed-off-by: Curtis Mason <[email protected]> * removed unnecessary function Signed-off-by: Curtis Mason <[email protected]> * Added testing for http-image-cloudevents Signed-off-by: Curtis Mason <[email protected]> * Data marshall arg fix and better image in sample Fixed bug where data_marshaller and data_unmarshaller wasn't being passed into positional arguments. Also used cloudevents logo for the image in http-image-cloudevents Signed-off-by: Curtis Mason <[email protected]> * adjusted http-image-cloudevents samples Signed-off-by: Curtis Mason <[email protected]> * reformat and README changes Signed-off-by: Curtis Mason <[email protected]> * io bytes casting in data_unmarshaller Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * removed unusued imports in http-image samples Signed-off-by: Curtis Mason <[email protected]> * removed samples/http-cloudevents/tmp.png Signed-off-by: Curtis Mason <[email protected]> * Nits Signed-off-by: Curtis Mason <[email protected]> * Implemented to_json and from_json (#72) * added test_to_json test Signed-off-by: Curtis Mason <[email protected]> * implemented to_json with tests Signed-off-by: Curtis Mason <[email protected]> * from_json and to_json tests Signed-off-by: Curtis Mason <[email protected]> * Tests for to_json being able to talk to from_json Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * added documentation for to_json and from_json Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * Fixed top level extensions bug (#71) * Fixed top level extensions bug Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * fixed name bug in test_event_extensions Signed-off-by: Curtis Mason <[email protected]> * fixed broken links in README.md (#75) Signed-off-by: Curtis Mason <[email protected]> * Fixed marshaller documentation typo's in http (#76) * Fixed marshaller documentation in http directory Signed-off-by: Curtis Mason <[email protected]> * adjusted marshaller documentation Signed-off-by: Curtis Mason <[email protected]> * None data fix (#78) * fixed none data issue Signed-off-by: Curtis Mason <[email protected]> * added none data test for marshalling Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * Samples image test server (#79) * fixed none data issue Signed-off-by: Curtis Mason <[email protected]> * added none data test for marshalling Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * added http server test in image sample Signed-off-by: Curtis Mason <[email protected]> * Removed print statements from test Signed-off-by: Curtis Mason <[email protected]> * removed requests from test Signed-off-by: Curtis Mason <[email protected]> * Top level http (#83) * Modularized http and made http a top level module Modularized the http directory by separating related functions into different scripts. Also removed EventClass and kept a singular CloudEvent. Finally, CloudEvent.__repr__ was refactored such that it doesn't depend on external methods. Signed-off-by: Curtis Mason <[email protected]> * renamed requests.py to http_methods Signed-off-by: Curtis Mason <[email protected]> * lint fixes Signed-off-by: Curtis Mason <[email protected]> * http-json-cloudevents testing (#80) * Added tests to http-json-cloudevents Signed-off-by: Curtis Mason <[email protected]> * removed outdated python-requests sample code Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * Added flask to requirements Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> * docs: add README badge (#85) Signed-off-by: Grant Timmerman <[email protected]> * added pypi-release rule (#87) * added pypi-release rule Signed-off-by: Curtis Mason <[email protected]> * added RELEASING.md Signed-off-by: Curtis Mason <[email protected]> * Adjusted RELEASING.md Signed-off-by: Curtis Mason <[email protected]> * Update .github/workflows/pypi-release.yml Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * workflow pypi name changed Signed-off-by: Curtis Mason <[email protected]> * Update RELEASING.md Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Update RELEASING.md Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Update RELEASING.md Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * removed some pbr stuff Signed-off-by: Curtis Mason <[email protected]> * Removed all pbr stuff Signed-off-by: Curtis Mason <[email protected]> * README nits Signed-off-by: Curtis Mason <[email protected]> * RELEASING adjustment in README Signed-off-by: Curtis Mason <[email protected]> * author update in setup.cfg Signed-off-by: Curtis Mason <[email protected]> * removed setup.cfg Signed-off-by: Curtis Mason <[email protected]> * Update setup.py Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * lint fix Signed-off-by: Curtis Mason <[email protected]> Co-authored-by: Dustin Ingram <[email protected]> * pypi-release git tags automation (#88) * added pypi_packaging Signed-off-by: Curtis Mason <[email protected]> * reverted pypi-release Signed-off-by: Curtis Mason <[email protected]> * added pypi_package workflow Signed-off-by: Curtis Mason <[email protected]> * added gitpython dependency Signed-off-by: Curtis Mason <[email protected]> * added git import in createTag function Signed-off-by: Curtis Mason <[email protected]> * Updated RELEASING.md and implemented pypi_config in pypi_packaging.pg Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Fixed some docs Signed-off-by: Curtis Mason <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * Update .github/workflows/pypi-release.yml Co-authored-by: Dustin Ingram <[email protected]> Signed-off-by: Curtis Mason <[email protected]> * added __version__ Signed-off-by: Curtis Mason <[email protected]> * lint change Signed-off-by: Curtis Mason <[email protected]> * reinstalling cloudevents in workflow Signed-off-by: Curtis Mason <[email protected]> * added cloudevents to publish.txt Signed-off-by: Curtis Mason <[email protected]> * removed old release_doc Signed-off-by: Curtis Mason <[email protected]> Co-authored-by: Dustin Ingram <[email protected]> Co-authored-by: Evan Anderson <[email protected]> Co-authored-by: Dustin Ingram <[email protected]> Co-authored-by: Grant Timmerman <[email protected]>
1 parent d551dba commit 390134c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2167
-545
lines changed

.github/workflows/pypi-release.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: PyPI-Release
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build-and-publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Set up Python
14+
uses: actions/setup-python@v1
15+
with:
16+
python-version: "3.x"
17+
- name: Install build dependencies
18+
run: pip install -U setuptools wheel build
19+
- name: Build
20+
run: python -m build .
21+
- name: Publish
22+
uses: pypa/gh-action-pypi-publish@master
23+
with:
24+
password: ${{ secrets.pypi_password }}
25+
- name: Install GitPython and cloudevents for pypi_packaging
26+
run: pip install -U -r requirements/publish.txt
27+
- name: Create Tag
28+
run: python pypi_packaging.py

.isort.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[settings]
2+
line_length = 80
3+
multi_line_output = 3
4+
include_trailing_comma = True

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
repos:
2+
- repo: https://github.com/timothycrosley/isort/
3+
rev: 5.0.4
4+
hooks:
5+
- id: isort
6+
- repo: https://github.com/psf/black
7+
rev: 19.10b0
8+
hooks:
9+
- id: black
10+
language_version: python3.8

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.0]
8+
### Added
9+
- Added a user friendly CloudEvent class with data validation ([#36])
10+
- CloudEvent structured cloudevent support ([#47])
11+
12+
### Removed
13+
- Removed support for Cloudevents V0.2 and V0.1 ([#43])
14+
715
## [0.3.0]
816
### Added
917
- Added Cloudevents V0.3 and V1 implementations ([#22])
@@ -66,3 +74,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6674
[#23]: https://github.com/cloudevents/sdk-python/pull/23
6775
[#25]: https://github.com/cloudevents/sdk-python/pull/25
6876
[#27]: https://github.com/cloudevents/sdk-python/pull/27
77+
[#36]: https://github.com/cloudevents/sdk-python/pull/36
78+
[#43]: https://github.com/cloudevents/sdk-python/pull/43
79+
[#47]: https://github.com/cloudevents/sdk-python/pull/47

README.md

Lines changed: 70 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Python SDK for [CloudEvents](https://github.com/cloudevents/spec)
22

3+
[![PyPI version](https://badge.fury.io/py/cloudevents.svg)](https://badge.fury.io/py/cloudevents)
4+
35
## Status
46

57
This SDK is still considered a work in progress, therefore things might (and
@@ -14,159 +16,87 @@ This SDK current supports the following versions of CloudEvents:
1416

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

17-
Parsing upstream structured Event from HTTP request:
19+
## Sending CloudEvents
1820

19-
```python
20-
import io
21-
22-
from cloudevents.sdk.event import v1
23-
from cloudevents.sdk import marshaller
24-
25-
m = marshaller.NewDefaultHTTPMarshaller()
26-
27-
event = m.FromRequest(
28-
v1.Event(),
29-
{"content-type": "application/cloudevents+json"},
30-
io.StringIO(
31-
"""
32-
{
33-
"specversion": "1.0",
34-
"datacontenttype": "application/json",
35-
"type": "word.found.name",
36-
"id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
37-
"time": "2018-10-23T12:28:22.4579346Z",
38-
"source": "<source-url>"
39-
}
40-
"""
41-
),
42-
lambda x: x.read(),
43-
)
44-
```
21+
Below we will provide samples on how to send cloudevents using the popular
22+
[`requests`](http://docs.python-requests.org) library.
4523

46-
Parsing upstream binary Event from HTTP request:
24+
### Binary HTTP CloudEvent
4725

4826
```python
49-
import io
50-
51-
from cloudevents.sdk.event import v1
52-
from cloudevents.sdk import marshaller
53-
54-
m = marshaller.NewDefaultHTTPMarshaller()
55-
56-
event = m.FromRequest(
57-
v1.Event(),
58-
{
59-
"ce-specversion": "1.0",
60-
"content-type": "application/json",
61-
"ce-type": "word.found.name",
62-
"ce-id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
63-
"ce-time": "2018-10-23T12:28:22.4579346Z",
64-
"ce-source": "<source-url>",
65-
},
66-
io.BytesIO(b"this is where your CloudEvent data"),
67-
lambda x: x.read(),
68-
)
69-
```
27+
from cloudevents.http import CloudEvent, to_binary_http
28+
import requests
7029

71-
Creating a minimal CloudEvent in version 0.1:
7230

73-
```python
74-
from cloudevents.sdk.event import v1
75-
76-
event = (
77-
v1.Event()
78-
.SetContentType("application/json")
79-
.SetData('{"name":"john"}')
80-
.SetEventID("my-id")
81-
.SetSource("from-galaxy-far-far-away")
82-
.SetEventTime("tomorrow")
83-
.SetEventType("cloudevent.greet.you")
84-
)
31+
# This data defines a binary cloudevent
32+
attributes = {
33+
"type": "com.example.sampletype1",
34+
"source": "https://example.com/event-producer",
35+
}
36+
data = {"message": "Hello World!"}
37+
38+
event = CloudEvent(attributes, data)
39+
headers, body = to_binary_http(event)
40+
41+
# POST
42+
requests.post("<some-url>", data=body, headers=headers)
8543
```
8644

87-
Creating HTTP request from CloudEvent:
45+
### Structured HTTP CloudEvent
8846

8947
```python
90-
from cloudevents.sdk import converters
91-
from cloudevents.sdk import marshaller
92-
from cloudevents.sdk.converters import structured
93-
from cloudevents.sdk.event import v1
94-
95-
event = (
96-
v1.Event()
97-
.SetContentType("application/json")
98-
.SetData('{"name":"john"}')
99-
.SetEventID("my-id")
100-
.SetSource("from-galaxy-far-far-away")
101-
.SetEventTime("tomorrow")
102-
.SetEventType("cloudevent.greet.you")
103-
)
104-
105-
m = marshaller.NewHTTPMarshaller([structured.NewJSONHTTPCloudEventConverter()])
106-
107-
headers, body = m.ToRequest(event, converters.TypeStructured, lambda x: x)
108-
```
48+
from cloudevents.http import CloudEvent, to_structured_http
49+
import requests
10950

110-
## HOWTOs with various Python HTTP frameworks
11151

112-
In this topic you'd find various example how to integrate an SDK with various HTTP frameworks.
52+
# This data defines a structured cloudevent
53+
attributes = {
54+
"type": "com.example.sampletype2",
55+
"source": "https://example.com/event-producer",
56+
}
57+
data = {"message": "Hello World!"}
58+
event = CloudEvent(attributes, data)
59+
headers, body = to_structured_http(event)
11360

114-
### Python requests
61+
# POST
62+
requests.post("<some-url>", data=body, headers=headers)
63+
```
11564

116-
One of popular framework is [`requests`](http://docs.python-requests.org/en/master/).
65+
You can find a complete example of turning a CloudEvent into a HTTP request [in the samples directory](samples/http-json-cloudevents/client.py).
11766

118-
#### CloudEvent to request
67+
#### Request to CloudEvent
11968

120-
The code below shows how integrate both libraries in order to convert a CloudEvent into an HTTP request:
69+
The code below shows how to consume a cloudevent using the popular python web framework
70+
[flask](https://flask.palletsprojects.com/en/1.1.x/quickstart/):
12171

12272
```python
123-
def run_binary(event, url):
124-
binary_headers, binary_data = http_marshaller.ToRequest(
125-
event, converters.TypeBinary, json.dumps)
126-
127-
print("binary CloudEvent")
128-
for k, v in binary_headers.items():
129-
print("{0}: {1}\r\n".format(k, v))
130-
print(binary_data.getvalue())
131-
response = requests.post(url,
132-
headers=binary_headers,
133-
data=binary_data.getvalue())
134-
response.raise_for_status()
135-
136-
137-
def run_structured(event, url):
138-
structured_headers, structured_data = http_marshaller.ToRequest(
139-
event, converters.TypeStructured, json.dumps
140-
)
141-
print("structured CloudEvent")
142-
print(structured_data.getvalue())
73+
from flask import Flask, request
14374

144-
response = requests.post(url,
145-
headers=structured_headers,
146-
data=structured_data.getvalue())
147-
response.raise_for_status()
75+
from cloudevents.http import from_http
14876

149-
```
77+
app = Flask(__name__)
15078

151-
Complete example of turning a CloudEvent into a request you can find [here](samples/python-requests/cloudevent_to_request.py).
15279

153-
#### Request to CloudEvent
80+
# create an endpoint at http://localhost:/3000/
81+
@app.route("/", methods=["POST"])
82+
def home():
83+
# create a CloudEvent
84+
event = from_http(request.get_data(), request.headers)
15485

155-
The code below shows how integrate both libraries in order to create a CloudEvent from an HTTP request:
86+
# you can access cloudevent fields as seen below
87+
print(
88+
f"Found {event['id']} from {event['source']} with type "
89+
f"{event['type']} and specversion {event['specversion']}"
90+
)
15691

157-
```python
158-
response = requests.get(url)
159-
response.raise_for_status()
160-
headers = response.headers
161-
data = io.BytesIO(response.content)
162-
event = v1.Event()
163-
http_marshaller = marshaller.NewDefaultHTTPMarshaller()
164-
event = http_marshaller.FromRequest(
165-
event, headers, data, json.load)
92+
return "", 204
16693

94+
95+
if __name__ == "__main__":
96+
app.run(port=3000)
16797
```
16898

169-
Complete example of turning a CloudEvent into a request you can find [here](samples/python-requests/request_to_cloudevent.py).
99+
You can find a complete example of turning a CloudEvent into a HTTP request [in the samples directory](samples/http-json-cloudevents/server.py).
170100

171101
## SDK versioning
172102

@@ -189,3 +119,17 @@ the same API. It will use semantic versioning with following rules:
189119
[CNCF's Slack workspace](https://slack.cncf.io/).
190120
- Email: https://lists.cncf.io/g/cncf-cloudevents-sdk
191121
- Contact for additional information: Denis Makogon (`@denysmakogon` on slack).
122+
123+
## Maintenance
124+
125+
We use black and isort for autoformatting. We setup a tox environment to reformat
126+
the codebase.
127+
128+
e.g.
129+
130+
```python
131+
pip install tox
132+
tox -e reformat
133+
```
134+
135+
For information on releasing version bumps see [RELEASING.md](RELEASING.md)

RELEASING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Releasing CloudEvents SDK for Python
2+
3+
This repository is configured to automatically publish the corresponding [PyPI
4+
package](https://pypi.org/project/cloudevents/) and GitHub Tag via GitHub Actions.
5+
6+
To release a new CloudEvents SDK, contributors should bump `__version__` in
7+
[cloudevents](cloudevents/__init__.py) to reflect the new release version. On merge, the action
8+
will automatically build and release to PyPI using
9+
[this PyPI GitHub Action](https://github.com/pypa/gh-action-pypi-publish). This
10+
action gets called on all pushes to master (such as a version branch being merged
11+
into master), but only releases a new version when the version number has changed. Note,
12+
this action assumes pushes to master are version updates. Consequently,
13+
[pypi-release.yml](.github/workflows/pypi-release.yml) will fail if you attempt to
14+
push to master without updating `__version__` in
15+
[cloudevents](cloudevents/__init__.py) so don't forget to do so.
16+
17+
After a version update is merged, the script [pypi_packaging.py](pypi_packaging.py)
18+
will create a GitHub tag for the new cloudevents version using `__version__`.
19+
The script fails if `__version__` and the local pypi version for
20+
cloudevents are out of sync. For this reason, [pypi-release.yml](.github/workflows/pypi-release.yml)
21+
first must upload the new cloudevents pypi package, and then download the recently updated pypi
22+
cloudevents package for [pypi_packaging.py](pypi_packaging.py) not to fail.
23+
24+
View the GitHub workflow [pypi-release.yml](.github/workflows/pypi-release.yml) for
25+
more information.

cloudevents/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "1.0.0"

cloudevents/http/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
import json
15+
import typing
16+
17+
from cloudevents.http.event import CloudEvent
18+
from cloudevents.http.http_methods import (
19+
from_http,
20+
to_binary_http,
21+
to_structured_http,
22+
)
23+
from cloudevents.http.json_methods import from_json, to_json

0 commit comments

Comments
 (0)