2
2
All pydantic BaseModel class representations of Sandbox API responses
3
3
"""
4
4
from __future__ import annotations
5
+
6
+ import json
5
7
from enum import Enum
6
8
from typing import TYPE_CHECKING , List , Optional
7
9
8
10
from pydantic import BaseModel , Field
9
11
12
+ RESPONSE_DICT_KEY = "response_dict"
13
+
14
+
10
15
# added for dev intellisense: https://stackoverflow.com/a/71257588
11
16
if TYPE_CHECKING :
12
17
from dataclasses import dataclass as _basemodel_decorator
13
18
else :
19
+
14
20
def _basemodel_decorator (func ):
15
21
return func
16
22
17
23
18
24
class SandboxApiBaseModel (BaseModel ):
25
+ """ Base Model for all other classes. Defines useful helper methods """
26
+
19
27
class Config :
20
28
use_enum_values = True
21
29
22
30
response_dict : dict = None
23
- """ the original dictionary received from api response """
31
+ """ this attribute will cache the original response before loading into model """
32
+
33
+ @classmethod
34
+ def dict_to_model (cls , response_dict : dict ) -> SandboxApiBaseModel :
35
+ """ calls parse_obj, but additionally caches the response dict """
36
+ wrapped_item = cls .parse_obj (response_dict )
37
+ wrapped_item .response_dict = response_dict
38
+ return wrapped_item
39
+
40
+ @classmethod
41
+ def list_to_models (cls , response_list : List [dict ]) -> List [SandboxApiBaseModel ]:
42
+ return [cls .dict_to_model (dict_item ) for dict_item in response_list ]
24
43
25
44
def pretty_json (self , indent = 4 , exclude_response = True ):
26
45
excluded_key_set = set ()
27
46
if exclude_response :
28
- excluded_key_set .add ("response_dict" )
47
+ excluded_key_set .add (RESPONSE_DICT_KEY )
29
48
return self .json (indent = indent , exclude = excluded_key_set )
30
49
31
50
32
- def list_to_model (response_list : List [dict ], model : SandboxApiBaseModel ) -> List [SandboxApiBaseModel ]:
33
- return [dict_to_model (dict_item , model ) for dict_item in response_list ]
34
-
35
-
36
- def dict_to_model (response_dict : dict , model : SandboxApiBaseModel ) -> SandboxApiBaseModel :
37
- """ calls parse_obj, but additionally caches the response dict """
38
- wrapped_item = model .parse_obj (response_dict )
39
- wrapped_item .response_dict = response_dict
40
- return wrapped_item
51
+ def models_to_json (model_list : List [SandboxApiBaseModel ]) -> str :
52
+ """ helper method to be used to convert a list of models to a list of dicts and dump to json """
53
+ list_of_dicts = [x .dict () for x in model_list ]
54
+ excluded_keys = [RESPONSE_DICT_KEY ]
55
+ updated_list = [{k : v for k , v in curr_dict if k not in excluded_keys } for curr_dict in list_of_dicts ]
56
+ return json .dumps (updated_list , indent = 4 )
41
57
42
58
43
59
class BlueprintAvailabilityStates (str , Enum ):
@@ -73,6 +89,7 @@ class SetupStages(str, Enum):
73
89
PROVISIONING = "Provisioning"
74
90
CONNECTIVITY = "Connectivity"
75
91
CONFIGURATION = "Configuration"
92
+ ENDED = "Ended"
76
93
77
94
78
95
class SandboxEventTypes (str , Enum ):
@@ -94,6 +111,10 @@ class CommandExecutionStates(str, Enum):
94
111
COMPLETE = "Complete"
95
112
FAILED = "Failed"
96
113
114
+ @classmethod
115
+ def get_incomplete_execution_states (cls ) -> List :
116
+ return [cls .PENDING , cls .RUNNING ]
117
+
97
118
98
119
@_basemodel_decorator
99
120
class BlueprintInput (SandboxApiBaseModel ):
@@ -142,6 +163,9 @@ class SandboxDetails(SandboxApiBaseModel):
142
163
state : Optional [SandboxStates ]
143
164
setup_stage : Optional [SetupStages ]
144
165
166
+ def components_to_json (self ):
167
+ return models_to_json (self .components )
168
+
145
169
146
170
@_basemodel_decorator
147
171
class BlueprintReference (SandboxApiBaseModel ):
@@ -175,6 +199,9 @@ class ActivityEventsResponse(SandboxApiBaseModel):
175
199
next_event_id : Optional [int ]
176
200
events : Optional [List [SandboxEvent ]]
177
201
202
+ def events_to_json (self ) -> str :
203
+ return models_to_json (self .events )
204
+
178
205
179
206
@_basemodel_decorator
180
207
class CommandParameterDetails (SandboxApiBaseModel ):
@@ -230,8 +257,9 @@ class CommandExecutionDetails(SandboxApiBaseModel):
230
257
started : Optional [str ]
231
258
ended : Optional [str ]
232
259
output : Optional [str ]
233
- command_context : Optional [CommandContextDetails ] = Field (description = "additional data to populate about command. "
234
- "NOTE: this does not come from api response" )
260
+ command_context : Optional [CommandContextDetails ] = Field (
261
+ description = "additional data to populate about command. " "NOTE: this does not come from api response"
262
+ )
235
263
236
264
237
265
@_basemodel_decorator
0 commit comments