1- from .metrics_shared import OpenPAYGOMetricsShared
21import copy
32
3+ from .metrics_shared import OpenPAYGOMetricsShared
4+
45
56class MetricsRequestHandler (object ):
6-
7- def __init__ (self , serial_number , data_format = None , secret_key = None , auth_method = None ):
7+ def __init__ (
8+ self , serial_number , data_format = None , secret_key = None , auth_method = None
9+ ):
810 self .secret_key = secret_key
911 self .auth_method = auth_method
1012 self .request_dict = {
11- ' serial_number' : serial_number ,
13+ " serial_number" : serial_number ,
1214 }
1315 self .data_format = data_format
1416 if self .data_format :
15- if self .data_format .get ('id' ):
16- self .request_dict [' data_format_id' ] = data_format .get ('id' )
17+ if self .data_format .get ("id" ):
18+ self .request_dict [" data_format_id" ] = data_format .get ("id" )
1719 else :
18- self .request_dict [' data_format' ] = data_format
20+ self .request_dict [" data_format" ] = data_format
1921 self .data = {}
2022 self .historical_data = {}
2123
2224 def set_request_count (self , request_count ):
23- self .request_dict [' request_count' ] = request_count
25+ self .request_dict [" request_count" ] = request_count
2426
2527 def set_timestamp (self , timestamp ):
26- self .request_dict [' timestamp' ] = timestamp
28+ self .request_dict [" timestamp" ] = timestamp
2729
2830 def set_data (self , data ):
2931 self .data = data
3032
3133 def set_historical_data (self , historical_data ):
32- if not self .data_format .get (' historical_data_interval' ):
34+ if not self .data_format .get (" historical_data_interval" ):
3335 for time_step in historical_data :
34- if not time_step .get ('timestamp' ):
35- raise ValueError ('Historical Data objects must have a time stamp if no historical_data_interval is defined.' )
36+ if not time_step .get ("timestamp" ):
37+ raise ValueError (
38+ "Historical Data objects must have a time stamp if no "
39+ "historical_data_interval is defined."
40+ )
3641 self .historical_data = historical_data
3742
3843 def get_simple_request_payload (self ):
@@ -41,11 +46,15 @@ def get_simple_request_payload(self):
4146
4247 def get_simple_request_dict (self ):
4348 simple_request = self .request_dict
44- simple_request [' data' ] = self .data
45- simple_request [' historical_data' ] = self .historical_data
49+ simple_request [" data" ] = self .data
50+ simple_request [" historical_data" ] = self .historical_data
4651 # We prepare the auth
4752 if self .auth_method :
48- simple_request ['auth' ] = OpenPAYGOMetricsShared .generate_request_signature_from_data (simple_request , self .auth_method , self .secret_key )
53+ simple_request [
54+ "auth"
55+ ] = OpenPAYGOMetricsShared .generate_request_signature_from_data (
56+ simple_request , self .auth_method , self .secret_key
57+ )
4958 return simple_request
5059
5160 def get_condensed_request_payload (self ):
@@ -54,36 +63,55 @@ def get_condensed_request_payload(self):
5463
5564 def get_condensed_request_dict (self ):
5665 if not self .data_format :
57- raise ValueError (' No Data Format provided for condensed request' )
58- data_order = self .data_format .get (' data_order' )
66+ raise ValueError (" No Data Format provided for condensed request" )
67+ data_order = self .data_format .get (" data_order" )
5968 if self .data and not data_order :
60- raise ValueError (' Data Format does not contain data_order' )
61- historical_data_order = self .data_format .get (' historical_data_order' )
69+ raise ValueError (" Data Format does not contain data_order" )
70+ historical_data_order = self .data_format .get (" historical_data_order" )
6271 if self .historical_data and not historical_data_order :
63- raise ValueError (' Data Format does not contain historical_data_order' )
72+ raise ValueError (" Data Format does not contain historical_data_order" )
6473 condensed_request = copy .deepcopy (self .request_dict )
65- condensed_request [' data' ] = []
66- condensed_request [' historical_data' ] = []
74+ condensed_request [" data" ] = []
75+ condensed_request [" historical_data" ] = []
6776 # We add the data
6877 data_copy = copy .deepcopy (self .data )
6978 for var in data_order :
70- condensed_request ['data' ].append (data_copy .pop (var ) if var in data_copy else None )
79+ condensed_request ["data" ].append (
80+ data_copy .pop (var ) if var in data_copy else None
81+ )
7182 if len (data_copy ) > 0 :
72- raise ValueError ('Additional variables not present in the data format: ' + str (data_copy ))
73- condensed_request ['data' ] = OpenPAYGOMetricsShared .remove_trailing_empty_elements (condensed_request ['data' ])
83+ raise ValueError (
84+ "Additional variables not present in the data format: " + str (data_copy )
85+ )
86+ condensed_request [
87+ "data"
88+ ] = OpenPAYGOMetricsShared .remove_trailing_empty_elements (
89+ condensed_request ["data" ]
90+ )
7491 # We add the historical data
7592 historical_data_copy = copy .deepcopy (self .historical_data )
7693 for time_step in historical_data_copy :
7794 time_step_data = []
7895 for var in historical_data_order :
7996 time_step_data .append (time_step .pop (var ) if var in time_step else None )
8097 if len (time_step ) > 0 :
81- raise ValueError ('Additional variables not present in the historical data format: ' + str (time_step ))
82- time_step_data = OpenPAYGOMetricsShared .remove_trailing_empty_elements (time_step_data )
83- condensed_request ['historical_data' ].append (time_step_data )
98+ raise ValueError (
99+ "Additional variables not present in the historical data format: "
100+ + str (time_step )
101+ )
102+ time_step_data = OpenPAYGOMetricsShared .remove_trailing_empty_elements (
103+ time_step_data
104+ )
105+ condensed_request ["historical_data" ].append (time_step_data )
84106 # We prepare the auth
85107 if self .auth_method :
86- condensed_request ['auth' ] = OpenPAYGOMetricsShared .generate_request_signature_from_data (condensed_request , self .auth_method , self .secret_key )
108+ condensed_request [
109+ "auth"
110+ ] = OpenPAYGOMetricsShared .generate_request_signature_from_data (
111+ condensed_request , self .auth_method , self .secret_key
112+ )
87113 # We replace the key names by the condensed ones
88- condensed_request = OpenPAYGOMetricsShared .convert_dict_keys_to_condensed (condensed_request )
114+ condensed_request = OpenPAYGOMetricsShared .convert_dict_keys_to_condensed (
115+ condensed_request
116+ )
89117 return condensed_request
0 commit comments