Skip to content

Commit

Permalink
fix: process full server response
Browse files Browse the repository at this point in the history
  • Loading branch information
st3v3nmw committed Oct 7, 2024
1 parent 4f2f0db commit 6871045
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion landscape/client/broker/tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def request_with_payload(self, payload):
transport.exchange,
payload,
computer_id="34",
exchange_token="abcd-efgh",
exchange_token=b"abcd-efgh",
message_api=b"X.Y",
)

Expand Down
13 changes: 11 additions & 2 deletions landscape/client/broker/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def exchange(
self,
payload: dict,
computer_id: Optional[str] = None,
exchange_token: Optional[str] = None,
exchange_token: Optional[bytes] = None,
message_api: bytes = SERVER_API,
) -> Union[dict, None]:
"""Exchange message data with the server.
Expand All @@ -59,7 +59,16 @@ def exchange(
except Exception:
return None

return asdict(response)
# Return `ServerResponse` as a dictionary
# converting the field names back to kebab case
# which (imo) is better than mixing snake_case & kebab-case
# in landscape.client.broker.exchange.MessageExchange.
return asdict(
response,
dict_factory=lambda data: {
k.replace("_", "-"): v for k, v in data
},
)


class FakeTransport:
Expand Down
19 changes: 15 additions & 4 deletions landscape/client/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
class ServerResponse:
"""The HTTP response from the server after a message exchange."""

server_uuid: str
server_api: str
server_uuid: bytes
messages: List[Dict[str, Any]]
client_accepted_types_hash: Optional[bytes] = None
next_exchange_token: Optional[bytes] = None
next_expected_sequence: Optional[int] = None


def exchange_messages(
Expand All @@ -33,7 +37,7 @@ def exchange_messages(
*,
cainfo: Optional[str] = None,
computer_id: Optional[str] = None,
exchange_token: Optional[str] = None,
exchange_token: Optional[bytes] = None,
server_api: str = SERVER_API.decode(),
) -> ServerResponse:
"""Sends `payload` via HTTP(S) to `server_url`, parsing and returning the
Expand Down Expand Up @@ -61,7 +65,7 @@ def exchange_messages(
headers["X-Computer-ID"] = computer_id

if exchange_token:
headers["X-Exchange-Token"] = exchange_token
headers["X-Exchange-Token"] = exchange_token.decode()

curl = pycurl.Curl()

Expand Down Expand Up @@ -91,4 +95,11 @@ def exchange_messages(

logging.debug(f"Received payload:\n{pformat(response)}")

return ServerResponse(response["server-uuid"], response["messages"])
return ServerResponse(
response["server-api"],
response["server-uuid"],
response["messages"],
response.get("client-accepted-types-hash"),
response.get("next-exchange-token"),
response.get("next-expected-sequence"),
)
3 changes: 2 additions & 1 deletion landscape/client/tests/test_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_success(self):
payload = {"messages": [{"type": "my-message-type", "some-value": 5}]}

mock_response = {
"server-api": b"3.2",
"server-uuid": "my-server-uuid",
"messages": [{"type": "my-server-message-type", "other-value": 6}]
}
Expand All @@ -40,7 +41,7 @@ def test_success(self):
"https://my-server.local/message-system",
cainfo="mycainfo",
computer_id="my-secure-id",
exchange_token="my-exchange-token",
exchange_token=b"my-exchange-token",
)

self.assertEqual(server_response.server_uuid, "my-server-uuid")
Expand Down
3 changes: 3 additions & 0 deletions landscape/client/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_success(self):
)

self.exchange_messages_mock.return_value = ServerResponse(
"3.2",
"thisisaserveruuid",
[{"type": "set-id", "id": "mysecureid", "insecure-id": 1}],
)
Expand Down Expand Up @@ -129,6 +130,7 @@ def test_no_messages(self):
)

self.exchange_messages_mock.return_value = ServerResponse(
"3.2",
server_uuid="thisisaserveruuid",
messages=[],
)
Expand All @@ -149,6 +151,7 @@ def test_no_set_id_message(self):
)

self.exchange_messages_mock.return_value = ServerResponse(
"3.2",
server_uuid="thisisaserveruuid",
messages=[{"type": "unknown-message-type"}],
)
Expand Down

0 comments on commit 6871045

Please sign in to comment.