ya_client/activity/
provider.rs

1//! Provider part of the Activity API
2use ya_client_model::activity::{ActivityState, ActivityUsage, ProviderEvent, ACTIVITY_API_PATH};
3
4use crate::{web::default_on_timeout, web::WebClient, web::WebInterface, Result};
5use chrono::{DateTime, Utc};
6use std::time::Duration;
7
8#[derive(Clone)]
9pub struct ActivityProviderApi {
10    client: WebClient,
11}
12
13impl WebInterface for ActivityProviderApi {
14    const API_URL_ENV_VAR: &'static str = crate::activity::ACTIVITY_URL_ENV_VAR;
15    const API_SUFFIX: &'static str = ACTIVITY_API_PATH;
16
17    fn from_client(client: WebClient) -> Self {
18        ActivityProviderApi { client }
19    }
20}
21
22/// Bindings for Provider part of the Activity API.
23impl ActivityProviderApi {
24    /// Fetch list of activity_ids
25    pub async fn get_activity_ids(&self) -> Result<Vec<String>> {
26        self.client.get("activity").send().json().await
27    }
28
29    /// Fetch activity state (which may include error details)
30    pub async fn get_activity_state(&self, activity_id: &str) -> Result<ActivityState> {
31        let uri = url_format!("activity/{activity_id}/state");
32        self.client.get(&uri).send().json().await
33    }
34
35    /// Set state of specified Activity.
36    pub async fn set_activity_state(&self, activity_id: &str, state: &ActivityState) -> Result<()> {
37        let uri = url_format!("activity/{activity_id}/state");
38        self.client.put(&uri).send_json(&state).json().await
39    }
40
41    /// Fetch current activity usage (which may include error details)
42    pub async fn get_activity_usage(&self, activity_id: &str) -> Result<ActivityUsage> {
43        let uri = url_format!("activity/{activity_id}/usage");
44        self.client.get(&uri).send().json().await
45    }
46
47    /// Get agreement corresponding to the activity
48    pub async fn get_activity_agreement(&self, activity_id: &str) -> Result<String> {
49        let uri = url_format!("activity/{activity_id}/agreement");
50        self.client.get(&uri).send().json().await
51    }
52
53    /// Fetch Requestor command events.
54    #[rustfmt::skip]
55    pub async fn get_activity_events(
56        &self,
57        after_timestamp: Option<DateTime<Utc>>,
58        app_session_id: Option<String>,
59        timeout: Option<Duration>,
60        max_events: Option<u32>,
61    ) -> Result<Vec<ProviderEvent>> {
62        let after_timestamp = after_timestamp.map(|ts| ts.to_rfc3339());
63        let timeout = timeout.map(|d| d.as_secs_f32());
64        let url = url_format!(
65            "events",
66            #[query] after_timestamp,
67            #[query] app_session_id,
68            #[query] timeout,
69            #[query] max_events,
70        );
71
72        self.client.get(&url).send().json().await.or_else(default_on_timeout)
73    }
74}