Skip to content

Commit 2eb4d66

Browse files
committed
chore: update contracts, tests and linting errors.
1 parent f244469 commit 2eb4d66

10 files changed

Lines changed: 88 additions & 21 deletions

File tree

contracts

nitric/api/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def publish(
5959
payload = {}
6060
payload_struct = Struct()
6161
payload_struct.update(payload)
62-
nitric_event = NitricEvent(id=event_id, payloadType=payload_type, payload=payload_struct)
62+
nitric_event = NitricEvent(id=event_id, payload_type=payload_type, payload=payload_struct)
6363
request = event_model.EventPublishRequest(topic=topic_name, event=nitric_event)
6464
self._exec("Publish", request)
6565
return event_id

nitric/api/queue.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _task_to_wire(self, task: Task) -> queue.NitricTask:
5959

6060
return queue.NitricTask(
6161
id=task.task_id,
62-
payloadType=task.payload_type,
62+
payload_type=task.payload_type,
6363
payload=payload_struct,
6464
)
6565

@@ -72,9 +72,9 @@ def _wire_to_task(self, task: queue.NitricTask) -> Task:
7272
"""
7373
return Task(
7474
task_id=task.id,
75-
payload_type=task.payloadType,
75+
payload_type=task.payload_type,
7676
payload=MessageToDict(task.payload),
77-
lease_id=task.leaseId,
77+
lease_id=task.lease_id,
7878
)
7979

8080
def _wire_to_failed_task(self, failed_task: queue.FailedTask) -> FailedTask:

nitric/api/storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def write(self, bucket_name: str, key: str, body: bytes):
4242
:param body: data to be stored.
4343
:return: storage result.
4444
"""
45-
request = storage.StorageWriteRequest(bucketName=bucket_name, key=key, body=body)
45+
request = storage.StorageWriteRequest(bucket_name=bucket_name, key=key, body=body)
4646
response = self._exec("Write", request)
4747
return response
4848

@@ -54,6 +54,6 @@ def read(self, bucket_name: str, key: str) -> bytes:
5454
:param key: key for the file to retrieve.
5555
:return: the file as bytes.
5656
"""
57-
request = storage.StorageReadRequest(bucketName=bucket_name, key=key)
57+
request = storage.StorageReadRequest(bucket_name=bucket_name, key=key)
5858
response = self._exec("Read", request)
5959
return response.body

nitric/faas/response.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,73 @@
2222

2323

2424
class TopicResponseContext(object):
25+
"""Response context for triggers raised by a topic event."""
26+
2527
def __init__(self, success: bool = True):
28+
"""Create a Topic response context."""
2629
self.success = success
2730

2831
def to_grpc_topic_response_context(self) -> faas_pb2.TopicResponseContext:
32+
"""Translate a topic response context for wire transport."""
2933
return faas_pb2.TopicResponseContext(success=self.success)
3034

3135

3236
class HttpResponseContext(object):
33-
def __init__(self, headers: Dict[str, str] = {}, status: int = 200):
34-
self.headers = headers
37+
"""Response context for triggers raised by a HTTP request."""
38+
39+
def __init__(self, headers: Dict[str, str] = None, status: int = 200):
40+
"""Create a HTTP response context."""
41+
if headers is None:
42+
self.headers = {}
43+
else:
44+
self.headers = headers
3545
self.status = status
3646

3747
def to_grpc_http_response_context(self) -> faas_pb2.HttpResponseContext:
48+
"""Translate a HTTP response context for on wire transport."""
3849
return faas_pb2.HttpResponseContext(headers=self.headers, status=self.status)
3950

4051

4152
class ResponseContext(object):
53+
"""Wrapper for typed response context for triggers."""
54+
4255
def __init__(self, context: Union[TopicResponseContext, HttpResponseContext]):
56+
"""Create a new response context wrapper."""
4357
self.context = context
4458

4559
def is_http(self):
60+
"""
61+
Determine if response is for a HTTP request.
62+
63+
:return true if context is for a HTTP request.
64+
"""
4665
return isinstance(self.context, HttpResponseContext)
4766

4867
def is_topic(self):
68+
"""
69+
Determine if response is for an event raised by a topic.
70+
71+
:return true if context is for a topic event.
72+
"""
4973
return isinstance(self.context, TopicResponseContext)
5074

5175
def as_http(self) -> Union[HttpResponseContext, None]:
76+
"""
77+
Unwraps response context as Http response context.
78+
79+
:return HttpResponseContext if is_http is true otherwise return None
80+
"""
5281
if not self.is_http():
5382
return None
5483

5584
return self.context
5685

5786
def as_topic(self) -> Union[TopicResponseContext, None]:
58-
"""Determine is ResponseContext is for a topic"""
87+
"""
88+
Unwraps response context as Topic response context.
89+
90+
:return TopicResponseContext if is_topic is true otherwise return None
91+
"""
5992
if not self.is_topic():
6093
return None
6194

@@ -76,8 +109,7 @@ def __init__(
76109
self.data = data
77110

78111
def to_grpc_trigger_response_context(self) -> TriggerResponse:
79-
"""Translate a response object ready for on the wire transport"""
80-
112+
"""Translate a response object ready for on the wire transport."""
81113
response = TriggerResponse(data=self.data)
82114

83115
if self.context.is_http():

nitric/faas/trigger.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
class HttpTriggerContext(object):
27-
"""Represents Trigger metadata from a HTTP subscription"""
27+
"""Represents Trigger metadata from a HTTP subscription."""
2828

2929
def __init__(
3030
self,
@@ -33,16 +33,18 @@ def __init__(
3333
headers: typing.Dict[str, str],
3434
query_params: typing.Dict[str, str],
3535
):
36+
"""Create a Http trigger context."""
3637
self.method = method
3738
self.path = path
3839
self.headers = headers
3940
self.query_params = query_params
4041

4142

4243
class TopicTriggerContext(object):
43-
"""Represents Trigger metadata from a topic subscription"""
44+
"""Represents Trigger metadata from a topic subscription."""
4445

4546
def __init__(self, topic: str):
47+
"""Create a Topic trigger context."""
4648
self.topic = topic
4749

4850

@@ -54,25 +56,50 @@ def __init__(self, context: typing.Union[TopicTriggerContext, HttpTriggerContext
5456
self.context = context
5557

5658
def is_http(self) -> bool:
59+
"""
60+
Determine if trigger was raised by a http request.
61+
62+
:return true if trigger was raised by a HTTP request
63+
"""
5764
return isinstance(self.context, HttpTriggerContext)
5865

5966
def as_http(self) -> typing.Union[HttpTriggerContext, None]:
67+
"""
68+
Unwrap HttpTriggerContext.
69+
70+
:return HttpTriggerContext if is_http is true otherwise None
71+
"""
6072
if not self.is_http():
6173
return None
6274

6375
return self.context
6476

6577
def is_topic(self) -> bool:
78+
"""
79+
Determine if trigger was raised by a topic event.
80+
81+
:return true if trigger is for a topic event
82+
"""
6683
return isinstance(self.context, TriggerContext)
6784

6885
def as_topic(self) -> typing.Union[TopicTriggerContext, None]:
86+
"""
87+
Unwrap TopicTriggerContext.
88+
89+
:return TopicTriggerContext if is_topic is true otherwise None
90+
"""
6991
if not self.is_topic():
7092
return None
7193

7294
return self.context
7395

7496
@staticmethod
7597
def from_trigger_request(trigger_request: TriggerRequest):
98+
"""
99+
Create a TriggerContext from a gRPC TriggerRequest.
100+
101+
:return Created TriggerContext
102+
"""
76103
if trigger_request.http is not None:
77104
return TriggerContext(
78105
context=HttpTriggerContext(
@@ -120,9 +147,12 @@ def get_object(self) -> dict:
120147

121148
def default_response(self) -> Response:
122149
"""
123-
Convenience method to construct a relevant default response
150+
Create a relevant default response.
151+
124152
The returned response can be interrogated with its context to determine the appropriate
125-
response context e.g. response.context.is_http() or response.context.is_topic()
153+
response context e.g. response.context.is_http() or response.context.is_topic().
154+
155+
:returns Default response for this Trigger
126156
"""
127157
response_ctx = None
128158

@@ -135,6 +165,11 @@ def default_response(self) -> Response:
135165

136166
@staticmethod
137167
def from_trigger_request(trigger_request: TriggerRequest):
168+
"""
169+
Create a Trigger from a gRPC TriggerRequest.
170+
171+
:returns Created Trigger
172+
"""
138173
context = TriggerContext.from_trigger_request(trigger_request)
139174

140175
return Trigger(context=context, data=trigger_request.data)

tests/api/test_event_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_publish():
4242
mock_publish.assert_called_once()
4343
assert mock_publish.call_args[0][0].topic == "topic_name"
4444
assert mock_publish.call_args[0][0].event.id == "abc-123"
45-
assert mock_publish.call_args[0][0].event.payloadType == "payload.type"
45+
assert mock_publish.call_args[0][0].event.payload_type == "payload.type"
4646
assert mock_publish.call_args[0][0].event.payload["content"] == "of event"
4747

4848

tests/api/test_queue_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_push():
3838
mock_push.assert_called_once()
3939
assert mock_push.call_args[0][0].queue == "test-queue"
4040
assert mock_push.call_args[0][0].tasks[0].id == "1234"
41-
assert mock_push.call_args[0][0].tasks[0].payloadType == "test-payload"
41+
assert mock_push.call_args[0][0].tasks[0].payload_type == "test-payload"
4242
assert mock_push.call_args[0][0].tasks[0].payload["test"] == "test"
4343

4444

tests/api/test_storage_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_write():
3636

3737
# Ensure the 'Put' method is called with the expected input
3838
mock_create.assert_called_once() # No input data required to get topics
39-
assert mock_create.call_args[0][0].bucketName == "bucket_name"
39+
assert mock_create.call_args[0][0].bucket_name == "bucket_name"
4040
assert mock_create.call_args[0][0].key == "key_name"
4141
assert mock_create.call_args[0][0].body == content
4242

@@ -55,7 +55,7 @@ def test_read():
5555

5656
# Ensure the 'Get' method is called with the expected input
5757
mock_create.assert_called_once() # No input data required to get topics
58-
assert mock_create.call_args[0][0].bucketName == "bucket_name"
58+
assert mock_create.call_args[0][0].bucket_name == "bucket_name"
5959
assert mock_create.call_args[0][0].key == "key_name"
6060

6161

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ deps =
1818
pip-licenses
1919
commands =
2020
flake8 nitric
21-
black --check nitric tests tools
21+
black nitric tests tools
2222
pydocstyle nitric
2323
pip-licenses --allow-only="MIT License;BSD License;Zope Public License;Python Software Foundation License;Apache License 2.0;Apache Software License;MIT License, Mozilla Public License 2.0 (MPL 2.0);MIT;BSD License, Apache Software License;3-Clause BSD License;Historical Permission Notice and Disclaimer (HPND);Mozilla Public License 2.0 (MPL 2.0);Apache Software License, BSD License;BSD;Python Software Foundation License, MIT License;Public Domain;Public Domain, Python Software Foundation License, BSD License, GNU General Public License (GPL);GNU Library or Lesser General Public License (LGPL);LGPL;Apache Software License, MIT License" --ignore-packages nitric
2424

0 commit comments

Comments
 (0)