Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit ff139e5

Browse files
committed
Mark status code as optional to match latest Apilytics OpenAPI schema
1 parent f3ad204 commit ff139e5

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Change `status_code` into an optional parameter in `ApilyticsSender.set_response_info`.
13+
1014
## [1.1.0] - 2022-01-16
1115

1216
### Added

apilytics/core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@ def __exit__(
9797
)
9898
self._executor.submit(self._send_metrics)
9999

100-
def set_response_info(self, *, status_code: int) -> None:
100+
def set_response_info(self, *, status_code: Optional[int] = None) -> None:
101101
"""
102102
Update the context manager with info from the HTTP response object.
103103
104104
Should be called before the context manager's block ends.
105105
106106
Args:
107-
status_code: Status code for the HTTP response.
107+
status_code: Status code for the HTTP response. Can be omitted (or None)
108+
if the middleware could not get the status code.
108109
"""
109110
self._status_code = status_code
110111

@@ -120,10 +121,14 @@ def _send_metrics(self) -> None:
120121
)
121122
data = {
122123
"path": self._path,
123-
**({"query": self._query} if self._query else {}),
124124
"method": self._method,
125-
"statusCode": self._status_code,
126125
"timeMillis": (self._end_time_ns - self._start_time_ns) // 1_000_000,
126+
**({"query": self._query} if self._query else {}), # Don't send empty str.
127+
**(
128+
{"statusCode": self._status_code}
129+
if self._status_code is not None
130+
else {}
131+
),
127132
}
128133
try:
129134
urllib.request.urlopen(url=request, data=json.dumps(data).encode())

tests/fastapi/test_fastapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_middleware_should_send_data_even_on_errors(
8888

8989
__, call_kwargs = mocked_urlopen.call_args
9090
data = tests.conftest.decode_request_data(call_kwargs["data"])
91+
assert data.keys() == {"method", "path", "timeMillis"}
9192
assert data["method"] == "GET"
9293
assert data["path"] == "/error"
93-
assert data["statusCode"] is None # Was not able to call `set_response_info`.
9494
assert isinstance(data["timeMillis"], int)

0 commit comments

Comments
 (0)