You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+74-2Lines changed: 74 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,10 +21,18 @@ This open-source project was sponsored by:
21
21
22
22
## Table of Contents
23
23
24
+
-[Key Features](#key-features)
24
25
-[Installing the library](#installing-the-library)
25
-
-[Getting Started - Generating Tokens](#getting-started---generating-tokens)
26
-
-[Getting Started - Decoding a Token](#getting-started---decoding-a-token)
26
+
-[Getting Started - OpenPAYGO Token](#getting-started---openpaygo-token)
27
+
-[Generating Tokens](#generating-tokens)
28
+
-[Decoding Tokens](#decoding-tokens)
29
+
-[Getting Started - OpenPAYGO Metrics](#getting-started---openpaygo-metrics)
30
+
-[Changelog](#changelog)
31
+
-[2023-10-03 - v0.2.0](#2023-10-03---v020)
27
32
33
+
## Key Features
34
+
- Implements token generation and decoding with full support for the v2.3 of the [OpenPAYGO Token](https://github.com/EnAccess/OpenPAYGO-Token) specifications.
35
+
- Implements payload authentication (verification + signing) and conversion from simple to condensed payload (and back) with full support of the v1.0-rc1 of the [OpenPAYGO Metrics](https://github.com/openpaygo/metrics) specifications.
You can use the `MetricsHandler` object to process your OpenPAYGO Metrics request from start to finish. It accepts the following initial inputs:
164
+
-`metrics_payload` (required): The OpenPAYGO Metrics payload, in a JSON string format.
165
+
-`secret_key` (optional): The secret key provided as a string containing 32 hexadecimal characters
166
+
-`data_format` (optional): The data format, provided as dictionnary matching the data format object specifications.
167
+
168
+
It provides the following methods:
169
+
-`get_device_serial()`: Returns the serial number of the device as a string.
170
+
-`set_device_parameters(secret_key, data_format)`: Used to set the device data required for proper processing of the request in the handler if it was not set initially, which is often the case as the serial number is usually required to fetch that data. It will return `ValueError` if either of the parameters is invalid.
171
+
-`is_auth_valid()`: Returns `true` if the authentication provided is valid or `false` if not.
172
+
-`get_simple_metrics()`: Returns the metrics provided in the simple expanded format. It will also convert relative timestamps into explicit timestamps for easier processing.
173
+
-`expects_token_answer()`: Return `true` if the payload requested tokens in the answer. You can set the tokens to be returned by calling `add_tokens_to_answer(token_list)` with `token_list` being a list of token strings.
174
+
-`expects_time_answer()`: Return `true` if the payload requested either relative time or absolute time in the answer. You can set the time to be returned by calling `add_time_to_answer(target_datetime)` with `target_datetime` being a datetime object. The function will automatically provide it in the correct format based on the request.
175
+
-`add_settings_to_answer(settings_dict)`: Will add the provided settings dictionnary to the answer.
176
+
-`add_extra_data_to_answer(extra_data_dict)`: Will add the provided extra data dictionnary to the answer.
177
+
-`get_answer()`: Will return the answer as a string based on the request and the data added to answer, it will automatically handle the authentication and fomatting.
178
+
179
+
180
+
**Example - Full Request flow:**
181
+
182
+
```python
183
+
from openpaygo import MetricsHandler
184
+
from my_db_service import get_device, store_metric
185
+
186
+
187
+
@app.route('/dd')
188
+
defdevice_data():
189
+
# We load the metrics
190
+
try:
191
+
metrics = MetricsHandler(request.data)
192
+
exceptValueErroras e:
193
+
return {'error': 'Invalid data format'}, 400
194
+
# We get the serial number and load the device data from our storage
0 commit comments