dfns_sdk_rs/utils/
fetch.rs

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// @dfns-sdk-rs/src/utils/fetch.rs

use crate::{
    error::{DfnsError, PolicyPendingError},
    models::generic::DfnsBaseApiOptions,
    utils::nonce::generate_nonce,
};
use reqwest::{Client, Method, Response, StatusCode};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use url::Url;

const DEFAULT_DFNS_BASE_URL: &str = "https://api.dfns.io";
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum HttpMethod {
    GET,
    POST,
    PUT,
    DELETE,
}

impl From<HttpMethod> for Method {
    fn from(method: HttpMethod) -> Self {
        match method {
            HttpMethod::GET => Method::GET,
            HttpMethod::POST => Method::POST,
            HttpMethod::PUT => Method::PUT,
            HttpMethod::DELETE => Method::DELETE,
        }
    }
}

#[derive(Debug, Clone)]
pub struct FetchOptions<T> {
    pub method: HttpMethod,
    pub headers: Option<HashMap<String, String>>,
    pub body: Option<Value>,
    pub api_options: T,
}

pub type FetchResult = Result<Response, DfnsError>;

pub trait Fetch {
    async fn execute(&self, url: &str, options: FetchOptions<DfnsBaseApiOptions>) -> FetchResult;
}

pub struct DfnsFetch {
    client: Client,
}

impl Clone for DfnsFetch {
    fn clone(&self) -> Self {
        Self {
            client: Client::new(),
        }
    }
}

impl std::fmt::Debug for DfnsFetch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DfnsFetch")
            .field("client", &"Client")
            .finish()
    }
}

impl PartialEq for DfnsFetch {
    fn eq(&self, _other: &Self) -> bool {
        true
    }
}

impl DfnsFetch {
    pub fn new() -> Self {
        Self {
            client: Client::new(),
        }
    }

    #[warn(dead_code)]
    async fn handle_response(&self, response: Response) -> FetchResult {
        if response.status().is_success() {
            Ok(response)
        } else {
            let status = response.status();
            let body: Value = response.json().await.unwrap_or_default();

            if status == StatusCode::ACCEPTED {
                Err(PolicyPendingError::new(Some(body)).into())
            } else {
                let message = body
                    .get("error")
                    .and_then(|e| e.get("message"))
                    .or_else(|| body.get("message"))
                    .and_then(|m| m.as_str())
                    .unwrap_or("Unknown error")
                    .to_string();

                Err(DfnsError::new(status.as_u16(), message, Some(body)))
            }
        }
    }
}

impl Fetch for DfnsFetch {
    async fn execute(
        &self,
        resource: &str,
        options: FetchOptions<DfnsBaseApiOptions>,
    ) -> FetchResult {
        let base_url = options
            .api_options
            .base_url
            .unwrap_or_else(|| DEFAULT_DFNS_BASE_URL.to_string());
        let url = Url::parse(&base_url)?.join(resource)?;

        let mut headers = reqwest::header::HeaderMap::new();

        headers.insert("x-dfns-appid", options.api_options.app_id.parse()?);
        headers.insert("x-dfns-nonce", generate_nonce().parse()?);
        headers.insert("x-dfns-sdk-version", VERSION.parse()?);

        if let Some(app_secret) = options.api_options.app_secret {
            headers.insert("x-dfns-appsecret", app_secret.parse()?);
        }

        if let Some(auth_token) = options.api_options.auth_token {
            headers.insert("authorization", format!("Bearer {}", auth_token).parse()?);
        }

        if let Some(custom_headers) = options.headers {
            for (key, value) in custom_headers {
                let key_str: &'static str = Box::leak(key.into_boxed_str());
                headers.insert(key_str, value.parse()?);
            }
        }

        let mut request = self
            .client
            .request(options.method.into(), url)
            .headers(headers);

        if let Some(body) = options.body {
            request = request
                .header("content-type", "application/json")
                .json(&body);
        }

        request.send().await.map_err(|e| e.into())
    }
}

pub async fn simple_fetch<T: Serialize + DeserializeOwned>(
    resource: &str,
    options: FetchOptions<DfnsBaseApiOptions>,
) -> Result<T, DfnsError> {
    let fetch = DfnsFetch::new();
    let response = fetch.execute(resource, options).await?;
    response.json::<T>().await.map_err(|e| e.into())
}