meter-lib 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of meter-lib might be problematic. Click here for more details.

meter_lib/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .core import fetch_customer_id, post_meter_usage
meter_lib/core.py ADDED
@@ -0,0 +1,56 @@
1
+ import requests
2
+
3
+ def fetch_customer_id(tenant_id: str):
4
+ """
5
+ Fetch the customer account for a given tenant_id.
6
+ Returns a dict with customer info, or None if not found/error.
7
+ """
8
+ url = f"http://metering.metering.svc.cluster.local:8000/api/v1/customer-accounts/tenant/{tenant_id}"
9
+ try:
10
+ response = requests.get(url, timeout=10)
11
+ response.raise_for_status()
12
+ data = response.json()
13
+
14
+ # Handle list response
15
+ if isinstance(data, list) and len(data) > 0:
16
+ return data[0]
17
+ elif isinstance(data, dict):
18
+ return data
19
+ else:
20
+ print(f"No customer account found for tenant {tenant_id}")
21
+ return None
22
+
23
+ except requests.RequestException as e:
24
+ print(f"Error fetching {url}: {e}")
25
+ return None
26
+
27
+
28
+ def post_meter_usage(tenant_id: str, device_id: str, meter_id: str, total_usage: int) :
29
+ """
30
+ Posts meter usage to the events API.
31
+ Uses tenant_id + device_id in headers and includes customer_id in payload.
32
+ """
33
+ customer_account = fetch_customer_id(tenant_id)
34
+ if not customer_account:
35
+ print("No customer account available, skipping meter usage post")
36
+ return None
37
+
38
+ url = "http://metering.metering.svc.cluster.local:8000/api/v1/events"
39
+ headers = {
40
+ "x-tenant-id": tenant_id,
41
+ "x-device-id": device_id,
42
+ "Content-Type": "application/json"
43
+ }
44
+ payload = {
45
+ "meter_id": meter_id,
46
+ "total_usage": total_usage,
47
+ "customer_id": customer_account["id"]
48
+ }
49
+
50
+ try:
51
+ response = requests.post(url, headers=headers, json=payload, timeout=10)
52
+ response.raise_for_status()
53
+ return response.json()
54
+ except requests.RequestException as e:
55
+ print(f" Error posting to {url}: {e}")
56
+ return None
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: meter-lib
3
+ Version: 0.0.2
4
+ Summary: A litewave library to collect the customer credit usage
5
+ Project-URL: Homepage, https://github.com/aiorch/meter-lib
6
+ Project-URL: Repository, https://github.com/aiorch/meter-lib
7
+ Project-URL: Issues, https://github.com/aiorch/meter-lib/issues
8
+ Author-email: Sruthi <sruthi@litewave.ai>
9
+ License: MIT
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Requires-Python: >=3.10
16
+ Requires-Dist: requests>=2.28
17
+ Description-Content-Type: text/markdown
18
+
19
+ # meter-lib
@@ -0,0 +1,5 @@
1
+ meter_lib/__init__.py,sha256=3iRDH_E4qSKZVLaiJAs4YjoFC8jPIvH4sbgL6x4pAT8,53
2
+ meter_lib/core.py,sha256=Qi5OL4rg4iwweDTeBOljYrnSwU19SgU8naIA2cflGSo,1824
3
+ meter_lib-0.0.2.dist-info/METADATA,sha256=bMPeM5NU5YA3Go0moCWkXUqw4YrgU0ZNDbuU2ybMr6g,715
4
+ meter_lib-0.0.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
+ meter_lib-0.0.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any