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
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use minreq::{Error, Response};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateStorageRes {
    #[serde(rename(deserialize = "id"))]
    pub storage_id: String,
    #[serde(rename(deserialize = "credentialID"))]
    pub credentials_id: String,
    #[serde(rename(deserialize = "credentialSecret"))]
    pub credentials_secret: String,
}

#[derive(Clone, Default, Debug)]
pub(crate) struct SCStorageClient {
    pub(crate) base_url: String,
}

impl SCStorageClient {
    #[tracing::instrument(level = "debug", ret, skip(self))]
    pub(crate) fn create_storage(&self) -> Result<Response, Error> {
        let url = format!("{}/storage/create", self.base_url);
        minreq::post(url).send()
    }
}

#[cfg(test)]
mod tests {
    use crate::sc::constants::test_utilities::{CREDENTIALS_ID, CREDENTIALS_SECRET, STORAGE_ID};
    use mockito::{mock, server_url};
    use serde_json::json;

    use super::*;

    fn client() -> SCStorageClient {
        SCStorageClient {
            base_url: server_url(),
        }
    }

    #[test]
    fn storage_can_be_created() {
        let m = mock("POST", "/storage/create")
            .with_body(
                json!({
                    "id" : STORAGE_ID,
                    "credentialID" : CREDENTIALS_ID,
                    "credentialSecret" : CREDENTIALS_SECRET
                })
                .to_string(),
            )
            .create();

        let response = client()
            .create_storage()
            .unwrap()
            .json::<CreateStorageRes>()
            .unwrap();

        m.assert();
        assert_eq!(response.storage_id, STORAGE_ID);
        assert_eq!(response.credentials_id, CREDENTIALS_ID);
        assert_eq!(response.credentials_secret, CREDENTIALS_SECRET);
    }
}