Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions openpaygo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .token_encode import OpenPAYGOTokenEncoder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing those imports? They are left at top level so that you can access them importing just the module, if we remove them it will break existing tools using it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It raised "unused variable" warnings. I think the recommended approach is to use __all__. Let me try to adapt. Do you have a piece of code, where I can test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing public but basically you should be able to import all of those variables when importing openpaygo. Like from openpaygo import TokenType should work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally find it a bit odd, if from openpaygo import TokenType works, but from openpaygo import * and then openpaygo.TokenType doesn't. Using __all__ should be non-breaking. WDYT?

from .token_decode import OpenPAYGOTokenDecoder
from .token_shared import TokenType
from .metrics_request import MetricsRequestHandler
from .metrics_response import MetricsResponseHandler
from .metrics_shared import AuthMethod
from .token_decode import OpenPAYGOTokenDecoder
from .token_encode import OpenPAYGOTokenEncoder
from .token_shared import TokenType


def generate_token(**kwargs):
Expand All @@ -12,3 +12,13 @@ def generate_token(**kwargs):

def decode_token(**kwargs):
return OpenPAYGOTokenDecoder.decode_token(**kwargs)


__all__ = [
MetricsRequestHandler,
MetricsResponseHandler,
AuthMethod,
OpenPAYGOTokenDecoder,
OpenPAYGOTokenEncoder,
TokenType,
]
88 changes: 58 additions & 30 deletions openpaygo/metrics_request.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
from .metrics_shared import OpenPAYGOMetricsShared
import copy

from .metrics_shared import OpenPAYGOMetricsShared


class MetricsRequestHandler(object):

def __init__(self, serial_number, data_format=None, secret_key=None, auth_method=None):
def __init__(
self, serial_number, data_format=None, secret_key=None, auth_method=None
):
self.secret_key = secret_key
self.auth_method = auth_method
self.request_dict = {
'serial_number': serial_number,
"serial_number": serial_number,
}
self.data_format = data_format
if self.data_format:
if self.data_format.get('id'):
self.request_dict['data_format_id'] = data_format.get('id')
if self.data_format.get("id"):
self.request_dict["data_format_id"] = data_format.get("id")
else:
self.request_dict['data_format'] = data_format
self.request_dict["data_format"] = data_format
self.data = {}
self.historical_data = {}

def set_request_count(self, request_count):
self.request_dict['request_count'] = request_count
self.request_dict["request_count"] = request_count

def set_timestamp(self, timestamp):
self.request_dict['timestamp'] = timestamp
self.request_dict["timestamp"] = timestamp

def set_data(self, data):
self.data = data

def set_historical_data(self, historical_data):
if not self.data_format.get('historical_data_interval'):
if not self.data_format.get("historical_data_interval"):
for time_step in historical_data:
if not time_step.get('timestamp'):
raise ValueError('Historical Data objects must have a time stamp if no historical_data_interval is defined.')
if not time_step.get("timestamp"):
raise ValueError(
"Historical Data objects must have a time stamp if no "
"historical_data_interval is defined."
)
self.historical_data = historical_data

def get_simple_request_payload(self):
Expand All @@ -41,11 +46,15 @@ def get_simple_request_payload(self):

def get_simple_request_dict(self):
simple_request = self.request_dict
simple_request['data'] = self.data
simple_request['historical_data'] = self.historical_data
simple_request["data"] = self.data
simple_request["historical_data"] = self.historical_data
# We prepare the auth
if self.auth_method:
simple_request['auth'] = OpenPAYGOMetricsShared.generate_request_signature_from_data(simple_request, self.auth_method, self.secret_key)
simple_request[
"auth"
] = OpenPAYGOMetricsShared.generate_request_signature_from_data(
simple_request, self.auth_method, self.secret_key
)
return simple_request

def get_condensed_request_payload(self):
Expand All @@ -54,36 +63,55 @@ def get_condensed_request_payload(self):

def get_condensed_request_dict(self):
if not self.data_format:
raise ValueError('No Data Format provided for condensed request')
data_order = self.data_format.get('data_order')
raise ValueError("No Data Format provided for condensed request")
data_order = self.data_format.get("data_order")
if self.data and not data_order:
raise ValueError('Data Format does not contain data_order')
historical_data_order = self.data_format.get('historical_data_order')
raise ValueError("Data Format does not contain data_order")
historical_data_order = self.data_format.get("historical_data_order")
if self.historical_data and not historical_data_order:
raise ValueError('Data Format does not contain historical_data_order')
raise ValueError("Data Format does not contain historical_data_order")
condensed_request = copy.deepcopy(self.request_dict)
condensed_request['data'] = []
condensed_request['historical_data'] = []
condensed_request["data"] = []
condensed_request["historical_data"] = []
# We add the data
data_copy = copy.deepcopy(self.data)
for var in data_order:
condensed_request['data'].append(data_copy.pop(var) if var in data_copy else None)
condensed_request["data"].append(
data_copy.pop(var) if var in data_copy else None
)
if len(data_copy) > 0:
raise ValueError('Additional variables not present in the data format: '+str(data_copy))
condensed_request['data'] = OpenPAYGOMetricsShared.remove_trailing_empty_elements(condensed_request['data'])
raise ValueError(
"Additional variables not present in the data format: " + str(data_copy)
)
condensed_request[
"data"
] = OpenPAYGOMetricsShared.remove_trailing_empty_elements(
condensed_request["data"]
)
# We add the historical data
historical_data_copy = copy.deepcopy(self.historical_data)
for time_step in historical_data_copy:
time_step_data = []
for var in historical_data_order:
time_step_data.append(time_step.pop(var) if var in time_step else None)
if len(time_step) > 0:
raise ValueError('Additional variables not present in the historical data format: '+str(time_step))
time_step_data = OpenPAYGOMetricsShared.remove_trailing_empty_elements(time_step_data)
condensed_request['historical_data'].append(time_step_data)
raise ValueError(
"Additional variables not present in the historical data format: "
+ str(time_step)
)
time_step_data = OpenPAYGOMetricsShared.remove_trailing_empty_elements(
time_step_data
)
condensed_request["historical_data"].append(time_step_data)
# We prepare the auth
if self.auth_method:
condensed_request['auth'] = OpenPAYGOMetricsShared.generate_request_signature_from_data(condensed_request, self.auth_method, self.secret_key)
condensed_request[
"auth"
] = OpenPAYGOMetricsShared.generate_request_signature_from_data(
condensed_request, self.auth_method, self.secret_key
)
# We replace the key names by the condensed ones
condensed_request = OpenPAYGOMetricsShared.convert_dict_keys_to_condensed(condensed_request)
condensed_request = OpenPAYGOMetricsShared.convert_dict_keys_to_condensed(
condensed_request
)
return condensed_request
Loading