forked from openwallet-foundation/owl-agent-test-harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_backchannel_client.py
213 lines (177 loc) · 6.98 KB
/
agent_backchannel_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import asyncio
from aiohttp import (
web,
ClientSession,
ClientRequest,
ClientResponse,
ClientError,
ClientTimeout,
)
import json
from time import sleep
######################################################################
# coroutine utilities
######################################################################
def run_coroutine(coroutine):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(coroutine())
finally:
loop.close()
def run_coroutine_with_args(coroutine, *args):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(coroutine(*args))
finally:
loop.close()
def run_coroutine_with_kwargs(coroutine, *args, **kwargs):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(coroutine(*args, **kwargs))
finally:
loop.close()
async def make_agent_backchannel_request(
method, path, data=None, text=False, params=None
) -> (int, str):
params = {k: v for (k, v) in (params or {}).items() if v is not None}
client_session: ClientSession = ClientSession()
async with client_session.request(method, path, json=data, params=params) as resp:
resp_status = resp.status
resp_text = await resp.text()
await client_session.close()
return (resp_status, resp_text)
def agent_backchannel_GET(url, topic, operation=None, id=None) -> (int, str):
agent_url = url + topic + "/"
if operation:
agent_url = agent_url + operation + "/"
if id:
agent_url = agent_url + id
(resp_status, resp_text) = run_coroutine_with_kwargs(
make_agent_backchannel_request, "GET", agent_url
)
return (resp_status, resp_text)
def agent_backchannel_POST(
url, topic, operation=None, id=None, data=None
) -> (int, str):
agent_url = url + topic + "/"
payload = {}
if data:
payload["data"] = data
if operation:
agent_url = agent_url + operation + "/"
if id:
if topic == "credential":
payload["cred_ex_id"] = id
else:
payload["id"] = id
(resp_status, resp_text) = run_coroutine_with_kwargs(
make_agent_backchannel_request, "POST", agent_url, data=payload
)
return (resp_status, resp_text)
def agent_backchannel_DELETE(url, topic, id=None, data=None) -> (int, str):
agent_url = url + topic + "/"
if id:
agent_url = agent_url + id
(resp_status, resp_text) = run_coroutine_with_kwargs(
make_agent_backchannel_request, "DELETE", agent_url
)
return (resp_status, resp_text)
def expected_agent_state(
agent_url, protocol_txt, thread_id, status_txt, wait_time=2.0, sleep_time=0.5
):
sleep(sleep_time)
state = "None"
if type(status_txt) != list:
status_txt = [status_txt]
# "N/A" means that the backchannel can't determine the state - we'll treat this as a successful response
status_txt.append("N/A")
for i in range(int(wait_time / sleep_time)):
(resp_status, resp_text) = agent_backchannel_GET(
agent_url + "/agent/command/", protocol_txt, id=thread_id
)
if resp_status == 200:
resp_json = json.loads(resp_text)
state = resp_json["state"]
if state in status_txt:
return True
sleep(sleep_time)
print(
"From",
agent_url,
"Expected state",
status_txt,
"but received",
state,
", with a response status of",
resp_status,
)
return False
def check_if_already_connected(context, sender, receiver):
# get receiver DID
receiver_url = context.config.userdata.get(receiver)
(resp_status, resp_text) = agent_backchannel_GET(
receiver_url + "/agent/command/", "did"
)
if resp_status == 200:
resp_json = json.loads(resp_text)
# assign thier_did
receiver_did = resp_json["did"]
# call GET connections for the sender with the receivers DID
sender_url = context.config.userdata.get(sender)
(sender_resp_status, sender_resp_text) = agent_backchannel_GET(
sender_url + "/agent/command/", "active-connection", id=receiver_did
)
if sender_resp_status == 200:
sender_resp_json = json.loads(sender_resp_text)
sender_connection_id = sender_resp_json["connection_id"]
sender_did = sender_resp_json["my_did"]
# call GET connections for the receiver with the senders did
(resp_status, resp_text) = agent_backchannel_GET(
receiver_url + "/agent/command/", "active-connection", id=sender_did
)
if resp_status == 200:
resp_json = json.loads(resp_text)
receiver_connection_id = resp_json["connection_id"]
# Populate connection id dictionary in context
if not hasattr(context, "connection_id_dict"):
context.connection_id_dict = {}
context.connection_id_dict[sender] = {}
context.connection_id_dict[receiver] = {}
context.connection_id_dict[sender][receiver] = sender_connection_id
context.connection_id_dict[receiver][sender] = receiver_connection_id
return True
else:
raise Exception(
f"Problem retreiving receiver's ({receiver}) connection id for active connection. Senders ({sender}) active connection info: {sender_resp_text}"
)
return False
def setup_already_connected(
context, requester_connection_info_json, requester, responder
):
requester_connection_id = requester_connection_info_json["connection_id"]
requester_did = requester_connection_info_json["my_did"]
responder_url = context.config.userdata.get(responder)
# call GET connection for the responder with the requester did
(resp_status, resp_text) = agent_backchannel_GET(
responder_url + "/agent/command/", "active-connection", id=requester_did
)
if resp_status == 200:
resp_json = json.loads(resp_text)
responder_connection_id = resp_json["connection_id"]
# Populate connection id dictionary in context
if not hasattr(context, "connection_id_dict"):
context.connection_id_dict = {}
if requester not in context.connection_id_dict:
context.connection_id_dict[requester] = {}
if responder not in context.connection_id_dict:
context.connection_id_dict[responder] = {}
context.connection_id_dict[requester][responder] = requester_connection_id
context.connection_id_dict[responder][requester] = responder_connection_id
return True
else:
raise Exception(
f"Problem retreiving responder's ({responder}) connection id for active connection. Requester's ({requester}) active connection info: {resp_text}"
)