summaryrefslogtreecommitdiff
path: root/makima/src/daemon/api/chain.rs
blob: c37c980e85794eafde1efbd588ac31d51e4ab680 (plain) (blame)
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
//! Chain API methods.

use uuid::Uuid;

use super::client::{ApiClient, ApiError};
use super::supervisor::JsonValue;
use crate::db::models::CreateChainRequest;

impl ApiClient {
    /// Create a new chain with contracts.
    pub async fn create_chain(&self, req: CreateChainRequest) -> Result<JsonValue, ApiError> {
        self.post("/api/v1/chains", &req).await
    }

    /// List all chains for the authenticated user.
    pub async fn list_chains(
        &self,
        status: Option<&str>,
        limit: i32,
    ) -> Result<JsonValue, ApiError> {
        let mut params = Vec::new();
        if let Some(s) = status {
            params.push(format!("status={}", s));
        }
        params.push(format!("limit={}", limit));
        let query_string = format!("?{}", params.join("&"));
        self.get(&format!("/api/v1/chains{}", query_string)).await
    }

    /// Get a chain by ID.
    pub async fn get_chain(&self, chain_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/chains/{}", chain_id)).await
    }

    /// Get contracts in a chain.
    pub async fn get_chain_contracts(&self, chain_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/chains/{}/contracts", chain_id))
            .await
    }

    /// Get chain DAG structure for visualization.
    pub async fn get_chain_graph(&self, chain_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/chains/{}/graph", chain_id))
            .await
    }

    /// Archive a chain.
    pub async fn archive_chain(&self, chain_id: Uuid) -> Result<JsonValue, ApiError> {
        self.delete_with_response(&format!("/api/v1/chains/{}", chain_id))
            .await
    }

    /// Start a chain (creates root contracts and optionally a supervisor).
    pub async fn start_chain(&self, chain_id: Uuid) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!("/api/v1/chains/{}/start", chain_id))
            .await
    }

    /// Start a chain with supervisor enabled.
    pub async fn start_chain_with_supervisor(
        &self,
        chain_id: Uuid,
        repository_url: Option<&str>,
    ) -> Result<JsonValue, ApiError> {
        #[derive(serde::Serialize)]
        #[serde(rename_all = "camelCase")]
        struct StartRequest<'a> {
            with_supervisor: bool,
            repository_url: Option<&'a str>,
        }
        let req = StartRequest {
            with_supervisor: true,
            repository_url,
        };
        self.post(&format!("/api/v1/chains/{}/start", chain_id), &req)
            .await
    }
}