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 12f60ee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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
client_accepted_types_hash: Optional[bytes]
messages: List[Dict[str, Any]]
next_exchange_token: Optional[bytes]
next_expected_sequence: Optional[int]
server_api: str
server_uuid: bytes


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.get("client-accepted-types-hash"),
response["messages"],
response.get("next-exchange-token"),
response.get("next-expected-sequence"),
response["server-api"],
response["server-uuid"],
)
1 change: 1 addition & 0 deletions landscape/lib/fetch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import os
import sys
import logging
from argparse import ArgumentParser

from twisted.internet.defer import DeferredList
Expand Down

0 comments on commit 12f60ee

Please sign in to comment.