11from typing import Any , Optional , Type , TypeVar
22
33import marshmallow_dataclass
4- from marshmallow import post_dump , Schema
4+ from marshmallow import post_dump , post_load , Schema
55from marshmallow .fields import Field
66
77import hostingde
@@ -26,6 +26,11 @@ def on_bind_field(self, field_name: str, field_obj: Field) -> None:
2626 """Use lower-camel-casing as external representation"""
2727 field_obj .data_key = camelcase (field_obj .data_key or field_name )
2828
29+ @post_load
30+ def post_load (self , data : Any , ** kwargs : Any ) -> Any :
31+ data ["context" ] = self .context .get ('client' , None )
32+ return data
33+
2934 @post_dump
3035 def remove_skip_values (self , data : dict , ** kwargs : dict ) -> dict :
3136 """
@@ -69,18 +74,6 @@ def __init__(self, **kwargs: Any):
6974 if "context" in kwargs and isinstance (kwargs ["context" ], hostingde .HostingDeClient ):
7075 self .client = kwargs ["context" ]
7176
72- def get_client (self ) -> 'hostingde.HostingDeClient' :
73- """
74- Get the client used to create this object.
75-
76- Throws an attribute error if no client is set.
77- :return: an initialized client
78- """
79- if not self .client :
80- raise AttributeError ("Context not set" )
81-
82- return self .client
83-
8477 @classmethod
8578 def from_json (cls : Type [T ], data : dict , client : 'hostingde.HostingDeClient' = None ) -> T :
8679 """
@@ -89,7 +82,6 @@ def from_json(cls: Type[T], data: dict, client: 'hostingde.HostingDeClient' = No
8982 :param client: The client to use for this object
9083 :return: New instance of the class
9184 """
92- inst = marshmallow_dataclass .class_schema (cls , base_schema = CamelCaseSchema )().load (data )
93- inst .client = client
94-
95- return inst
85+ return marshmallow_dataclass .class_schema (cls , base_schema = CamelCaseSchema )(context = {'client' : client }).load (
86+ data
87+ )
0 commit comments