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
123
124
125
126
127
128
129
130
use reqwest::blocking::Client;
use serde_json::Value;
use crate::api::{HttpMethods, Service};
use super::common::{OpenWhisk,WhiskError,whisk_errors};
use http::StatusCode;


/// A Client to make Requests with.
#[derive(Debug, Default)]
pub struct NativeClient(Client);

impl OpenWhisk for NativeClient {
    /// NativeClient - Http Client (Here client is Reqwest Client)
    type Output = NativeClient;
    /// Creates New WhiskClient
    ///
    /// # Arguments
    /// * `insecure` - Option of Bool to specify connection type
    fn new_whisk_client(insecure: Option<bool>) -> Self::Output {
        match insecure {
            Some(x) => match x {
                true => NativeClient(
                    reqwest::blocking::Client::builder()
                        .danger_accept_invalid_certs(x)
                        .timeout(None)
                        .build()
                        .unwrap(),
                ),
                false => NativeClient(
                    reqwest::blocking::Client::builder()
                        .timeout(None)
                        .build()
                        .unwrap(),
                ),
            },
            None => todo!(),
        }
    }
}

impl Service for NativeClient {
    type Output = reqwest::blocking::RequestBuilder;

    ///
    /// Creates New Request and Returns  `reqwest::blocking::RequestBuilder`
    ///
    /// # Arguments
    /// * `method`   - Option of HTTPMethods
    /// * `url`      - API Host url
    /// * `use_auth` - Option of tuple conatining Username and Password
    /// * `body`     - Option of value which can have parameters necessary for the body of request
    ///
    fn new_request(
        &self,
        method: Option<HttpMethods>,
        url: &str,
        use_auth: Option<(&str, &str)>,
        body: Option<Value>,
    ) -> Result<Self::Output, String> {
        let body = body.unwrap_or(serde_json::json!({}));

        match use_auth {
            Some(auth) => {
                let user = auth.0;
                let pass = auth.1;

                match method {
                    Some(http_method) => match http_method {
                        HttpMethods::GET => {
                            return Ok(self.0.get(url).basic_auth(user, Some(pass)))
                        }
                        HttpMethods::POST => {
                            return Ok(self.0.post(url).basic_auth(user, Some(pass)).json(&body))
                        }
                        HttpMethods::PUT => {
                            return Ok(self.0.put(url).basic_auth(user, Some(pass)).json(&body))
                        }
                        HttpMethods::DELETE => {
                            return Ok(self.0.delete(url).basic_auth(user, Some(pass)).json(&body))
                        }
                    },
                    None => Err(format!("Falied to create request")),
                }
            }
            None => match method {
                Some(http_method) => match http_method {
                    HttpMethods::GET => return Ok(self.0.get(url)),
                    HttpMethods::POST => return Ok(self.0.post(url).json(&body)),
                    HttpMethods::PUT => return Ok(self.0.put(url).json(&body)),
                    HttpMethods::DELETE => return Ok(self.0.delete(url).json(&body)),
                },
                None => Err(format!("Falied to create request")),
            },
        }
    }

    ///
    /// To invoke request and get response out of request execution
    ///
    /// # Arguments
    ///
    /// * `request` - Http request with url,auth and body
    ///
    ///
    ///
    fn invoke_request(&self, request: Self::Output) -> Result<Value, String> {
        match request.send() {
            Ok(response) => match response.status().clone() {
                StatusCode::OK => Ok(response.json().unwrap()),
                _ => {
                    let code = response.status();
                    let error: WhiskError = response.json().unwrap();

                    Err(whisk_errors(code, error.error))
                }
            },
            Err(error) => Err(format!("{}", error)),
        }
    }
}

impl Clone for NativeClient {
    fn clone(&self) -> Self {
        NativeClient(self.0.clone())
    }

    fn clone_from(&mut self, _source: &Self) {
        NativeClient(self.0.clone());
    }
}