Skip to main content

floopy/resources/
evaluations.rs

1use std::sync::Arc;
2
3use reqwest::Method;
4
5use crate::constants::{
6    evaluation_by_id, evaluation_cancel, evaluation_results, ENDPOINT_EVALUATIONS,
7};
8use crate::error::Result;
9use crate::http::HttpTransport;
10use crate::options::RequestOptions;
11use crate::types::{EvaluationCreateParams, EvaluationResultsPage, EvaluationRun};
12
13use super::require;
14
15/// Runs and inspects dataset evaluations.
16pub struct Evaluations {
17    t: Arc<HttpTransport>,
18}
19
20impl Evaluations {
21    pub(crate) fn new(t: Arc<HttpTransport>) -> Self {
22        Self { t }
23    }
24
25    /// Start an evaluation run.
26    ///
27    /// # Errors
28    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
29    /// failure.
30    pub async fn create(
31        &self,
32        params: EvaluationCreateParams,
33        req: impl Into<Option<RequestOptions>>,
34    ) -> Result<EvaluationRun> {
35        let body =
36            serde_json::to_value(&params).map_err(|e| crate::Error::Decode(e.to_string()))?;
37        let (data, _) = self
38            .t
39            .request(
40                Method::POST,
41                ENDPOINT_EVALUATIONS,
42                Some(&body),
43                &[],
44                req.into().as_ref(),
45            )
46            .await?;
47        require(data)
48    }
49
50    /// Fetch an evaluation run by id.
51    ///
52    /// # Errors
53    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
54    /// failure.
55    pub async fn get(
56        &self,
57        evaluation_id: &str,
58        req: impl Into<Option<RequestOptions>>,
59    ) -> Result<EvaluationRun> {
60        let (data, _) = self
61            .t
62            .request(
63                Method::GET,
64                &evaluation_by_id(evaluation_id),
65                None,
66                &[],
67                req.into().as_ref(),
68            )
69            .await?;
70        require(data)
71    }
72
73    /// Cancel a running evaluation.
74    ///
75    /// # Errors
76    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
77    /// failure.
78    pub async fn cancel(
79        &self,
80        evaluation_id: &str,
81        req: impl Into<Option<RequestOptions>>,
82    ) -> Result<EvaluationRun> {
83        let (data, _) = self
84            .t
85            .request(
86                Method::POST,
87                &evaluation_cancel(evaluation_id),
88                None,
89                &[],
90                req.into().as_ref(),
91            )
92            .await?;
93        require(data)
94    }
95
96    /// Fetch a page of scored rows for an evaluation.
97    ///
98    /// # Errors
99    /// Returns an [`Error`](crate::Error) on a non-2xx response or transport
100    /// failure.
101    pub async fn results(
102        &self,
103        evaluation_id: &str,
104        limit: Option<u32>,
105        cursor: Option<&str>,
106        req: impl Into<Option<RequestOptions>>,
107    ) -> Result<EvaluationResultsPage> {
108        let mut query = Vec::new();
109        if let Some(limit) = limit {
110            query.push(("limit".to_owned(), limit.to_string()));
111        }
112        if let Some(cursor) = cursor {
113            query.push(("cursor".to_owned(), cursor.to_owned()));
114        }
115        let (data, _) = self
116            .t
117            .request(
118                Method::GET,
119                &evaluation_results(evaluation_id),
120                None,
121                &query,
122                req.into().as_ref(),
123            )
124            .await?;
125        require(data)
126    }
127}