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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Used to interact with the [Training Endpoints](https://replicate.com/docs/reference/http#trainings.create).
//!
//!
//! # Example
//!
//! TODO
//!
//!

use std::collections::HashMap;

use crate::api_definitions::{CreateTraining, GetTraining, ListTraining, WebhookEvents};

pub struct TrainingOptions {
    pub destination: String,

    pub input: HashMap<String, String>,

    pub webhook: String,
    _webhook_events_filter: Option<WebhookEvents>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct CreateTrainingPayload {
    pub destination: String,

    pub input: HashMap<String, String>,

    pub webhook: String,
}

// #[derive(Clone)]
pub struct Training {
    // Holds a reference to a Replicate
    pub parent: crate::client::Client,
}

impl Training {
    pub fn new(rep: crate::client::Client) -> Self {
        Self { parent: rep }
    }

    pub fn create(
        &self,
        model_owner: String,
        model_name: String,
        version_id: String,
        options: TrainingOptions,
    ) -> Result<CreateTraining, Box<dyn std::error::Error>> {
        let client = reqwest::blocking::Client::new();

        let payload = CreateTrainingPayload {
            destination: options.destination,
            input: options.input,
            webhook: options.webhook,
        };

        let response = client
            .post(format!(
                "{}/models/{}/{}/versions/{}/trainings",
                self.parent.base_url, model_owner, model_name, version_id,
            ))
            .header("Authorization", format!("Token {}", self.parent.auth))
            .header("User-Agent", &self.parent.user_agent)
            .json(&payload)
            .send()?;

        let response_string = response.text()?;
        let response_struct: CreateTraining = serde_json::from_str(&response_string)?;

        Ok(response_struct)
    }

    pub fn get(&self, training_id: String) -> Result<GetTraining, Box<dyn std::error::Error>> {
        let client = reqwest::blocking::Client::new();

        let response = client
            .get(format!(
                "{}/trainings/{}",
                self.parent.base_url, training_id,
            ))
            .header("Authorization", format!("Token {}", self.parent.auth))
            .header("User-Agent", &self.parent.user_agent)
            .send()?;

        let response_string = response.text()?;
        let response_struct: GetTraining = serde_json::from_str(&response_string)?;

        Ok(response_struct)
    }

    pub fn list(&self) -> Result<ListTraining, Box<dyn std::error::Error>> {
        let client = reqwest::blocking::Client::new();

        let response = client
            .get(format!("{}/trainings", self.parent.base_url,))
            .header("Authorization", format!("Token {}", self.parent.auth))
            .header("User-Agent", &self.parent.user_agent)
            .send()?;

        let response_string = response.text()?;
        let response_struct: ListTraining = serde_json::from_str(&response_string)?;

        Ok(response_struct)
    }

    // Perhaps the training_id should be automatically derives, just like prediction one
    pub fn cancel(&self, training_id: String) -> Result<(), Box<dyn std::error::Error>> {
        let client = reqwest::blocking::Client::new();

        client
            .get(format!(
                "{}/trainings/{}/cancel",
                self.parent.base_url, training_id
            ))
            .header("Authorization", format!("Token {}", self.parent.auth))
            .header("User-Agent", &self.parent.user_agent)
            .send()?;

        Ok(())
    }
}