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
use crate::authorization::HEADER_KEY;
use crate::response::error::{ErrorResponse, ErrorResponseBody};
use crate::response::form::form_settings_prints::{
    GetFormSettingsPrintsResponse, GetFormSettingsPrintsResponseBody,
};
use crate::Query;

const RESOURCE: &str = "forms";
const NESTED_RESOURCE: &str = "versions";
const LAST_RESOURCE: &str = "settings/prints";

#[derive(Debug, Clone)]
pub struct FormSettingsPrints {
    url: String,
    authorization_header: String,
}

impl FormSettingsPrints {
    pub fn new(url: &str, authorization_header: &str) -> Self {
        Self {
            url: url.to_string() + RESOURCE,
            authorization_header: authorization_header.to_string(),
        }
    }

    pub async fn get(
        &self,
        form_id: i32,
        form_version: i32,
        query: Query,
    ) -> Result<GetFormSettingsPrintsResponse, ErrorResponse> {
        let request_url = format!(
            "{}/{}/{}/{}/{}",
            &self.url, form_id, NESTED_RESOURCE, form_version, LAST_RESOURCE,
        );

        let http_client = reqwest::Client::new();
        let result = http_client
            .get(request_url)
            .query(&query.to_queries())
            .header(HEADER_KEY, &self.authorization_header)
            .send()
            .await;

        match result {
            Ok(resp) => {
                let status = resp.status().as_u16();

                if status == 200 {
                    match resp.json::<GetFormSettingsPrintsResponseBody>().await {
                        Ok(body) => Ok(GetFormSettingsPrintsResponse { status, body }),
                        Err(err) => {
                            let body = ErrorResponseBody {
                                error: true,
                                messages: vec![err.to_string()],
                            };
                            let error_response = ErrorResponse { status, body };
                            Err(error_response)
                        }
                    }
                } else {
                    match resp.json::<ErrorResponseBody>().await {
                        Ok(body) => {
                            let error_response = ErrorResponse { status, body };
                            Err(error_response)
                        }
                        Err(err) => {
                            let body = ErrorResponseBody {
                                error: true,
                                messages: vec![err.to_string()],
                            };
                            let error_response = ErrorResponse { status, body };
                            Err(error_response)
                        }
                    }
                }
            }
            Err(err) => {
                let body = ErrorResponseBody {
                    error: true,
                    messages: vec![err.to_string()],
                };
                let error_response = ErrorResponse { status: 500, body };
                Err(error_response)
            }
        }
    }
}