ad4m_client/
runtime.rs

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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use std::sync::Arc;

use crate::{
    types::{PerspectiveExpression, SentPerspectiveMessage},
    util::query,
    ClientInfo,
};
use anyhow::{Context, Result};
use graphql_client::GraphQLQuery;

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct Info;

pub async fn info(executor_url: String, cap_token: String) -> Result<info::InfoRuntimeInfo> {
    let response_data: info::ResponseData = query(
        executor_url,
        cap_token,
        Info::build_query(info::Variables {}),
    )
    .await
    .with_context(|| "Failed to run runtime->info query")?;
    Ok(response_data.runtime_info)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct Quit;

pub async fn quit(executor_url: String, cap_token: String) -> Result<quit::ResponseData> {
    query(
        executor_url,
        cap_token,
        Quit::build_query(quit::Variables {}),
    )
    .await
    .with_context(|| "Failed to run runtime->quit query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct AddTrustedAgents;

pub async fn add_trusted_agents(
    executor_url: String,
    cap_token: String,
    agents: Vec<String>,
) -> Result<add_trusted_agents::ResponseData> {
    query(
        executor_url,
        cap_token,
        AddTrustedAgents::build_query(add_trusted_agents::Variables { agents }),
    )
    .await
    .with_context(|| "Failed to run runtime->add-trusted-agents query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct DeleteTrustedAgents;

pub async fn delete_trusted_agents(
    executor_url: String,
    cap_token: String,
    agents: Vec<String>,
) -> Result<delete_trusted_agents::ResponseData> {
    query(
        executor_url,
        cap_token,
        DeleteTrustedAgents::build_query(delete_trusted_agents::Variables { agents }),
    )
    .await
    .with_context(|| "Failed to run runtime -> delete-trusted-agents query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct TrustedAgents;

pub async fn trusted_agents(executor_url: String, cap_token: String) -> Result<Vec<String>> {
    let response_data: trusted_agents::ResponseData = query(
        executor_url,
        cap_token,
        TrustedAgents::build_query(trusted_agents::Variables {}),
    )
    .await
    .with_context(|| "Failed to run runtime->trusted-agents query")?;
    Ok(response_data.get_trusted_agents)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct LinkLanguageTemplates;

pub async fn link_language_templates(
    executor_url: String,
    cap_token: String,
) -> Result<Vec<String>> {
    let response_data: link_language_templates::ResponseData = query(
        executor_url,
        cap_token,
        LinkLanguageTemplates::build_query(link_language_templates::Variables {}),
    )
    .await
    .with_context(|| "Failed to run runtime->link-language-templates query")?;

    Ok(response_data.runtime_known_link_language_templates)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct AddLinkLanguageTemplates;

pub async fn add_link_language_templates(
    executor_url: String,
    cap_token: String,
    addresses: Vec<String>,
) -> Result<add_link_language_templates::ResponseData> {
    query(
        executor_url,
        cap_token,
        AddLinkLanguageTemplates::build_query(add_link_language_templates::Variables { addresses }),
    )
    .await
    .with_context(|| "Failed to run runtime->add-link-language-templates query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct RemoveLinkLanguageTemplates;

pub async fn remove_link_language_templates(
    executor_url: String,
    cap_token: String,
    addresses: Vec<String>,
) -> Result<remove_link_language_templates::ResponseData> {
    query(
        executor_url,
        cap_token,
        RemoveLinkLanguageTemplates::build_query(remove_link_language_templates::Variables {
            addresses,
        }),
    )
    .await
    .with_context(|| "Failed to run runtime->remove-link-language-templates query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct Friends;

pub async fn friends(executor_url: String, cap_token: String) -> Result<Vec<String>> {
    let response_data: friends::ResponseData = query(
        executor_url,
        cap_token,
        Friends::build_query(friends::Variables {}),
    )
    .await
    .with_context(|| "Failed to run runtime->friends query")?;
    Ok(response_data.runtime_friends)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct AddFriends;

pub async fn add_friends(
    executor_url: String,
    cap_token: String,
    dids: Vec<String>,
) -> Result<add_friends::ResponseData> {
    query(
        executor_url,
        cap_token,
        AddFriends::build_query(add_friends::Variables { dids }),
    )
    .await
    .with_context(|| "Failed to run runtime->add-friends query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct RemoveFriends;

pub async fn remove_friends(
    executor_url: String,
    cap_token: String,
    dids: Vec<String>,
) -> Result<remove_friends::ResponseData> {
    query(
        executor_url,
        cap_token,
        RemoveFriends::build_query(remove_friends::Variables { dids }),
    )
    .await
    .with_context(|| "Failed to run runtime->remove-friends query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct HcAgentInfos;

pub async fn hc_agent_infos(executor_url: String, cap_token: String) -> Result<String> {
    let response_data: hc_agent_infos::ResponseData = query(
        executor_url,
        cap_token,
        HcAgentInfos::build_query(hc_agent_infos::Variables {}),
    )
    .await
    .with_context(|| "Failed to run runtime->hc-agent-infos query")?;
    Ok(response_data.runtime_hc_agent_infos)
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct HcAddAgentInfos;

pub async fn hc_add_agent_infos(
    executor_url: String,
    cap_token: String,
    agent_infos: String,
) -> Result<hc_add_agent_infos::ResponseData> {
    query(
        executor_url,
        cap_token,
        HcAddAgentInfos::build_query(hc_add_agent_infos::Variables { agent_infos }),
    )
    .await
    .with_context(|| "Failed to run runtime->hc-add-agent-infos query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct VerifyStringSignedByDid;

pub async fn verify_string_signed_by_did(
    executor_url: String,
    cap_token: String,
    did: String,
    did_signing_key_id: String,
    data: String,
    signed_data: String,
) -> Result<verify_string_signed_by_did::ResponseData> {
    query(
        executor_url,
        cap_token,
        VerifyStringSignedByDid::build_query(verify_string_signed_by_did::Variables {
            did,
            did_signing_key_id,
            data,
            signed_data,
        }),
    )
    .await
    .with_context(|| "Failed to run runtime->verify-string-signed-by-did query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct SetStatus;

pub async fn set_status(
    executor_url: String,
    cap_token: String,
    status: set_status::PerspectiveInput,
) -> Result<set_status::ResponseData> {
    query(
        executor_url,
        cap_token,
        SetStatus::build_query(set_status::Variables { status }),
    )
    .await
    .with_context(|| "Failed to run runtime->set-status query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct FriendStatus;

pub async fn friend_status(
    executor_url: String,
    cap_token: String,
    did: String,
) -> Result<friend_status::ResponseData> {
    query(
        executor_url,
        cap_token,
        FriendStatus::build_query(friend_status::Variables { did }),
    )
    .await
    .with_context(|| "Failed to run runtime->friend-status query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct FriendSendMessage;

pub async fn friend_send_message(
    executor_url: String,
    cap_token: String,
    did: String,
    message: friend_send_message::PerspectiveInput,
) -> Result<friend_send_message::ResponseData> {
    query(
        executor_url,
        cap_token,
        FriendSendMessage::build_query(friend_send_message::Variables { did, message }),
    )
    .await
    .with_context(|| "Failed to run runtime->friend-send-message query")
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct MessageInbox;

pub async fn message_inbox(
    executor_url: String,
    cap_token: String,
    filter: Option<String>,
) -> Result<Vec<PerspectiveExpression>> {
    let response: message_inbox::ResponseData = query(
        executor_url,
        cap_token,
        MessageInbox::build_query(message_inbox::Variables { filter }),
    )
    .await
    .with_context(|| "Failed to run runtime->message-inbox query")?;

    Ok(response
        .runtime_message_inbox
        .into_iter()
        .map(|d| d.into())
        .collect())
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.gql",
    query_path = "src/runtime.gql",
    response_derives = "Debug"
)]
pub struct MessageOutbox;

pub async fn message_outbox(
    executor_url: String,
    cap_token: String,
    filter: Option<String>,
) -> Result<Vec<SentPerspectiveMessage>> {
    let response: message_outbox::ResponseData = query(
        executor_url,
        cap_token,
        MessageOutbox::build_query(message_outbox::Variables { filter }),
    )
    .await
    .with_context(|| "Failed to run runtime->message-outbox query")?;

    Ok(response
        .runtime_message_outbox
        .into_iter()
        .map(|d| d.into())
        .collect())
}

pub struct RuntimeClient {
    info: Arc<ClientInfo>,
}

impl RuntimeClient {
    pub fn new(info: Arc<ClientInfo>) -> Self {
        Self { info }
    }

    pub async fn info(&self) -> Result<info::InfoRuntimeInfo> {
        info(self.info.executor_url.clone(), self.info.cap_token.clone()).await
    }

    pub async fn quit(&self) -> Result<quit::ResponseData> {
        quit(self.info.executor_url.clone(), self.info.cap_token.clone()).await
    }

    pub async fn add_trusted_agents(
        &self,
        agents: Vec<String>,
    ) -> Result<add_trusted_agents::ResponseData> {
        add_trusted_agents(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            agents,
        )
        .await
    }

    pub async fn delete_trusted_agents(
        &self,
        agents: Vec<String>,
    ) -> Result<delete_trusted_agents::ResponseData> {
        delete_trusted_agents(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            agents,
        )
        .await
    }

    pub async fn trusted_agents(&self) -> Result<Vec<String>> {
        trusted_agents(self.info.executor_url.clone(), self.info.cap_token.clone()).await
    }

    pub async fn link_language_templates(&self) -> Result<Vec<String>> {
        link_language_templates(self.info.executor_url.clone(), self.info.cap_token.clone()).await
    }

    pub async fn add_link_language_templates(
        &self,
        addresses: Vec<String>,
    ) -> Result<add_link_language_templates::ResponseData> {
        add_link_language_templates(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            addresses,
        )
        .await
    }

    pub async fn remove_link_language_templates(
        &self,
        addresses: Vec<String>,
    ) -> Result<remove_link_language_templates::ResponseData> {
        remove_link_language_templates(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            addresses,
        )
        .await
    }

    pub async fn friends(&self) -> Result<Vec<String>> {
        friends(self.info.executor_url.clone(), self.info.cap_token.clone()).await
    }

    pub async fn add_friends(&self, friends: Vec<String>) -> Result<add_friends::ResponseData> {
        add_friends(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            friends,
        )
        .await
    }

    pub async fn remove_friends(
        &self,
        friends: Vec<String>,
    ) -> Result<remove_friends::ResponseData> {
        remove_friends(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            friends,
        )
        .await
    }

    pub async fn hc_agent_infos(&self) -> Result<String> {
        hc_agent_infos(self.info.executor_url.clone(), self.info.cap_token.clone()).await
    }

    pub async fn hc_add_agent_infos(
        &self,
        agent_infos: String,
    ) -> Result<hc_add_agent_infos::ResponseData> {
        hc_add_agent_infos(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            agent_infos,
        )
        .await
    }

    pub async fn verify_string_signed_by_did(
        &self,
        did: String,
        did_signing_key_id: String,
        data: String,
        signed_data: String,
    ) -> Result<bool> {
        let response = verify_string_signed_by_did(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            did,
            did_signing_key_id,
            data,
            signed_data,
        )
        .await?;

        Ok(response.runtime_verify_string_signed_by_did)
    }

    pub async fn set_status(&self, status: set_status::PerspectiveInput) -> Result<()> {
        set_status(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            status,
        )
        .await?;

        Ok(())
    }

    pub async fn friend_status(&self, did: String) -> Result<friend_status::ResponseData> {
        friend_status(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            did,
        )
        .await
    }

    pub async fn friend_send_message(
        &self,
        did: String,
        message: friend_send_message::PerspectiveInput,
    ) -> Result<friend_send_message::ResponseData> {
        friend_send_message(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            did,
            message,
        )
        .await
    }

    pub async fn message_inbox(
        &self,
        filter: Option<String>,
    ) -> Result<Vec<PerspectiveExpression>> {
        message_inbox(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            filter,
        )
        .await
    }

    pub async fn message_outbox(
        &self,
        filter: Option<String>,
    ) -> Result<Vec<SentPerspectiveMessage>> {
        message_outbox(
            self.info.executor_url.clone(),
            self.info.cap_token.clone(),
            filter,
        )
        .await
    }
}