Skip to content

Commit 939bdb1

Browse files
cumason123di
andauthored
Removed _http suffix from http_methods (cloudevents#108)
* Removed _http suffix from http_methods to_binary_http renamed to_binary, and to_structured_http renamed to_structured. These functions are inside of cloudevents.http thus the _http part should be implicitly understood. Signed-off-by: Curtis Mason <cumason@google.com> * version bump Signed-off-by: Curtis Mason <cumason@google.com> * deprecated instead of removal Signed-off-by: Curtis Mason <cumason@bu.edu> * Update setup.py Co-authored-by: Dustin Ingram <di@users.noreply.github.com> Signed-off-by: Curtis Mason <cumason@bu.edu> * 1.1.0 version bump Signed-off-by: Curtis Mason <cumason@bu.edu> Co-authored-by: Dustin Ingram <di@users.noreply.github.com>
1 parent 3c26bcf commit 939bdb1

File tree

12 files changed

+93
-45
lines changed

12 files changed

+93
-45
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Below we will provide samples on how to send cloudevents using the popular
2424
### Binary HTTP CloudEvent
2525

2626
```python
27-
from cloudevents.http import CloudEvent, to_binary_http
27+
from cloudevents.http import CloudEvent, to_binary
2828
import requests
2929

3030

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

3838
event = CloudEvent(attributes, data)
39-
headers, body = to_binary_http(event)
39+
headers, body = to_binary(event)
4040

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

4747
```python
48-
from cloudevents.http import CloudEvent, to_structured_http
48+
from cloudevents.http import CloudEvent, to_structured
4949
import requests
5050

5151

@@ -56,7 +56,7 @@ attributes = {
5656
}
5757
data = {"message": "Hello World!"}
5858
event = CloudEvent(attributes, data)
59-
headers, body = to_structured_http(event)
59+
headers, body = to_structured(event)
6060

6161
# POST
6262
requests.post("<some-url>", data=body, headers=headers)

cloudevents/http/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
from cloudevents.http.event_type import is_binary, is_structured
1919
from cloudevents.http.http_methods import (
2020
from_http,
21+
to_binary,
2122
to_binary_http,
23+
to_structured,
2224
to_structured_http,
2325
)
2426
from cloudevents.http.json_methods import from_json, to_json

cloudevents/http/http_methods.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import typing
33

4+
from deprecation import deprecated
5+
46
import cloudevents.exceptions as cloud_exceptions
57
from cloudevents.http.event import CloudEvent
68
from cloudevents.http.event_type import is_binary, is_structured
@@ -107,7 +109,7 @@ def _to_http(
107109
)
108110

109111

110-
def to_structured_http(
112+
def to_structured(
111113
event: CloudEvent, data_marshaller: types.MarshallerType = None,
112114
) -> (dict, typing.Union[bytes, str]):
113115
"""
@@ -123,7 +125,7 @@ def to_structured_http(
123125
return _to_http(event=event, data_marshaller=data_marshaller)
124126

125127

126-
def to_binary_http(
128+
def to_binary(
127129
event: CloudEvent, data_marshaller: types.MarshallerType = None,
128130
) -> (dict, typing.Union[bytes, str]):
129131
"""
@@ -141,3 +143,17 @@ def to_binary_http(
141143
format=converters.TypeBinary,
142144
data_marshaller=data_marshaller,
143145
)
146+
147+
148+
@deprecated(deprecated_in="1.0.2", details="Use to_binary function instead")
149+
def to_binary_http(
150+
event: CloudEvent, data_marshaller: types.MarshallerType = None,
151+
) -> (dict, typing.Union[bytes, str]):
152+
return to_binary(event, data_marshaller)
153+
154+
155+
@deprecated(deprecated_in="1.0.2", details="Use to_structured function instead")
156+
def to_structured_http(
157+
event: CloudEvent, data_marshaller: types.MarshallerType = None,
158+
) -> (dict, typing.Union[bytes, str]):
159+
return to_structured(event, data_marshaller)

cloudevents/http/json_methods.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing
22

33
from cloudevents.http.event import CloudEvent
4-
from cloudevents.http.http_methods import from_http, to_structured_http
4+
from cloudevents.http.http_methods import from_http, to_structured
55
from cloudevents.sdk import types
66

77

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

2222

2323
def from_json(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 pytest
15+
16+
from cloudevents.http import (
17+
CloudEvent,
18+
to_binary,
19+
to_binary_http,
20+
to_structured,
21+
to_structured_http,
22+
)
23+
24+
25+
@pytest.fixture
26+
def event():
27+
return CloudEvent({"source": "s", "type": "t"}, None)
28+
29+
30+
def test_to_binary_http_deprecated(event):
31+
with pytest.deprecated_call():
32+
assert to_binary(event) == to_binary_http(event)
33+
34+
35+
def test_to_structured_http_deprecated(event):
36+
with pytest.deprecated_call():
37+
assert to_structured(event) == to_structured_http(event)

cloudevents/tests/test_event_extensions.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515

1616
import pytest
1717

18-
from cloudevents.http import (
19-
CloudEvent,
20-
from_http,
21-
to_binary_http,
22-
to_structured_http,
23-
)
18+
from cloudevents.http import CloudEvent, from_http, to_binary, to_structured
2419

2520
test_data = json.dumps({"data-key": "val"})
2621
test_attributes = {
@@ -39,7 +34,7 @@ def test_cloudevent_access_extensions(specversion):
3934
@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
4035
def test_to_binary_extensions(specversion):
4136
event = CloudEvent(test_attributes, test_data)
42-
headers, body = to_binary_http(event)
37+
headers, body = to_binary(event)
4338

4439
assert "ce-ext1" in headers
4540
assert headers.get("ce-ext1") == test_attributes["ext1"]
@@ -65,7 +60,7 @@ def test_from_binary_extensions(specversion):
6560
@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
6661
def test_to_structured_extensions(specversion):
6762
event = CloudEvent(test_attributes, test_data)
68-
headers, body = to_structured_http(event)
63+
headers, body = to_structured(event)
6964

7065
body = json.loads(body)
7166

cloudevents/tests/test_http_events.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
from_http,
2727
is_binary,
2828
is_structured,
29+
to_binary,
2930
to_binary_http,
31+
to_structured,
3032
to_structured_http,
3133
)
3234
from cloudevents.sdk import converters
@@ -174,9 +176,9 @@ def test_roundtrip_non_json_event(converter, specversion):
174176
event = CloudEvent(attrs, compressed_data)
175177

176178
if converter == converters.TypeStructured:
177-
headers, data = to_structured_http(event, data_marshaller=lambda x: x)
179+
headers, data = to_structured(event, data_marshaller=lambda x: x)
178180
elif converter == converters.TypeBinary:
179-
headers, data = to_binary_http(event, data_marshaller=lambda x: x)
181+
headers, data = to_binary(event, data_marshaller=lambda x: x)
180182

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

246248
event = CloudEvent(attributes, data)
247-
headers, body_bytes = to_structured_http(event)
249+
headers, body_bytes = to_structured(event)
248250
assert isinstance(body_bytes, bytes)
249251
body = json.loads(body_bytes)
250252

@@ -264,7 +266,7 @@ def test_binary_to_request(specversion):
264266
}
265267
data = {"message": "Hello World!"}
266268
event = CloudEvent(attributes, data)
267-
headers, body_bytes = to_binary_http(event)
269+
headers, body_bytes = to_binary(event)
268270
body = json.loads(body_bytes)
269271

270272
for key in data:
@@ -395,8 +397,8 @@ def test_none_data_cloudevent(specversion):
395397
"specversion": specversion,
396398
}
397399
)
398-
to_binary_http(event)
399-
to_structured_http(event)
400+
to_binary(event)
401+
to_structured(event)
400402

401403

402404
def test_wrong_specversion():
@@ -426,7 +428,7 @@ def test_wrong_specversion_to_request():
426428
event = CloudEvent({"source": "s", "type": "t"}, None)
427429
with pytest.raises(cloud_exceptions.CloudEventTypeErrorRequiredFields) as e:
428430
event["specversion"] = "0.2"
429-
to_binary_http(event)
431+
to_binary(event)
430432
assert "Unsupported specversion: 0.2" in str(e.value)
431433

432434

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

470-
_, new_data = to_binary_http(event)
472+
_, new_data = to_binary(event)
471473
assert new_data == None

samples/http-image-cloudevents/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import requests
1717

18-
from cloudevents.http import CloudEvent, to_binary_http, to_structured_http
18+
from cloudevents.http import CloudEvent, to_binary, to_structured
1919

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

3535
# Create cloudevent HTTP headers and content
36-
headers, body = to_binary_http(event)
36+
headers, body = to_binary(event)
3737

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

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

5858
# Send cloudevent
5959
requests.post(url, headers=headers, data=body)

samples/http-image-cloudevents/image_sample_test.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
from image_sample_server import app
88
from PIL import Image
99

10-
from cloudevents.http import (
11-
CloudEvent,
12-
from_http,
13-
to_binary_http,
14-
to_structured_http,
15-
)
10+
from cloudevents.http import CloudEvent, from_http, to_binary, to_structured
1611

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

3732
# Create http headers/body content
38-
headers, body = to_binary_http(event)
33+
headers, body = to_binary(event)
3934

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

6459
# Create http headers/body content
65-
headers, body = to_structured_http(event)
60+
headers, body = to_structured(event)
6661

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

9489
# Create cloudevent HTTP headers and content
95-
# Note that to_structured_http will create a data_base64 data field in
90+
# Note that to_structured will create a data_base64 data field in
9691
# specversion 1.0 (default specversion) if given
9792
# an event whose data field is of type bytes.
98-
headers, body = to_structured_http(event)
93+
headers, body = to_structured(event)
9994

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

115110
# Create cloudevent HTTP headers and content
116-
headers, body = to_binary_http(event)
111+
headers, body = to_binary(event)
117112

118113
# Send cloudevent
119114
r = client.post("/", headers=headers, data=body)

samples/http-json-cloudevents/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import requests
1818

19-
from cloudevents.http import CloudEvent, to_binary_http, to_structured_http
19+
from cloudevents.http import CloudEvent, to_binary, to_structured
2020

2121

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

3030
event = CloudEvent(attributes, data)
31-
headers, body = to_binary_http(event)
31+
headers, body = to_binary(event)
3232

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

4646
event = CloudEvent(attributes, data)
47-
headers, body = to_structured_http(event)
47+
headers, body = to_structured(event)
4848

4949
# send and print event
5050
requests.post(url, headers=headers, data=body)

samples/http-json-cloudevents/json_sample_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from json_sample_server import app
33

4-
from cloudevents.http import CloudEvent, to_binary_http, to_structured_http
4+
from cloudevents.http import CloudEvent, to_binary, to_structured
55

66

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

2121
event = CloudEvent(attributes, data)
22-
headers, body = to_binary_http(event)
22+
headers, body = to_binary(event)
2323

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

3636
event = CloudEvent(attributes, data)
37-
headers, body = to_structured_http(event)
37+
headers, body = to_structured(event)
3838

3939
r = client.post("/", headers=headers, data=body)
4040
assert r.status_code == 204

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@
4141
],
4242
packages=find_packages(exclude=["cloudevents.tests"]),
4343
version=pypi_config["version_target"],
44+
install_requires=["deprecation>=2.0,<3.0"],
4445
)

0 commit comments

Comments
 (0)