Skip to content

Commit

Permalink
[fix] simplify json handling (#66)
Browse files Browse the repository at this point in the history
* [fix] simplify json handling

* shut up pyright
  • Loading branch information
aquamatthias authored Nov 17, 2023
1 parent b9ac039 commit 305a18d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "resotoclient"
version = "1.6.0"
version = "1.6.1"
description = "Resoto Python client library"
authors = ["Some Engineering Inc."]
license = "Apache-2.0"
Expand Down
8 changes: 3 additions & 5 deletions resotoclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
Optional,
List,
Tuple,
Sequence,
Mapping,
Type,
AsyncIterator,
TypeVar,
Awaitable,
Callable,
cast,
)
from types import TracebackType
from resotoclient.models import (
Expand Down Expand Up @@ -246,7 +244,7 @@ def get_node(self, node_id: str, graph: str = "resoto") -> JsObject:
def delete_node(self, node_id: str, graph: str = "resoto") -> None:
return self._await(lambda c: c.delete_node(node_id, graph))

def patch_nodes(self, nodes: Sequence[JsObject], graph: str = "resoto") -> List[JsObject]:
def patch_nodes(self, nodes: List[JsObject], graph: str = "resoto") -> List[JsObject]:
return self._await(lambda c: c.patch_nodes(nodes, graph))

def merge_graph(self, update: List[JsObject], graph: str = "resoto") -> GraphUpdate:
Expand Down Expand Up @@ -404,7 +402,7 @@ def dataframe(self, search: str, section: Optional[str] = "reported", graph: str

def extract_node(node: JsObject) -> Optional[JsObject]:
node_data = node
if not isinstance(node_data, dict):
if not node_data:
return None
if aggregate_search:
if flatten and "group" in node_data and isinstance(node_data["group"], dict):
Expand All @@ -416,7 +414,7 @@ def extract_node(node: JsObject) -> Optional[JsObject]:
if flatten:
if not "reported" in node or not isinstance(node["reported"], dict):
return None
node_data = cast(Dict[str, Any], node["reported"])
node_data = node["reported"]
for k, v in node.items():
if isinstance(v, dict) and k != "reported":
node_data[k] = v
Expand Down
3 changes: 1 addition & 2 deletions resotoclient/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Optional,
List,
Tuple,
Sequence,
Type,
)
from types import TracebackType
Expand Down Expand Up @@ -223,7 +222,7 @@ async def delete_node(self, node_id: str, graph: str = "resoto") -> None:
else:
raise AttributeError(await response.text())

async def patch_nodes(self, nodes: Sequence[JsObject], graph: str = "resoto") -> List[JsObject]:
async def patch_nodes(self, nodes: List[JsObject], graph: str = "resoto") -> List[JsObject]:
response = await self._patch(
f"/graph/{graph}/nodes",
json=nodes,
Expand Down
11 changes: 3 additions & 8 deletions resotoclient/json_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional, Type, TypeVar, Any
import jsons
import json
from typing import Optional, Type, TypeVar

import jsons

from resotoclient.models import JsValue

Expand Down Expand Up @@ -29,9 +30,3 @@ def json_dump(
return jsons.dump(obj, cls) # type: ignore


def __identity(obj: JsValue, *args: Any, **kwargs: Any) -> JsValue:
return obj


jsons.set_serializer(__identity, JsValue)
jsons.set_deserializer(__identity, JsValue)
6 changes: 3 additions & 3 deletions resotoclient/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from dataclasses import dataclass, field
from typing import List, Optional, Mapping, Sequence, Union, Dict
from datetime import timedelta
from enum import Enum
from typing import List, Optional, Mapping, Union, Dict, Any

JsValue = Union[None, int, str, float, bool, Sequence["JsValue"], Mapping[str, "JsValue"]]
JsObject = Mapping[str, JsValue]
JsValue = Union[None, int, str, float, bool, List[Any], Dict[str, Any]]
JsObject = Dict[str, JsValue]


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion tests/async_resotoclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def core_client() -> AsyncIterator[ResotoClient]:
async def test_listen_to_events(core_client: ResotoClient) -> None:
received: List[JsObject] = []
send_queue: Queue[JsObject] = Queue()
messages = [dict(kind="event", message_type="test", data={"foo": i}) for i in range(5)]
messages: List[JsObject] = [dict(kind="event", message_type="test", data={"foo": i}) for i in range(5)]
for msg in messages:
# add some messages that should be ignored - we are only listening for test events
await send_queue.put(dict(kind="event", message_type="ignore_me"))
Expand Down
14 changes: 13 additions & 1 deletion tests/model_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
from typing import Any

import jsons

from resotoclient.json_utils import json_dump, json_load
from resotoclient.models import Property, Kind
from resotoclient.models import Property, Kind, JsValue


def __identity(obj: JsValue, *args: Any, **kwargs: Any) -> JsValue:
return obj


jsons.set_serializer(__identity, JsValue) # type: ignore
jsons.set_deserializer(__identity, JsValue) # type: ignore


def test_prop_js_roundtrip() -> None:
Expand Down

0 comments on commit 305a18d

Please sign in to comment.