Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make service_token optional and refactor to implement only in base class #40

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 9 additions & 5 deletions payments_py/ai_query_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import json
from typing import Any, List, Optional, Union
from payments_py.data_models import AgentExecutionStatus, Step, TaskLog, Task
from payments_py.data_models import AgentExecutionStatus, ServiceTokenResultDto, Step, TaskLog, Task
from payments_py.nvm_backend import BackendApiOptions, NVMBackendApi

# Define API Endpoints
Expand Down Expand Up @@ -67,7 +67,7 @@ async def log_task(self, task_log: TaskLog):
await self.socket_client.emit('_task-log', json.dumps(data))


async def create_task(self, did: str, task: Task, _callback: Optional[Any]=None):
async def create_task(self, did: str, task: Task, _callback: Optional[Any]=None, token: Optional[ServiceTokenResultDto]=None):
"""
Subscribers can create an AI Task for an Agent. The task must contain the input query that will be used by the AI Agent.
This method is used by subscribers of a Payment Plan required to access a specific AI Agent or Service. Users who are not subscribers won't be able to create AI Tasks for that Agent.
Expand All @@ -78,6 +78,7 @@ async def create_task(self, did: str, task: Task, _callback: Optional[Any]=None)
did (str): The DID of the service.
task (Task): The task to create.
_callback (Any): The callback to execute when a new task log event is received (optional)
token (ServiceTokenResultDto): The service token (optional)


Example:
Expand All @@ -91,7 +92,8 @@ async def create_task(self, did: str, task: Task, _callback: Optional[Any]=None)
print('Task created:', task.json())
"""
endpoint = self.parse_url_to_proxy(TASK_ENDPOINT).replace('{did}', did)
token = self.get_service_token(did)
if not token:
token = self.get_service_token(did)
result = self.post(endpoint, task, headers={'Authorization': f'Bearer {token.accessToken}'})
if(result.status_code == 201 and _callback):
tasks = result.json()
Expand Down Expand Up @@ -139,7 +141,7 @@ def search_tasks(self, search_params: Any):
"""
return self.post(self.parse_url_to_backend(SEARCH_TASKS_ENDPOINT), search_params)

def get_task_with_steps(self, did: str, task_id: str):
def get_task_with_steps(self, did: str, task_id: str, token: Optional[ServiceTokenResultDto]=None):
"""
It returns the full task and the steps resulted of the execution of the task.

Expand All @@ -149,9 +151,11 @@ def get_task_with_steps(self, did: str, task_id: str):
Args:
did (str): The DID of the service.
task_id (str): The task ID.
token (ServiceTokenResultDto): The service token (optional)
"""
endpoint = self.parse_url_to_proxy(GET_TASK_ENDPOINT).replace('{did}', did).replace('{taskId}', task_id)
token = self.get_service_token(did)
if not token:
token = self.get_service_token(did)
return self.get(endpoint, headers={'Authorization': f'Bearer {token.accessToken}'})

def get_steps_from_task(self, did: str, task_id: str, status: Optional[str] = None):
Expand Down
31 changes: 1 addition & 30 deletions payments_py/payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class Payments(NVMBackendApi):
order_plan: Orders the plan.
get_asset_ddo: Gets the asset DDO.
get_plan_balance: Gets the plan balance.
get_service_token: Gets the service token.
get_plan_associated_services: Gets the plan associated services.
get_plan_associated_files: Gets the plan associated files.
get_plan_details: Gets the plan details.
Expand Down Expand Up @@ -567,7 +566,7 @@ def get_plan_balance(self, plan_did: str, account_address: Optional[str] = None)

Raises:
HTTPError: If the API call fails.

Example:
response = your_instance.get_plan_balance(plan_did="did:example:123456", account_address="0xABC123")
response.raise_for_status()
Expand Down Expand Up @@ -602,34 +601,6 @@ def get_plan_balance(self, plan_did: str, account_address: Optional[str] = None)
}
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.
This configuration includes:
- The JWT access token
- The Proxy url that can be used to query the agent/service.

Args:
service_did (str): The DID of the service.

Returns:
ServiceTokenResultDto: The result of the creation operation.

Raises:
HTTPError: If the API call fails.

Example:
response = your_instance.get_service_token(service_did="did:nv:xyz789")
print(response)
"""
url = f"{self.environment.value['backend']}/api/v1/payments/service/token/{service_did}"
response = self.get(url)
response.raise_for_status()
return ServiceTokenResultDto.model_validate(response.json()['token'])

def get_plan_associated_services(self, plan_did: str):
"""
Get array of services/agent DIDs associated with a payment plan.
Expand Down
Loading