33import datetime
44from dataclasses import dataclass
55from enum import Enum , unique
6+ from typing import Any , Self
67
78from beartype import BeartypeConf , beartype
89
@@ -29,6 +30,24 @@ class DatabaseSummaryReport:
2930 target_quota : int
3031 total_recos : int
3132
33+ @classmethod
34+ def from_response_dict (cls , response_dict : dict [str , Any ]) -> Self :
35+ """Construct from a VWS API response dict."""
36+ return cls (
37+ active_images = int (response_dict ["active_images" ]),
38+ current_month_recos = int (response_dict ["current_month_recos" ]),
39+ failed_images = int (response_dict ["failed_images" ]),
40+ inactive_images = int (response_dict ["inactive_images" ]),
41+ name = response_dict ["name" ],
42+ previous_month_recos = int (response_dict ["previous_month_recos" ]),
43+ processing_images = int (response_dict ["processing_images" ]),
44+ reco_threshold = int (response_dict ["reco_threshold" ]),
45+ request_quota = int (response_dict ["request_quota" ]),
46+ request_usage = int (response_dict ["request_usage" ]),
47+ target_quota = int (response_dict ["target_quota" ]),
48+ total_recos = int (response_dict ["total_recos" ]),
49+ )
50+
3251
3352@beartype
3453@unique
@@ -63,6 +82,23 @@ class TargetSummaryReport:
6382 current_month_recos : int
6483 previous_month_recos : int
6584
85+ @classmethod
86+ def from_response_dict (cls , response_dict : dict [str , Any ]) -> Self :
87+ """Construct from a VWS API response dict."""
88+ return cls (
89+ status = TargetStatuses (value = response_dict ["status" ]),
90+ database_name = response_dict ["database_name" ],
91+ target_name = response_dict ["target_name" ],
92+ upload_date = datetime .date .fromisoformat (
93+ response_dict ["upload_date" ]
94+ ),
95+ active_flag = bool (response_dict ["active_flag" ]),
96+ tracking_rating = int (response_dict ["tracking_rating" ]),
97+ total_recos = int (response_dict ["total_recos" ]),
98+ current_month_recos = int (response_dict ["current_month_recos" ]),
99+ previous_month_recos = int (response_dict ["previous_month_recos" ]),
100+ )
101+
66102
67103@beartype (conf = BeartypeConf (is_pep484_tower = True ))
68104@dataclass (frozen = True )
@@ -103,6 +139,26 @@ class QueryResult:
103139 target_id : str
104140 target_data : TargetData | None
105141
142+ @classmethod
143+ def from_response_dict (cls , response_dict : dict [str , Any ]) -> Self :
144+ """Construct from a VWS API query result item dict."""
145+ target_data : TargetData | None = None
146+ if "target_data" in response_dict :
147+ target_data_dict = response_dict ["target_data" ]
148+ target_timestamp = datetime .datetime .fromtimestamp (
149+ timestamp = target_data_dict ["target_timestamp" ],
150+ tz = datetime .UTC ,
151+ )
152+ target_data = TargetData (
153+ name = target_data_dict ["name" ],
154+ application_metadata = target_data_dict ["application_metadata" ],
155+ target_timestamp = target_timestamp ,
156+ )
157+ return cls (
158+ target_id = response_dict ["target_id" ],
159+ target_data = target_data ,
160+ )
161+
106162
107163@beartype
108164@dataclass (frozen = True )
@@ -115,3 +171,18 @@ class TargetStatusAndRecord:
115171
116172 status : TargetStatuses
117173 target_record : TargetRecord
174+
175+ @classmethod
176+ def from_response_dict (cls , response_dict : dict [str , Any ]) -> Self :
177+ """Construct from a VWS API response dict."""
178+ status = TargetStatuses (value = response_dict ["status" ])
179+ target_record_dict = dict (response_dict ["target_record" ])
180+ target_record = TargetRecord (
181+ target_id = target_record_dict ["target_id" ],
182+ active_flag = bool (target_record_dict ["active_flag" ]),
183+ name = target_record_dict ["name" ],
184+ width = float (target_record_dict ["width" ]),
185+ tracking_rating = int (target_record_dict ["tracking_rating" ]),
186+ reco_rating = target_record_dict ["reco_rating" ],
187+ )
188+ return cls (status = status , target_record = target_record )
0 commit comments