Skip to content

Commit 18c3eac

Browse files
committed
fix: resolve exception when action ID is passed to printer formatter
1 parent 3f3bfe6 commit 18c3eac

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/binarylane/console/printers/formatter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def object_to_list(row: Dict[str, Any], columns: List[str]) -> List[Any]:
3333
if isinstance(response, str):
3434
data = [[DEFAULT_HEADING]] if show_header else []
3535
data += [[response]]
36-
36+
elif isinstance(response, int):
37+
data = [[DEFAULT_HEADING]] if show_header else []
38+
data += [[str(response)]]
3739
else:
3840
data = [["name", "value"]] if show_header else []
3941
data += [_flatten(item, True) for item in response.to_dict().items()]

src/binarylane/console/printers/json_printer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ class JsonPrinter(Printer):
1010
"""Output an API response as 'raw' JSON"""
1111

1212
def print(self, response: Any, fields: Optional[List[str]] = None) -> None:
13-
print(json.dumps(response.to_dict()))
13+
print(self.format_response(response))
14+
15+
def format_response(self, response: Any) -> str:
16+
return json.dumps(response.to_dict() if hasattr(response, "to_dict") else response)

tests/printers/test_formatter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@ def test_format_networks_v4_and_v6(servers_response: ServersResponse) -> None:
9393
Network(ip_address="value4", type=NetworkType.PUBLIC),
9494
]
9595
assert formatter.format_response(servers_response, False, ["networks"]) == [["ipv4\nipv6"]]
96+
97+
98+
# ActionLinkRunner when used with --async will print the action ID
99+
def test_format_int() -> None:
100+
assert formatter.format_response(12345, True) == [[formatter.DEFAULT_HEADING], ["12345"]]
101+
assert formatter.format_response(12345, False) == [["12345"]]

tests/printers/test_json.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import annotations
2+
3+
from typing import Any, Dict, List
4+
5+
from binarylane.console.printers.json_printer import JsonPrinter
6+
7+
printer = JsonPrinter()
8+
9+
10+
def test_format_str() -> None:
11+
assert printer.format_response("test") == '"test"'
12+
13+
14+
def test_format_list() -> None:
15+
ns1 = "ns1.binarylane.com.au"
16+
ns2 = "ns2.binarylane.com.au"
17+
dns = [ns1, ns2]
18+
19+
assert printer.format_response(dns) == '["ns1.binarylane.com.au", "ns2.binarylane.com.au"]'
20+
21+
22+
def test_format_dict() -> None:
23+
class DnsList:
24+
dns: List[str]
25+
meta: Dict[str, Any]
26+
links: List[str]
27+
28+
def __init__(self) -> None:
29+
self.dns = ["ns1.binarylane.com.au", "ns2.binarylane.com.au"]
30+
31+
def to_dict(self) -> Dict[str, Any]:
32+
return {"dns": self.dns}
33+
34+
response = DnsList()
35+
assert printer.format_response(response) == '{"dns": ["ns1.binarylane.com.au", "ns2.binarylane.com.au"]}'
36+
37+
38+
# ActionLinkRunner when used with --async will print the action ID
39+
def test_format_int() -> None:
40+
assert printer.format_response(12345) == "12345"

0 commit comments

Comments
 (0)