55import signal
66import sys
77import threading
8-
8+ import jwt
9+ import socketio
10+ import requests
911import time
12+
1013from datetime import datetime , timezone , timedelta
1114from importlib .metadata import version
1215from typing import List , Optional , Union , Dict , Any , Callable
13-
14- import requests
15- import socketio
1616from web3 import Web3
17+ from requests .auth import AuthBase
1718
1819from virtuals_acp .account import ACPAccount
1920from virtuals_acp .configs .configs import (
4950
5051
5152
52- import jwt
53- import requests
54-
55- from requests .auth import AuthBase
56-
5753class BearerAuth (AuthBase ):
5854 def __init__ (self , get_access_token : Callable [[], str ]):
5955 self ._get_access_token = get_access_token
@@ -70,7 +66,8 @@ def clear_token(self):
7066
7167
7268class ACPApiClient :
73- def __init__ (self , acp_url : str , wallet_address : str , require_auth : bool = False ):
69+ def __init__ (self , acp_contract_client : BaseAcpContractClient , acp_url : str , wallet_address : str , require_auth : bool = False ):
70+ self .acp_contract_client = acp_contract_client
7471 self .base_url = f"{ acp_url } /api"
7572 self .wallet_address = wallet_address
7673 self .require_auth = require_auth
@@ -130,13 +127,17 @@ def get_access_token(self) -> str:
130127 needs_refresh = True
131128
132129 if not needs_refresh :
133- return self .access_token
130+ # Access token is still valid
131+ if self .access_token :
132+ return self .access_token
133+ else :
134+ raise Exception ("Access token needs refreshing!" )
134135
135136 self .access_token = self .refresh_token ()
136137 return self .access_token
137138
138139 def refresh_token (self ) -> str :
139- challenge = self .get_auth_challenge (self . wallet_address )
140+ challenge = self .get_auth_challenge ()
140141 signature = self .acp_contract_client .sign_typed_data (challenge )
141142
142143 verified = self .verify_auth_challenge (
@@ -204,8 +205,8 @@ def __init__(
204205 "All contract clients must have the same agent wallet address"
205206 )
206207
207- self .acp_client = ACPApiClient (self .acp_url , self .wallet_address )
208- self .no_auth_acp_client = ACPApiClient (self .acp_url , self .wallet_address , require_auth = False )
208+ self .acp_client = ACPApiClient (self .acp_contract_client , self . acp_url , self .wallet_address )
209+ self .no_auth_acp_client = ACPApiClient (self .acp_contract_client , self . acp_url , self .wallet_address , require_auth = False )
209210
210211 # Socket.IO setup
211212 self .on_new_task = on_new_task
@@ -235,7 +236,6 @@ def init(self):
235236 logger .info (f"Initializing socket" )
236237
237238 try :
238- # TODO: auth needs to include access token now
239239 auth_data = {
240240 "walletAddress" : self .wallet_address ,
241241 "accessToken" : self .acp_client .get_access_token ()
0 commit comments