viaduct/
client.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::{new_backend::get_backend, settings::validate_request, Request, Response, Result};
6
7/// HTTP Client
8///
9/// This represents the "new" API.
10/// See `README.md` for details about the transition from the old to new API.
11#[derive(Default)]
12pub struct Client {
13    settings: ClientSettings,
14}
15
16impl Client {
17    pub fn new(settings: ClientSettings) -> Self {
18        Self { settings }
19    }
20
21    pub async fn send(&self, request: Request) -> Result<Response> {
22        validate_request(&request)?;
23        get_backend()?
24            .send_request(request, self.settings.clone())
25            .await
26    }
27
28    pub fn send_sync(&self, request: Request) -> Result<Response> {
29        pollster::block_on(self.send(request))
30    }
31}
32
33#[derive(Debug, uniffi::Record, Clone)]
34#[repr(C)]
35pub struct ClientSettings {
36    // Timeout for the entire request in ms (0 indicates no timeout).
37    #[uniffi(default = 0)]
38    pub timeout: u32,
39    // Maximum amount of redirects to follow (0 means redirects are not allowed)
40    #[uniffi(default = 10)]
41    pub redirect_limit: u32,
42}
43
44impl Default for ClientSettings {
45    fn default() -> Self {
46        Self {
47            #[cfg(target_os = "ios")]
48            timeout: 7000,
49            #[cfg(not(target_os = "ios"))]
50            timeout: 10000,
51            redirect_limit: 10,
52        }
53    }
54}