dune_api/pipelines/
api.rs

1//! Pipelines API implementation
2
3use super::types::*;
4use crate::client::Client;
5use crate::error::{self, Error, Result};
6
7/// Pipelines API
8pub struct PipelinesApi<'a> {
9    client: &'a Client,
10}
11
12impl<'a> PipelinesApi<'a> {
13    pub(crate) fn new(client: &'a Client) -> Self {
14        Self { client }
15    }
16
17    /// Execute a pipeline
18    pub async fn execute(
19        &self,
20        request: &ExecutePipelineRequest,
21    ) -> Result<ExecutePipelineResponse> {
22        let url = format!("{}/v1/pipelines/execute", self.client.base_url());
23        let response = self.client.http().post(&url).json(request).send().await?;
24
25        if response.status().is_success() {
26            Ok(response.json().await?)
27        } else if response.status() == 402 {
28            Err(error::insufficient_credits())
29        } else if response.status() == 429 {
30            Err(Error::rate_limited(None))
31        } else {
32            let status = response.status().as_u16();
33            let message = response.text().await.unwrap_or_default();
34            Err(Error::api(status, message))
35        }
36    }
37
38    /// Get pipeline execution status
39    pub async fn status(&self, pipeline_execution_id: &str) -> Result<PipelineExecutionStatus> {
40        let url = format!(
41            "{}/v1/pipelines/executions/{}/status",
42            self.client.base_url(),
43            pipeline_execution_id
44        );
45        let response = self.client.http().get(&url).send().await?;
46
47        if response.status().is_success() {
48            Ok(response.json().await?)
49        } else if response.status() == 404 {
50            Err(error::not_found(format!(
51                "Pipeline execution {}",
52                pipeline_execution_id
53            )))
54        } else {
55            let status = response.status().as_u16();
56            let message = response.text().await.unwrap_or_default();
57            Err(Error::api(status, message))
58        }
59    }
60}