Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed _http suffix from http_methods #108

Merged
merged 7 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Below we will provide samples on how to send cloudevents using the popular
### Binary HTTP CloudEvent

```python
from cloudevents.http import CloudEvent, to_binary_http
from cloudevents.http import CloudEvent, to_binary
import requests


Expand All @@ -36,7 +36,7 @@ attributes = {
data = {"message": "Hello World!"}

event = CloudEvent(attributes, data)
headers, body = to_binary_http(event)
headers, body = to_binary(event)

# POST
requests.post("<some-url>", data=body, headers=headers)
Expand All @@ -45,7 +45,7 @@ requests.post("<some-url>", data=body, headers=headers)
### Structured HTTP CloudEvent

```python
from cloudevents.http import CloudEvent, to_structured_http
from cloudevents.http import CloudEvent, to_structured
import requests


Expand All @@ -56,7 +56,7 @@ attributes = {
}
data = {"message": "Hello World!"}
event = CloudEvent(attributes, data)
headers, body = to_structured_http(event)
headers, body = to_structured(event)

# POST
requests.post("<some-url>", data=body, headers=headers)
Expand Down
2 changes: 2 additions & 0 deletions cloudevents/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from cloudevents.http.event_type import is_binary, is_structured
from cloudevents.http.http_methods import (
from_http,
to_binary,
to_binary_http,
to_structured,
to_structured_http,
)
from cloudevents.http.json_methods import from_json, to_json
20 changes: 18 additions & 2 deletions cloudevents/http/http_methods.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import typing

from deprecation import deprecated

import cloudevents.exceptions as cloud_exceptions
from cloudevents.http.event import CloudEvent
from cloudevents.http.event_type import is_binary, is_structured
Expand Down Expand Up @@ -107,7 +109,7 @@ def _to_http(
)


def to_structured_http(
def to_structured(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
) -> (dict, typing.Union[bytes, str]):
"""
Expand All @@ -123,7 +125,7 @@ def to_structured_http(
return _to_http(event=event, data_marshaller=data_marshaller)


def to_binary_http(
def to_binary(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
) -> (dict, typing.Union[bytes, str]):
"""
Expand All @@ -141,3 +143,17 @@ def to_binary_http(
format=converters.TypeBinary,
data_marshaller=data_marshaller,
)


@deprecated(deprecated_in="1.0.2", details="Use to_binary function instead")
def to_binary_http(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
) -> (dict, typing.Union[bytes, str]):
return to_binary(event, data_marshaller)


@deprecated(deprecated_in="1.0.2", details="Use to_structured function instead")
def to_structured_http(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
) -> (dict, typing.Union[bytes, str]):
return to_structured(event, data_marshaller)
4 changes: 2 additions & 2 deletions cloudevents/http/json_methods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typing

from cloudevents.http.event import CloudEvent
from cloudevents.http.http_methods import from_http, to_structured_http
from cloudevents.http.http_methods import from_http, to_structured
from cloudevents.sdk import types


Expand All @@ -17,7 +17,7 @@ def to_json(
:type data_marshaller: typing.Callable
:returns: json object representing the given event
"""
return to_structured_http(event, data_marshaller=data_marshaller)[1]
return to_structured(event, data_marshaller=data_marshaller)[1]


def from_json(
Expand Down
37 changes: 37 additions & 0 deletions cloudevents/tests/test_deprecated_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# 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 pytest

from cloudevents.http import (
CloudEvent,
to_binary,
to_binary_http,
to_structured,
to_structured_http,
)


@pytest.fixture
def event():
return CloudEvent({"source": "s", "type": "t"}, None)


def test_to_binary_http_deprecated(event):
with pytest.deprecated_call():
assert to_binary(event) == to_binary_http(event)


def test_to_structured_http_deprecated(event):
with pytest.deprecated_call():
assert to_structured(event) == to_structured_http(event)
11 changes: 3 additions & 8 deletions cloudevents/tests/test_event_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@

import pytest

from cloudevents.http import (
CloudEvent,
from_http,
to_binary_http,
to_structured_http,
)
from cloudevents.http import CloudEvent, from_http, to_binary, to_structured

test_data = json.dumps({"data-key": "val"})
test_attributes = {
Expand All @@ -39,7 +34,7 @@ def test_cloudevent_access_extensions(specversion):
@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
def test_to_binary_extensions(specversion):
event = CloudEvent(test_attributes, test_data)
headers, body = to_binary_http(event)
headers, body = to_binary(event)

assert "ce-ext1" in headers
assert headers.get("ce-ext1") == test_attributes["ext1"]
Expand All @@ -65,7 +60,7 @@ def test_from_binary_extensions(specversion):
@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
def test_to_structured_extensions(specversion):
event = CloudEvent(test_attributes, test_data)
headers, body = to_structured_http(event)
headers, body = to_structured(event)

body = json.loads(body)

Expand Down
18 changes: 10 additions & 8 deletions cloudevents/tests/test_http_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from_http,
is_binary,
is_structured,
to_binary,
to_binary_http,
to_structured,
to_structured_http,
)
from cloudevents.sdk import converters
Expand Down Expand Up @@ -174,9 +176,9 @@ def test_roundtrip_non_json_event(converter, specversion):
event = CloudEvent(attrs, compressed_data)

if converter == converters.TypeStructured:
headers, data = to_structured_http(event, data_marshaller=lambda x: x)
headers, data = to_structured(event, data_marshaller=lambda x: x)
elif converter == converters.TypeBinary:
headers, data = to_binary_http(event, data_marshaller=lambda x: x)
headers, data = to_binary(event, data_marshaller=lambda x: x)

headers["binary-payload"] = "true" # Decoding hint for server
_, r = app.test_client.post("/event", headers=headers, data=data)
Expand Down Expand Up @@ -244,7 +246,7 @@ def test_structured_to_request(specversion):
data = {"message": "Hello World!"}

event = CloudEvent(attributes, data)
headers, body_bytes = to_structured_http(event)
headers, body_bytes = to_structured(event)
assert isinstance(body_bytes, bytes)
body = json.loads(body_bytes)

Expand All @@ -264,7 +266,7 @@ def test_binary_to_request(specversion):
}
data = {"message": "Hello World!"}
event = CloudEvent(attributes, data)
headers, body_bytes = to_binary_http(event)
headers, body_bytes = to_binary(event)
body = json.loads(body_bytes)

for key in data:
Expand Down Expand Up @@ -395,8 +397,8 @@ def test_none_data_cloudevent(specversion):
"specversion": specversion,
}
)
to_binary_http(event)
to_structured_http(event)
to_binary(event)
to_structured(event)


def test_wrong_specversion():
Expand Down Expand Up @@ -426,7 +428,7 @@ def test_wrong_specversion_to_request():
event = CloudEvent({"source": "s", "type": "t"}, None)
with pytest.raises(cloud_exceptions.CloudEventTypeErrorRequiredFields) as e:
event["specversion"] = "0.2"
to_binary_http(event)
to_binary(event)
assert "Unsupported specversion: 0.2" in str(e.value)


Expand Down Expand Up @@ -467,5 +469,5 @@ def test_uppercase_headers_with_none_data_binary():
assert event[key.lower()[3:]] == headers[key]
assert event.data == None

_, new_data = to_binary_http(event)
_, new_data = to_binary(event)
assert new_data == None
8 changes: 4 additions & 4 deletions samples/http-image-cloudevents/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import requests

from cloudevents.http import CloudEvent, to_binary_http, to_structured_http
from cloudevents.http import CloudEvent, to_binary, to_structured

resp = requests.get(
"https://raw.githubusercontent.com/cncf/artwork/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png"
Expand All @@ -33,7 +33,7 @@ def send_binary_cloud_event(url: str):
event = CloudEvent(attributes, image_bytes)

# Create cloudevent HTTP headers and content
headers, body = to_binary_http(event)
headers, body = to_binary(event)

# Send cloudevent
requests.post(url, headers=headers, data=body)
Expand All @@ -50,10 +50,10 @@ def send_structured_cloud_event(url: str):
event = CloudEvent(attributes, image_bytes)

# Create cloudevent HTTP headers and content
# Note that to_structured_http will create a data_base64 data field in
# Note that to_structured will create a data_base64 data field in
# specversion 1.0 (default specversion) if given
# an event whose data field is of type bytes.
headers, body = to_structured_http(event)
headers, body = to_structured(event)

# Send cloudevent
requests.post(url, headers=headers, data=body)
Expand Down
17 changes: 6 additions & 11 deletions samples/http-image-cloudevents/image_sample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
from image_sample_server import app
from PIL import Image

from cloudevents.http import (
CloudEvent,
from_http,
to_binary_http,
to_structured_http,
)
from cloudevents.http import CloudEvent, from_http, to_binary, to_structured

image_fileobj = io.BytesIO(image_bytes)
image_expected_shape = (1880, 363)
Expand All @@ -35,7 +30,7 @@ def test_create_binary_image():
event = CloudEvent(attributes, image_bytes)

# Create http headers/body content
headers, body = to_binary_http(event)
headers, body = to_binary(event)

# Unmarshall CloudEvent and re-create image
reconstruct_event = from_http(
Expand All @@ -62,7 +57,7 @@ def test_create_structured_image():
event = CloudEvent(attributes, image_bytes)

# Create http headers/body content
headers, body = to_structured_http(event)
headers, body = to_structured(event)

# Structured has cloudevent attributes marshalled inside the body. For this
# reason we must load the byte object to create the python dict containing
Expand Down Expand Up @@ -92,10 +87,10 @@ def test_server_structured(client):
event = CloudEvent(attributes, image_bytes)

# Create cloudevent HTTP headers and content
# Note that to_structured_http will create a data_base64 data field in
# Note that to_structured will create a data_base64 data field in
# specversion 1.0 (default specversion) if given
# an event whose data field is of type bytes.
headers, body = to_structured_http(event)
headers, body = to_structured(event)

# Send cloudevent
r = client.post("/", headers=headers, data=body)
Expand All @@ -113,7 +108,7 @@ def test_server_binary(client):
event = CloudEvent(attributes, image_bytes)

# Create cloudevent HTTP headers and content
headers, body = to_binary_http(event)
headers, body = to_binary(event)

# Send cloudevent
r = client.post("/", headers=headers, data=body)
Expand Down
6 changes: 3 additions & 3 deletions samples/http-json-cloudevents/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import requests

from cloudevents.http import CloudEvent, to_binary_http, to_structured_http
from cloudevents.http import CloudEvent, to_binary, to_structured


def send_binary_cloud_event(url):
Expand All @@ -28,7 +28,7 @@ def send_binary_cloud_event(url):
data = {"message": "Hello World!"}

event = CloudEvent(attributes, data)
headers, body = to_binary_http(event)
headers, body = to_binary(event)

# send and print event
requests.post(url, headers=headers, data=body)
Expand All @@ -44,7 +44,7 @@ def send_structured_cloud_event(url):
data = {"message": "Hello World!"}

event = CloudEvent(attributes, data)
headers, body = to_structured_http(event)
headers, body = to_structured(event)

# send and print event
requests.post(url, headers=headers, data=body)
Expand Down
6 changes: 3 additions & 3 deletions samples/http-json-cloudevents/json_sample_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from json_sample_server import app

from cloudevents.http import CloudEvent, to_binary_http, to_structured_http
from cloudevents.http import CloudEvent, to_binary, to_structured


@pytest.fixture
Expand All @@ -19,7 +19,7 @@ def test_binary_request(client):
data = {"message": "Hello World!"}

event = CloudEvent(attributes, data)
headers, body = to_binary_http(event)
headers, body = to_binary(event)

r = client.post("/", headers=headers, data=body)
assert r.status_code == 204
Expand All @@ -34,7 +34,7 @@ def test_structured_request(client):
data = {"message": "Hello World!"}

event = CloudEvent(attributes, data)
headers, body = to_structured_http(event)
headers, body = to_structured(event)

r = client.post("/", headers=headers, data=body)
assert r.status_code == 204
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
],
packages=find_packages(exclude=["cloudevents.tests"]),
version=pypi_config["version_target"],
install_requires=["deprecation>=2.0,<3.0"],
)