ad4m_client/
expressions.rs

1use std::sync::Arc;
2
3use crate::{util::query, ClientInfo};
4use anyhow::{Context, Result};
5use graphql_client::GraphQLQuery;
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8
9#[derive(GraphQLQuery, Debug)]
10#[graphql(
11    schema_path = "schema.gql",
12    query_path = "src/expressions.gql",
13    response_derives = "Debug"
14)]
15pub struct ExpressionCreate;
16
17pub async fn expression_create(
18    executor_url: String,
19    cap_token: String,
20    language_address: String,
21    content: Value,
22) -> Result<String> {
23    let content = serde_json::to_string(&content)?;
24    let response_data: expression_create::ResponseData = query(
25        executor_url,
26        cap_token,
27        ExpressionCreate::build_query(expression_create::Variables {
28            language_address,
29            content,
30        }),
31    )
32    .await
33    .with_context(|| "Failed to run expressions->create mutation")?;
34    Ok(response_data.expression_create)
35}
36
37#[derive(GraphQLQuery, Debug, Serialize, Deserialize)]
38#[graphql(
39    schema_path = "schema.gql",
40    query_path = "src/expressions.gql",
41    response_derives = "Debug"
42)]
43pub struct Expression;
44
45pub async fn expression(
46    executor_url: String,
47    cap_token: String,
48    url: String,
49) -> Result<Option<expression::ExpressionExpression>> {
50    let response_data: expression::ResponseData = query(
51        executor_url,
52        cap_token,
53        Expression::build_query(expression::Variables { url }),
54    )
55    .await
56    .with_context(|| "Failed to run expressions->get query")?;
57    Ok(response_data.expression)
58}
59
60pub struct ExpressionsClient {
61    info: Arc<ClientInfo>,
62}
63
64impl ExpressionsClient {
65    pub fn new(info: Arc<ClientInfo>) -> Self {
66        Self { info }
67    }
68
69    pub async fn expression_create(
70        &self,
71        language_address: String,
72        content: Value,
73    ) -> Result<String> {
74        expression_create(
75            self.info.executor_url.clone(),
76            self.info.cap_token.clone(),
77            language_address,
78            content,
79        )
80        .await
81    }
82
83    pub async fn expression(
84        &self,
85        url: String,
86    ) -> Result<Option<expression::ExpressionExpression>> {
87        expression(
88            self.info.executor_url.clone(),
89            self.info.cap_token.clone(),
90            url,
91        )
92        .await
93    }
94}