22
33"""Module for utilities used in GoodData Pipelines provisioning."""
44
5+ from typing import Any , cast
6+
57import attrs
68from requests import Response
79
@@ -11,9 +13,8 @@ class AttributesMixin:
1113 Mixin class to provide a method for getting attributes of an object which may or may not exist.
1214 """
1315
14- @staticmethod
1516 def get_attrs (
16- * objects : object , overrides : dict [str , str ] | None = None
17+ self , * objects : object , overrides : dict [str , str ] | None = None
1718 ) -> dict [str , str ]:
1819 """
1920 Returns a dictionary of attributes from the given objects.
@@ -27,11 +28,11 @@ def get_attrs(
2728 """
2829 # TODO: This might not work great with nested objects, values which are lists of objects etc.
2930 # If we care about parsing the logs back from the string, we should consider some other approach
30- attrs : dict [str , str ] = {}
31+ attributes : dict [str , str ] = {}
3132 for context_object in objects :
3233 if isinstance (context_object , Response ):
3334 # for request.Response objects, keys need to be renamed to match the log schema
34- attrs .update (
35+ attributes .update (
3536 {
3637 "http_status" : str (context_object .status_code ),
3738 "http_method" : getattr (
@@ -42,23 +43,31 @@ def get_attrs(
4243 ),
4344 }
4445 )
46+ elif attrs .has (type (context_object )):
47+ for key , value in attrs .asdict (
48+ cast (attrs .AttrsInstance , context_object )
49+ ).items ():
50+ self ._add_to_dict (attributes , key , value )
4551 else :
4652 # Generic handling for other objects
4753 for key , value in context_object .__dict__ .items ():
48- if value is None :
49- continue
50-
51- if isinstance (value , list ):
52- attrs [key ] = ", " .join (
53- str (list_item ) for list_item in value
54- )
55- else :
56- attrs [key ] = str (value )
54+ self ._add_to_dict (attributes , key , value )
5755
5856 if overrides :
59- attrs .update (overrides )
57+ attributes .update (overrides )
58+
59+ return attributes
60+
61+ def _add_to_dict (
62+ self , attributes : dict [str , str ], key : str , value : Any
63+ ) -> None :
64+ if value is None :
65+ return
6066
61- return attrs
67+ if isinstance (value , list ):
68+ attributes [key ] = ", " .join (str (list_item ) for list_item in value )
69+ else :
70+ attributes [key ] = str (value )
6271
6372
6473@attrs .define
0 commit comments