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
use http::header::CONTENT_TYPE;
use http::{HeaderMap, HeaderValue};
use std::collections::HashMap;
use url::Url;

pub struct AuthorizationRequestParts {
    pub(crate) uri: Url,
    pub(crate) form_urlencoded: HashMap<String, String>,
    pub(crate) basic_auth: Option<(String, String)>,
    pub(crate) headers: HeaderMap,
}

impl AuthorizationRequestParts {
    pub fn new(
        uri: Url,
        form_urlencoded: HashMap<String, String>,
        basic_auth: Option<(String, String)>,
    ) -> AuthorizationRequestParts {
        let mut headers = HeaderMap::new();
        headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_static("application/x-www-form-urlencoded"),
        );
        AuthorizationRequestParts {
            uri,
            form_urlencoded,
            basic_auth,
            headers,
        }
    }

    pub fn with_extra_headers(&mut self, extra_headers: &HeaderMap) {
        for (header_name, header_value) in extra_headers.iter() {
            self.headers.insert(header_name, header_value.clone());
        }
    }

    pub fn with_extra_query_parameters(&mut self, extra_query_params: &HashMap<String, String>) {
        for (key, value) in extra_query_params.iter() {
            self.uri
                .query_pairs_mut()
                .append_pair(key.as_ref(), value.as_ref());
        }
    }
}