Skip to content

Commit

Permalink
added utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Jan 15, 2024
1 parent c1e9f65 commit 281507f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Johannes Roos <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
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 = "rath"
version = "0.5.0a0"
version = "0.5.0"
description = "async transport-agnostic graphql client"
authors = ["jhnnsrs <[email protected]>"]
readme = "README.md"
Expand Down
71 changes: 71 additions & 0 deletions rath/turms/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import Any, List


class NotQueriedError(Exception):
pass


def get_nested_error(obj: Any, nested: List[str], above: List[str]) -> Any:
"""Get a nested attribute or raise an error
Raises an error if a nested attribut is not present
This is used to raise an error if a nested attribute is not present.
And to give a hint where in a query tree a the nested attribute is missing.
Parameters
----------
obj : Any
The object to query
nested : List[str]
The nested attributes to query
above : _type_
The attributes above the nested attribute ( will be filled by the function)
Returns
-------
Any
The queried object
Raises
------
NotQueriedError
The nested attribute was not queried
"""
if hasattr(obj, nested[0]):
obj = getattr(obj, nested[0])
if len(nested) > 1:
return get_nested_error(obj, nested[1:], above + [nested[0]])
else:
return obj
else:
raise NotQueriedError(f"{nested} not queried. {above} was queried")


def get_attributes_or_error(self, *args) -> Any:
"""Get attributes or raise an error"""
returns = []
errors = []
for i in args:
if "." in i:
try:
returns.append(get_nested_error(self, i.split("."), []))
continue
except NotQueriedError:
errors.append(i)
else:
if hasattr(self, i):
returns.append(getattr(self, i))
else:
errors.append(i)

if len(errors) > 0:
raise NotQueriedError(
f"Required fields {errors} not queried on {self.__class__.__name__}"
)

if len(args) == 1:
return returns[0]
else:
return tuple(returns)

0 comments on commit 281507f

Please sign in to comment.