gql_client/
types.rs

1use std::collections::HashMap;
2#[cfg(not(target_arch = "wasm32"))]
3use std::convert::TryFrom;
4
5use serde::{Deserialize, Serialize};
6
7#[cfg(not(target_arch = "wasm32"))]
8use crate::GraphQLError;
9
10/// GQL client config
11#[derive(Clone, Debug, Deserialize, Serialize)]
12pub struct ClientConfig {
13  /// the endpoint about graphql server
14  pub endpoint: String,
15  /// gql query timeout, unit: seconds
16  pub timeout: Option<u64>,
17  /// additional request header
18  pub headers: Option<HashMap<String, String>>,
19  /// request proxy
20  pub proxy: Option<GQLProxy>,
21}
22
23/// proxy type
24#[derive(Clone, Debug, Deserialize, Serialize)]
25pub enum ProxyType {
26  Http,
27  Https,
28  All,
29}
30
31/// proxy auth, basic_auth
32#[derive(Clone, Debug, Deserialize, Serialize)]
33pub struct ProxyAuth {
34  pub username: String,
35  pub password: String,
36}
37
38/// request proxy
39#[derive(Clone, Debug, Deserialize, Serialize)]
40pub struct GQLProxy {
41  /// schema, proxy url
42  pub schema: String,
43  /// proxy type
44  pub type_: ProxyType,
45  /// auth
46  pub auth: Option<ProxyAuth>,
47}
48
49#[cfg(not(target_arch = "wasm32"))]
50impl TryFrom<GQLProxy> for reqwest::Proxy {
51  type Error = GraphQLError;
52
53  fn try_from(gql_proxy: GQLProxy) -> Result<Self, Self::Error> {
54    let proxy = match gql_proxy.type_ {
55      ProxyType::Http => reqwest::Proxy::http(gql_proxy.schema),
56      ProxyType::Https => reqwest::Proxy::https(gql_proxy.schema),
57      ProxyType::All => reqwest::Proxy::all(gql_proxy.schema),
58    }
59    .map_err(|e| Self::Error::with_text(format!("{:?}", e)))?;
60    Ok(proxy)
61  }
62}