wildland_http_client/evs/
client.rs

1//
2// Wildland Project
3//
4// Copyright © 2022 Golem Foundation
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License version 3 as published by
8// the Free Software Foundation.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18use std::rc::Rc;
19
20use serde::{Deserialize, Serialize};
21
22use crate::cross_platform_http_client::{CurrentPlatformClient, HttpClient, Request};
23use crate::error::WildlandHttpClientError;
24use crate::response_handler::check_status_code;
25
26#[derive(Debug, Serialize, Deserialize)]
27pub struct ConfirmTokenReq {
28    pub session_id: String,
29    pub email: String,
30    pub verification_token: String,
31}
32
33#[derive(Debug, Serialize, Deserialize)]
34pub struct GetStorageReq {
35    pub session_id: Option<String>,
36    pub email: String,
37}
38
39#[derive(Debug, Serialize, Deserialize, Clone)]
40pub struct GetStorageRes {
41    pub credentials: Option<String>,
42    pub session_id: Option<String>,
43}
44
45#[derive(Clone)]
46pub struct EvsClient {
47    pub(crate) http_client: Rc<dyn HttpClient>,
48}
49
50impl EvsClient {
51    #[tracing::instrument(level = "debug", skip_all)]
52    pub fn new(base_url: &str) -> Self {
53        let http_client = Rc::new(CurrentPlatformClient {
54            base_url: base_url.into(),
55        });
56
57        Self { http_client }
58    }
59
60    #[tracing::instrument(level = "debug", skip_all)]
61    pub fn confirm_token(&self, request: ConfirmTokenReq) -> Result<(), WildlandHttpClientError> {
62        let request = Request::new("/confirm_token").with_json(&request);
63        let response = self.http_client.put(request)?;
64        check_status_code(response)?;
65        Ok(())
66    }
67
68    #[tracing::instrument(level = "debug", skip_all)]
69    pub fn get_storage(
70        &self,
71        request: GetStorageReq,
72    ) -> Result<GetStorageRes, WildlandHttpClientError> {
73        let request = Request::new("/get_storage").with_json(&request);
74        let response = self.http_client.put(request)?;
75        let response = check_status_code(response)?;
76        let json = response.deserialize()?;
77        Ok(json)
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use mockall::predicate::eq;
84    use serde_json::json;
85
86    use super::*;
87    use crate::cross_platform_http_client::{MockHttpClient, Response};
88    use crate::evs::constants::test_utilities::{CREDENTIALS, EMAIL, VERIFICATION_TOKEN};
89
90    #[test]
91    fn should_confirm_token() {
92        let mut http_client = Box::new(MockHttpClient::new());
93
94        let request = ConfirmTokenReq {
95            email: EMAIL.into(),
96            verification_token: VERIFICATION_TOKEN.into(),
97            session_id: "some uuid".to_string(),
98        };
99
100        let http_request = Request::new("/confirm_token").with_json(&request);
101
102        http_client
103            .as_mut()
104            .expect_put()
105            .with(eq(http_request))
106            .times(1)
107            .returning(|_| {
108                Ok(Response {
109                    status_code: 200,
110                    body: vec![],
111                })
112            });
113
114        let response = EvsClient {
115            http_client: Rc::from(http_client as Box<dyn HttpClient>),
116        }
117        .confirm_token(request);
118
119        assert!(response.is_ok());
120    }
121
122    #[test]
123    fn should_get_storage() {
124        let mut http_client = Box::new(MockHttpClient::new());
125
126        let request = GetStorageReq {
127            email: EMAIL.into(),
128            session_id: Some("some uuid".to_string()),
129        };
130
131        let http_request = Request::new("/get_storage").with_json(&request);
132
133        http_client
134            .as_mut()
135            .expect_put()
136            .with(eq(http_request))
137            .times(1)
138            .returning(|_| {
139                Ok(Response {
140                    status_code: 200,
141                    body: serde_json::to_vec(&json!({ "credentials": CREDENTIALS })).unwrap(),
142                })
143            });
144
145        let response = EvsClient {
146            http_client: Rc::from(http_client as Box<dyn HttpClient>),
147        }
148        .get_storage(request)
149        .unwrap();
150        assert_eq!(response.credentials.unwrap(), CREDENTIALS);
151    }
152}