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
use std::collections::HashMap;
#[cfg(not(target_arch = "wasm32"))]
use std::convert::TryFrom;

use serde::{Deserialize, Serialize};

#[cfg(not(target_arch = "wasm32"))]
use crate::GraphQLError;

/// GQL client config
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ClientConfig {
  /// the endpoint about graphql server
  pub endpoint: String,
  /// gql query timeout, unit: seconds
  pub timeout: Option<u64>,
  /// additional request header
  pub headers: Option<HashMap<String, String>>,
  /// request proxy
  pub proxy: Option<GQLProxy>,
}

/// proxy type
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum ProxyType {
  Http,
  Https,
  All,
}

/// proxy auth, basic_auth
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProxyAuth {
  pub username: String,
  pub password: String,
}

/// request proxy
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GQLProxy {
  /// schema, proxy url
  pub schema: String,
  /// proxy type
  pub type_: ProxyType,
  /// auth
  pub auth: Option<ProxyAuth>,
}

#[cfg(not(target_arch = "wasm32"))]
impl TryFrom<GQLProxy> for reqwest::Proxy {
  type Error = GraphQLError;

  fn try_from(gql_proxy: GQLProxy) -> Result<Self, Self::Error> {
    let proxy = match gql_proxy.type_ {
      ProxyType::Http => reqwest::Proxy::http(gql_proxy.schema),
      ProxyType::Https => reqwest::Proxy::https(gql_proxy.schema),
      ProxyType::All => reqwest::Proxy::all(gql_proxy.schema),
    }
    .map_err(|e| Self::Error::with_text(format!("{:?}", e)))?;
    Ok(proxy)
  }
}