From f72c0ea55d5bb981e9b750ff417c40a8f5ac4823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charly=20L=C3=B3pez?= Date: Thu, 5 Dec 2024 08:13:29 +0100 Subject: [PATCH 1/3] added optional account_address to get_plan_balance, fixed create_task event handling --- payments_py/ai_query_api.py | 9 +++++---- payments_py/payments.py | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/payments_py/ai_query_api.py b/payments_py/ai_query_api.py index af18e1f..16bdc49 100644 --- a/payments_py/ai_query_api.py +++ b/payments_py/ai_query_api.py @@ -94,7 +94,8 @@ async def create_task(self, did: str, task: Task, _callback: Optional[Any]=None) token = self.get_service_token(did) result = self.post(endpoint, task, headers={'Authorization': f'Bearer {token.accessToken}'}) if(result.status_code == 201 and _callback): - await self.subscribe_task_logs(_callback, [result["task"]["task_id"]]) + tasks = result.json() + await self.subscribe_task_logs(_callback, [tasks["task"]["task_id"]]) return result def create_steps(self, did: str, task_id: str, steps: List[Step]): @@ -221,9 +222,9 @@ async def subscribe_task_logs(self, callback: Any, tasks: List[str]): if not tasks: raise Exception('No task rooms to join in configuration') await self.connect_socket() - - await self.socket_client.on('_connected', self._on_connected(callback, tasks)) + self.socket_client.on('_connected', self._on_connected(callback, tasks)) except Exception as error: + print(error) raise Exception(f"Unable to initialize websocket client: {self.web_socket_host} - {str(error)}") @@ -235,6 +236,6 @@ async def handle_connected_event(*args): async def handle_task_log_event(data: Any): callback(data) - await self.socket_client.on('task-log', handle_task_log_event) + self.socket_client.on('task-log', handle_task_log_event) return handle_connected_event diff --git a/payments_py/payments.py b/payments_py/payments.py index c30684e..2586695 100644 --- a/payments_py/payments.py +++ b/payments_py/payments.py @@ -1,6 +1,7 @@ import os from typing import List, Optional import requests +import jwt from payments_py.data_models import BalanceResultDto, BurnResultDto, CreateAssetResultDto, DownloadFileResultDto, MintResultDto, OrderPlanResultDto, ServiceTokenResultDto from payments_py.environments import Environment @@ -49,6 +50,8 @@ def __init__(self, nvm_api_key: str, environment: Environment, self.environment = environment self.app_id = app_id self.version = version + decoded_jwt = jwt.decode(self.nvm_api_key, options={"verify_signature": False}) + self.account_address = decoded_jwt.get('sub') if ai_protocol: self.ai_protocol = AIQueryApi(self.backend_options) @@ -551,13 +554,13 @@ def get_asset_ddo(self, did: str): response = self.get(f"{self.environment.value['backend']}/api/v1/payments/asset/ddo/{did}") return response - def get_plan_balance(self, plan_did: str, account_address: str) -> BalanceResultDto: + def get_plan_balance(self, plan_did: str, account_address: Optional[str] = None) -> BalanceResultDto: """ Get the balance of an account for a Payment Plan. Args: plan_did (str): The DID of the plan. - account_address (str): The account address. + account_address (Optional[str]): The account address. Defaults to `self.account_address` if not provided. Returns: BalanceResultDto: The response from the API call formatted as a BalanceResultDto. @@ -579,13 +582,18 @@ def get_plan_balance(self, plan_did: str, account_address: str) -> BalanceResult "balance": 10000000 } """ + # Use self.account_address if account_address is not provided + account_address = account_address or self.account_address + body = { "subscriptionDid": plan_did, - **{snake_to_camel(k): v for k, v in locals().items() if v is not None and k != 'self'} + "accountAddress": account_address, } + url = (f"{self.environment.value['backend']}/api/v1/payments/subscription/balance") response = self.post(url, body) response.raise_for_status() + balance = { "planType": response.json()['subscriptionType'], "isOwner": response.json()['isOwner'], @@ -594,6 +602,9 @@ def get_plan_balance(self, plan_did: str, account_address: str) -> BalanceResult } return BalanceResultDto.model_validate(balance) + def get_service_access_config(self, service_did: str) -> ServiceTokenResultDto: + return self.get_service_token(service_did) + def get_service_token(self, service_did: str) -> ServiceTokenResultDto: """ Get the required configuration for accessing a remote service agent. From ecf1ac384f9192549d790850bd14f421869923db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charly=20L=C3=B3pez?= Date: Thu, 5 Dec 2024 08:19:27 +0100 Subject: [PATCH 2/3] bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index af96e86..a7cbb06 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "payments-py" -version = "0.5.2" +version = "0.5.3" description = "" authors = ["enrique "] readme = "README.md" From 06c1a586910277475c475a41d023e24157a642ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charly=20L=C3=B3pez?= Date: Thu, 5 Dec 2024 08:41:19 +0100 Subject: [PATCH 3/3] remove print --- payments_py/ai_query_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/payments_py/ai_query_api.py b/payments_py/ai_query_api.py index 16bdc49..dc0eafc 100644 --- a/payments_py/ai_query_api.py +++ b/payments_py/ai_query_api.py @@ -224,7 +224,6 @@ async def subscribe_task_logs(self, callback: Any, tasks: List[str]): await self.connect_socket() self.socket_client.on('_connected', self._on_connected(callback, tasks)) except Exception as error: - print(error) raise Exception(f"Unable to initialize websocket client: {self.web_socket_host} - {str(error)}")