Skip to content

Commit be80247

Browse files
authored
swapped args for from_http (cloudevents#110)
Signed-off-by: Curtis Mason <cumason@bu.edu>
1 parent 939bdb1 commit be80247

File tree

8 files changed

+27
-26
lines changed

8 files changed

+27
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ app = Flask(__name__)
8181
@app.route("/", methods=["POST"])
8282
def home():
8383
# create a CloudEvent
84-
event = from_http(request.get_data(), request.headers)
84+
event = from_http(request.headers, request.get_data())
8585

8686
# you can access cloudevent fields as seen below
8787
print(

cloudevents/http/http_methods.py

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

1313

1414
def from_http(
15-
data: typing.Union[str, bytes, None],
1615
headers: typing.Dict[str, str],
16+
data: typing.Union[str, bytes, None],
1717
data_unmarshaller: types.UnmarshallerType = None,
1818
):
1919
"""
2020
Unwrap a CloudEvent (binary or structured) from an HTTP request.
21-
:param data: the HTTP request body
22-
:type data: typing.IO
2321
:param headers: the HTTP headers
2422
:type headers: typing.Dict[str, str]
23+
:param data: the HTTP request body
24+
:type data: typing.IO
2525
:param data_unmarshaller: Callable function to map data to a python object
2626
e.g. lambda x: x or lambda x: json.loads(x)
2727
:type data_unmarshaller: types.UnmarshallerType

cloudevents/http/json_methods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ def from_json(
3333
:type data_unmarshaller: typing.Callable
3434
:returns: CloudEvent representing given cloudevent json object
3535
"""
36-
return from_http(data=data, headers={}, data_unmarshaller=data_unmarshaller)
36+
return from_http(headers={}, data=data, data_unmarshaller=data_unmarshaller)

cloudevents/tests/test_event_extensions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_from_binary_extensions(specversion):
5151
"ce-ext2": "test2",
5252
}
5353
body = json.dumps({"data-key": "val"})
54-
event = from_http(body, headers)
54+
event = from_http(headers, body)
5555

5656
assert headers["ce-ext1"] == event["ext1"]
5757
assert headers["ce-ext2"] == event["ext2"]
@@ -81,7 +81,7 @@ def test_from_structured_extensions(specversion):
8181
}
8282

8383
data = json.dumps(body)
84-
event = from_http(data, headers)
84+
event = from_http(headers, data)
8585

8686
assert body["ext1"] == event["ext1"]
8787
assert body["ext2"] == event["ext2"]

cloudevents/tests/test_http_events.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async def echo(request):
7878
if "binary-payload" in request.headers:
7979
decoder = lambda x: x
8080
event = from_http(
81-
request.body, headers=dict(request.headers), data_unmarshaller=decoder
81+
dict(request.headers), request.body, data_unmarshaller=decoder
8282
)
8383
data = (
8484
event.data
@@ -96,8 +96,7 @@ def test_missing_required_fields_structured(body):
9696
# implemented. In this instance one of the required keys should have
9797
# prefix e-id instead of ce-id therefore it should throw
9898
_ = from_http(
99-
json.dumps(body),
100-
headers={"Content-Type": "application/cloudevents+json"},
99+
{"Content-Type": "application/cloudevents+json"}, json.dumps(body),
101100
)
102101

103102

@@ -108,7 +107,7 @@ def test_missing_required_fields_binary(headers):
108107
# and NotImplementedError because structured calls aren't
109108
# implemented. In this instance one of the required keys should have
110109
# prefix e-id instead of ce-id therefore it should throw
111-
_ = from_http(json.dumps(test_data), headers=headers)
110+
_ = from_http(headers, json.dumps(test_data))
112111

113112

114113
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
@@ -208,7 +207,7 @@ def test_missing_ce_prefix_binary_event(specversion):
208207
# and NotImplementedError because structured calls aren't
209208
# implemented. In this instance one of the required keys should have
210209
# prefix e-id instead of ce-id therefore it should throw
211-
_ = from_http(json.dumps(test_data), headers=prefixed_headers)
210+
_ = from_http(prefixed_headers, json.dumps(test_data))
212211

213212

214213
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
@@ -225,7 +224,7 @@ def test_valid_binary_events(specversion):
225224
"ce-specversion": specversion,
226225
}
227226
data = {"payload": f"payload-{i}"}
228-
events_queue.append(from_http(json.dumps(data), headers=headers))
227+
events_queue.append(from_http(headers, json.dumps(data)))
229228

230229
for i, event in enumerate(events_queue):
231230
data = event.data
@@ -288,7 +287,7 @@ def test_empty_data_structured_event(specversion):
288287
}
289288

290289
_ = from_http(
291-
json.dumps(attributes), {"content-type": "application/cloudevents+json"}
290+
{"content-type": "application/cloudevents+json"}, json.dumps(attributes)
292291
)
293292

294293

@@ -303,7 +302,7 @@ def test_empty_data_binary_event(specversion):
303302
"ce-time": "2018-10-23T12:28:22.4579346Z",
304303
"ce-source": "<source-url>",
305304
}
306-
_ = from_http("", headers)
305+
_ = from_http(headers, "")
307306

308307

309308
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
@@ -321,8 +320,8 @@ def test_valid_structured_events(specversion):
321320
}
322321
events_queue.append(
323322
from_http(
324-
json.dumps(event),
325323
{"content-type": "application/cloudevents+json"},
324+
json.dumps(event),
326325
)
327326
)
328327

@@ -343,7 +342,7 @@ def test_structured_no_content_type(specversion):
343342
"specversion": specversion,
344343
"data": test_data,
345344
}
346-
event = from_http(json.dumps(data), {},)
345+
event = from_http({}, json.dumps(data))
347346

348347
assert event["id"] == "id"
349348
assert event["source"] == "source.com.test"
@@ -381,7 +380,7 @@ def test_cloudevent_repr(specversion):
381380
"ce-time": "2018-10-23T12:28:22.4579346Z",
382381
"ce-source": "<source-url>",
383382
}
384-
event = from_http("", headers)
383+
event = from_http(headers, "")
385384
# Testing to make sure event is printable. I could runevent. __repr__() but
386385
# we had issues in the past where event.__repr__() could run but
387386
# print(event) would fail.
@@ -412,15 +411,15 @@ def test_wrong_specversion():
412411
}
413412
)
414413
with pytest.raises(cloud_exceptions.CloudEventTypeErrorRequiredFields) as e:
415-
from_http(data, headers)
414+
from_http(headers, data)
416415
assert "Found invalid specversion 0.2" in str(e.value)
417416

418417

419418
def test_invalid_data_format_structured_from_http():
420419
headers = {"Content-Type": "application/cloudevents+json"}
421420
data = 20
422421
with pytest.raises(cloud_exceptions.InvalidStructuredJSON) as e:
423-
from_http(data, headers)
422+
from_http(headers, data)
424423
assert "Expected json of type (str, bytes, bytearray)" in str(e.value)
425424

426425

@@ -452,7 +451,9 @@ def test_empty_json_structured():
452451
headers = {"Content-Type": "application/cloudevents+json"}
453452
data = ""
454453
with pytest.raises(cloud_exceptions.InvalidStructuredJSON) as e:
455-
from_http(data, headers)
454+
from_http(
455+
headers, data,
456+
)
456457
assert "Failed to read fields from structured event. " in str(e.value)
457458

458459

@@ -463,7 +464,7 @@ def test_uppercase_headers_with_none_data_binary():
463464
"Ce-Type": "cloudevent.event.type",
464465
"Ce-Specversion": "1.0",
465466
}
466-
event = from_http(None, headers)
467+
event = from_http(headers, None)
467468

468469
for key in headers:
469470
assert event[key.lower()[3:]] == headers[key]

samples/http-image-cloudevents/image_sample_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def home():
2626
# Create a CloudEvent.
2727
# data_unmarshaller will cast event.data into an io.BytesIO object
2828
event = from_http(
29-
request.get_data(),
3029
request.headers,
30+
request.get_data(),
3131
data_unmarshaller=lambda x: io.BytesIO(x),
3232
)
3333

samples/http-image-cloudevents/image_sample_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_create_binary_image():
3434

3535
# Unmarshall CloudEvent and re-create image
3636
reconstruct_event = from_http(
37-
body, headers, data_unmarshaller=lambda x: io.BytesIO(x)
37+
headers, body, data_unmarshaller=lambda x: io.BytesIO(x)
3838
)
3939

4040
# reconstruct_event.data is an io.BytesIO object due to data_unmarshaller
@@ -70,7 +70,7 @@ def test_create_structured_image():
7070

7171
# Unmarshall CloudEvent and re-create image
7272
reconstruct_event = from_http(
73-
body, headers, data_unmarshaller=lambda x: io.BytesIO(x)
73+
headers, body, data_unmarshaller=lambda x: io.BytesIO(x)
7474
)
7575

7676
# reconstruct_event.data is an io.BytesIO object due to data_unmarshaller

samples/http-json-cloudevents/json_sample_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@app.route("/", methods=["POST"])
2323
def home():
2424
# create a CloudEvent
25-
event = from_http(request.get_data(), request.headers)
25+
event = from_http(request.headers, request.get_data())
2626

2727
# you can access cloudevent fields as seen below
2828
print(

0 commit comments

Comments
 (0)