pinterest_api/
options.rs

1use std::time::Duration;
2
3use reqwest::RequestBuilder;
4
5const URL_PREFIX: &str = "https://api.pinterest.com/v5";
6const ENV_KEY: &str = "PINTEREST_PREFIX_API";
7
8#[derive(Debug, Clone, Default)]
9pub struct ApiOptions {
10    pub prefix_url: Option<String>,
11    pub timeout: Option<Duration>,
12}
13
14pub fn clear_prefix_url() {
15    std::env::set_var(ENV_KEY, URL_PREFIX);
16}
17
18pub fn setup_prefix_url(url: &str) {
19    std::env::set_var(ENV_KEY, url);
20}
21
22pub(crate) fn make_url(postfix_url: &str, options: &Option<ApiOptions>) -> String {
23    make_url_with_prefix(
24        &std::env::var(ENV_KEY).unwrap_or(URL_PREFIX.to_owned()),
25        options,
26        postfix_url,
27    )
28}
29
30fn make_url_with_prefix(
31    default_perfix_url: &str,
32    options: &Option<ApiOptions>,
33    postfix_url: &str,
34) -> String {
35    let prefix_url = if let Some(options) = options {
36        if let Some(prefix_url) = options.prefix_url.as_ref() {
37            prefix_url
38        } else {
39            default_perfix_url
40        }
41    } else {
42        default_perfix_url
43    };
44    format!("{}{}", prefix_url, postfix_url)
45}
46
47pub(crate) fn apply_options(
48    builder: RequestBuilder,
49    options: &Option<ApiOptions>,
50) -> RequestBuilder {
51    if let Some(options) = options {
52        if let Some(timeout) = options.timeout {
53            builder.timeout(timeout)
54        } else {
55            builder
56        }
57    } else {
58        builder
59    }
60}