chore(WIP): working on fixing macos cross compile

This commit is contained in:
Kasper Juul Hermansen 2023-03-18 20:21:08 +01:00
parent 46208d5681
commit 0f816b5e29
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 520 additions and 67 deletions

190
Cargo.lock generated
View File

@ -132,9 +132,9 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.1.8" version = "4.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d7ae14b20b94cb02149ed21a86c423859cbe18dc7ed69845cace50e52b40a5" checksum = "ce38afc168d8665cfc75c7b1dd9672e50716a137f433f070991619744a67342a"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"clap_lex", "clap_lex",
@ -145,9 +145,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_lex" name = "clap_lex"
version = "0.3.2" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350b9cf31731f9957399229e9b2adc51eeabdfbe9d71d9a0552275fd12710d09" checksum = "033f6b7a4acb1f358c742aaca805c939ee73b4c6209ae4318ec7aca81c42e646"
dependencies = [ dependencies = [
"os_str_bytes", "os_str_bytes",
] ]
@ -515,7 +515,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841" checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841"
dependencies = [ dependencies = [
"crc32fast", "crc32fast",
"libz-sys",
"miniz_oxide", "miniz_oxide",
] ]
@ -687,18 +686,6 @@ version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4" checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4"
[[package]]
name = "gql_client"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e57f4862d3e5cd4ffe8df03fa2137e620e33578bbc1895e6f8f569630e17b1db"
dependencies = [
"log",
"reqwest",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "graphql-introspection-query" name = "graphql-introspection-query"
version = "0.2.0" version = "0.2.0"
@ -874,6 +861,19 @@ dependencies = [
"want", "want",
] ]
[[package]]
name = "hyper-rustls"
version = "0.23.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1788965e61b367cd03a62950836d5cd41560c3577d90e40e0819373194d1661c"
dependencies = [
"http",
"hyper",
"rustls",
"tokio",
"tokio-rustls",
]
[[package]] [[package]]
name = "hyper-tls" name = "hyper-tls"
version = "0.5.0" version = "0.5.0"
@ -930,10 +930,11 @@ dependencies = [
[[package]] [[package]]
name = "io-lifetimes" name = "io-lifetimes"
version = "1.0.6" version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfa919a82ea574332e2de6e74b4c36e74d41982b335080fa59d4ef31be20fdf3" checksum = "0dd6da19f25979c7270e70fa95ab371ec3b701cd0eefc47667a09785b3c59155"
dependencies = [ dependencies = [
"hermit-abi 0.3.1",
"libc", "libc",
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
@ -946,9 +947,9 @@ checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]] [[package]]
name = "is-terminal" name = "is-terminal"
version = "0.4.4" version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b6b32576413a8e69b90e952e4a026476040d81017b80445deda5f2d3921857" checksum = "8687c819457e979cc940d09cb16e42a1bf70aa6b60a549de6d3a62a0ee90c69e"
dependencies = [ dependencies = [
"hermit-abi 0.3.1", "hermit-abi 0.3.1",
"io-lifetimes", "io-lifetimes",
@ -992,17 +993,6 @@ version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "libz-sys"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.1.4" version = "0.1.4"
@ -1125,9 +1115,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]] [[package]]
name = "openssl" name = "openssl"
version = "0.10.45" version = "0.10.46"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b102428fd03bc5edf97f62620f7298614c45cedf287c271e7ed450bbaf83f2e1" checksum = "fd2523381e46256e40930512c7fd25562b9eae4812cb52078f155e87217c9d1e"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cfg-if", "cfg-if",
@ -1155,25 +1145,15 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-src"
version = "111.25.1+1.1.1t"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ef9a9cc6ea7d9d5e7c4a913dc4b48d0e359eddf01af1dfec96ba7064b4aba10"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "openssl-sys" name = "openssl-sys"
version = "0.9.80" version = "0.9.81"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23bbbf7854cd45b83958ebe919f0e8e516793727652e27fda10a8384cfc790b7" checksum = "176be2629957c157240f68f61f2d0053ad3a4ecfdd9ebf1e6521d18d9635cf67"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"cc", "cc",
"libc", "libc",
"openssl-src",
"pkg-config", "pkg-config",
"vcpkg", "vcpkg",
] ]
@ -1384,7 +1364,6 @@ version = "0.11.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9" checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9"
dependencies = [ dependencies = [
"async-compression",
"base64", "base64",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
@ -1394,6 +1373,7 @@ dependencies = [
"http", "http",
"http-body", "http-body",
"hyper", "hyper",
"hyper-rustls",
"hyper-tls", "hyper-tls",
"ipnet", "ipnet",
"js-sys", "js-sys",
@ -1403,11 +1383,14 @@ dependencies = [
"once_cell", "once_cell",
"percent-encoding", "percent-encoding",
"pin-project-lite", "pin-project-lite",
"rustls",
"rustls-pemfile",
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
"tokio", "tokio",
"tokio-native-tls", "tokio-native-tls",
"tokio-rustls",
"tokio-util", "tokio-util",
"tower-service", "tower-service",
"url", "url",
@ -1415,9 +1398,25 @@ dependencies = [
"wasm-bindgen-futures", "wasm-bindgen-futures",
"wasm-streams", "wasm-streams",
"web-sys", "web-sys",
"webpki-roots",
"winreg", "winreg",
] ]
[[package]]
name = "ring"
version = "0.16.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
dependencies = [
"cc",
"libc",
"once_cell",
"spin",
"untrusted",
"web-sys",
"winapi",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.21" version = "0.1.21"
@ -1426,9 +1425,9 @@ checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.36.9" version = "0.36.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd5c6ff11fecd55b40746d1995a02f2eb375bf8c00d192d521ee09f42bef37bc" checksum = "2fe885c3a125aa45213b68cc1472a49880cb5923dc23f522ad2791b882228778"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"errno", "errno",
@ -1438,6 +1437,27 @@ dependencies = [
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
[[package]]
name = "rustls"
version = "0.20.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f"
dependencies = [
"log",
"ring",
"sct",
"webpki",
]
[[package]]
name = "rustls-pemfile"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d194b56d58803a43635bdc398cd17e383d6f71f9182b9a192c127ca42494a59b"
dependencies = [
"base64",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.13" version = "1.0.13"
@ -1459,6 +1479,16 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sct"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4"
dependencies = [
"ring",
"untrusted",
]
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.8.2" version = "2.8.2"
@ -1484,18 +1514,18 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.156" version = "1.0.157"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "314b5b092c0ade17c00142951e50ced110ec27cea304b1037c6969246c2469a4" checksum = "707de5fcf5df2b5788fca98dd7eab490bc2fd9b7ef1404defc462833b83f25ca"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.156" version = "1.0.157"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7e29c4601e36bcec74a223228dce795f4cd3616341a4af93520ca1a837c087d" checksum = "78997f4555c22a7971214540c4a661291970619afd56de19f77e0de86296e1e5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1579,6 +1609,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
@ -1642,18 +1678,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.39" version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.39" version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1726,6 +1762,17 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "tokio-rustls"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59"
dependencies = [
"rustls",
"tokio",
"webpki",
]
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.7" version = "0.7.7"
@ -1856,9 +1903,9 @@ checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.3.11" version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524b68aca1d05e03fdf03fcdce2c6c94b6daf6d16861ddaa7e4f2b6638a9052c" checksum = "7d502c968c6a838ead8e69b2ee18ec708802f99db92a0d156705ec9ef801993b"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
@ -1890,6 +1937,12 @@ dependencies = [
"void", "void",
] ]
[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"
@ -2030,6 +2083,25 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "webpki"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "webpki-roots"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87"
dependencies = [
"webpki",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -20,15 +20,20 @@ serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
reqwest = { version = "0.11.14", features = [
"stream",
"rustls",
"hyper-rustls",
"rustls-tls",
] }
clap = "4.1.6" clap = "4.1.6"
dirs = "4.0.0" dirs = "4.0.0"
flate2 = { version = "1.0.25", features = ["zlib"] } graphql_client = { version = "0.12.0", features = [
graphql-introspection-query = "0.2.0" "reqwest-rustls",
graphql_client = { version = "0.12.0", features = ["reqwest"] } ], default_features = false }
hex = "0.4.3" hex = "0.4.3"
hex-literal = "0.3.4" hex-literal = "0.3.4"
platform-info = "1.0.2" platform-info = "1.0.2"
reqwest = { version = "0.11.14", features = ["stream", "deflate"] }
sha2 = "0.10.6" sha2 = "0.10.6"
tar = "0.4.38" tar = "0.4.38"
tempfile = "3.3.0" tempfile = "3.3.0"

View File

@ -20,13 +20,14 @@ tracing-subscriber = { workspace = true }
base64 = "0.21.0" base64 = "0.21.0"
gql_client = "1.0.7" gql_client = "1.0.7"
dirs = "4.0.0" dirs = "4.0.0"
flate2 = { version = "1.0.25", features = ["zlib"] } flate2 = { version = "1.0.25", features = ["rust_backend"] }
graphql-introspection-query = "0.2.0"
graphql_client = { version = "0.12.0", features = ["reqwest"] }
hex = "0.4.3" hex = "0.4.3"
hex-literal = "0.3.4" hex-literal = "0.3.4"
platform-info = "1.0.2" platform-info = "1.0.2"
reqwest = { version = "0.11.14", features = ["stream", "deflate", "native-tls-vendored"] } reqwest = { version = "0.11.14", features = [
"stream",
"rustls-tls",
], default-features = false }
sha2 = "0.10.6" sha2 = "0.10.6"
tar = "0.4.38" tar = "0.4.38"
tempfile = "3.3.0" tempfile = "3.3.0"

View File

@ -0,0 +1,375 @@
use reqwest::Error;
use reqwest::{Client, Url};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{self, Formatter};
use std::str::FromStr;
#[derive(Clone)]
pub struct GraphQLError {
message: String,
json: Option<Vec<GraphQLErrorMessage>>,
}
// https://spec.graphql.org/June2018/#sec-Errors
#[derive(Deserialize, Debug, Clone)]
#[allow(dead_code)]
pub struct GraphQLErrorMessage {
message: String,
locations: Option<Vec<GraphQLErrorLocation>>,
extensions: Option<HashMap<String, String>>,
path: Option<Vec<GraphQLErrorPathParam>>,
}
#[derive(Deserialize, Debug, Clone)]
#[allow(dead_code)]
pub struct GraphQLErrorLocation {
line: u32,
column: u32,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum GraphQLErrorPathParam {
String(String),
Number(u32),
}
impl GraphQLError {
pub fn with_text(message: impl AsRef<str>) -> Self {
Self {
message: message.as_ref().to_string(),
json: None,
}
}
pub fn with_message_and_json(message: impl AsRef<str>, json: Vec<GraphQLErrorMessage>) -> Self {
Self {
message: message.as_ref().to_string(),
json: Some(json),
}
}
pub fn with_json(json: Vec<GraphQLErrorMessage>) -> Self {
Self::with_message_and_json("Look at json field for more details", json)
}
pub fn message(&self) -> &str {
&self.message
}
pub fn json(&self) -> Option<Vec<GraphQLErrorMessage>> {
self.json.clone()
}
}
fn format(err: &GraphQLError, f: &mut Formatter<'_>) -> fmt::Result {
// Print the main error message
writeln!(f, "\nGQLClient Error: {}", err.message)?;
// Check if query errors have been received
if err.json.is_none() {
return Ok(());
}
let errors = err.json.as_ref();
for err in errors.unwrap() {
writeln!(f, "Message: {}", err.message)?;
}
Ok(())
}
impl fmt::Display for GraphQLError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
format(self, f)
}
}
impl fmt::Debug for GraphQLError {
#[allow(clippy::needless_borrow)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
format(&self, f)
}
}
impl From<Error> for GraphQLError {
fn from(error: Error) -> Self {
Self {
message: error.to_string(),
json: None,
}
}
}
/// 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)
}
}
#[derive(Clone, Debug)]
pub struct GQLClient {
config: ClientConfig,
}
#[derive(Serialize)]
struct RequestBody<T: Serialize> {
query: String,
variables: T,
}
#[derive(Deserialize, Debug)]
struct GraphQLResponse<T> {
data: Option<T>,
errors: Option<Vec<GraphQLErrorMessage>>,
}
impl GQLClient {
fn client(&self) -> Result<Client, GraphQLError> {
let mut builder = Client::builder().timeout(std::time::Duration::from_secs(
self.config.timeout.unwrap_or(5),
));
if let Some(proxy) = &self.config.proxy {
builder = builder.proxy(proxy.clone().try_into()?);
}
builder
.build()
.map_err(|e| GraphQLError::with_text(format!("Can not create client: {:?}", e)))
}
}
impl GQLClient {
pub fn new(endpoint: impl AsRef<str>) -> Self {
Self {
config: ClientConfig {
endpoint: endpoint.as_ref().to_string(),
timeout: None,
headers: Default::default(),
proxy: None,
},
}
}
pub fn new_with_headers(
endpoint: impl AsRef<str>,
headers: HashMap<impl ToString, impl ToString>,
) -> Self {
let _headers: HashMap<String, String> = headers
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.into_iter()
.collect();
Self {
config: ClientConfig {
endpoint: endpoint.as_ref().to_string(),
timeout: None,
headers: Some(_headers),
proxy: None,
},
}
}
pub fn new_with_config(config: ClientConfig) -> Self {
Self { config }
}
}
impl GQLClient {
pub async fn query<K>(&self, query: &str) -> Result<Option<K>, GraphQLError>
where
K: for<'de> Deserialize<'de>,
{
self.query_with_vars::<K, ()>(query, ()).await
}
pub async fn query_unwrap<K>(&self, query: &str) -> Result<K, GraphQLError>
where
K: for<'de> Deserialize<'de>,
{
self.query_with_vars_unwrap::<K, ()>(query, ()).await
}
pub async fn query_with_vars_unwrap<K, T: Serialize>(
&self,
query: &str,
variables: T,
) -> Result<K, GraphQLError>
where
K: for<'de> Deserialize<'de>,
{
match self.query_with_vars(query, variables).await? {
Some(v) => Ok(v),
None => Err(GraphQLError::with_text(format!(
"No data from graphql server({}) for this query",
self.config.endpoint
))),
}
}
pub async fn query_with_vars<K, T: Serialize>(
&self,
query: &str,
variables: T,
) -> Result<Option<K>, GraphQLError>
where
K: for<'de> Deserialize<'de>,
{
self.query_with_vars_by_endpoint(&self.config.endpoint, query, variables)
.await
}
async fn query_with_vars_by_endpoint<K, T: Serialize>(
&self,
endpoint: impl AsRef<str>,
query: &str,
variables: T,
) -> Result<Option<K>, GraphQLError>
where
K: for<'de> Deserialize<'de>,
{
let mut times = 1;
let mut endpoint = endpoint.as_ref().to_string();
let endpoint_url = Url::from_str(&endpoint).map_err(|e| {
GraphQLError::with_text(format!("Wrong endpoint: {}. {:?}", endpoint, e))
})?;
let schema = endpoint_url.scheme();
let host = endpoint_url
.host()
.ok_or_else(|| GraphQLError::with_text(format!("Wrong endpoint: {}", endpoint)))?;
let client: Client = self.client()?;
let body = RequestBody {
query: query.to_string(),
variables,
};
loop {
if times > 10 {
return Err(GraphQLError::with_text(format!(
"Many redirect location: {}",
endpoint
)));
}
let mut request = client.post(&endpoint).json(&body);
if let Some(headers) = &self.config.headers {
if !headers.is_empty() {
for (name, value) in headers {
request = request.header(name, value);
}
}
}
let raw_response = request.send().await?;
if let Some(location) = raw_response.headers().get(reqwest::header::LOCATION) {
let redirect_url = location.to_str().map_err(|e| {
GraphQLError::with_text(format!(
"Failed to parse response header: Location. {:?}",
e
))
})?;
// if the response location start with http:// or https://
if redirect_url.starts_with("http://") || redirect_url.starts_with("https://") {
times += 1;
endpoint = redirect_url.to_string();
continue;
}
// without schema
endpoint = if redirect_url.starts_with('/') {
format!("{}://{}{}", schema, host, redirect_url)
} else {
format!("{}://{}/{}", schema, host, redirect_url)
};
times += 1;
continue;
}
let status = raw_response.status();
let response_body_text = raw_response
.text()
.await
.map_err(|e| GraphQLError::with_text(format!("Can not get response: {:?}", e)))?;
let json: GraphQLResponse<K> =
serde_json::from_str(&response_body_text).map_err(|e| {
GraphQLError::with_text(format!(
"Failed to parse response: {:?}. The response body is: {}",
e, response_body_text
))
})?;
if !status.is_success() {
return Err(GraphQLError::with_message_and_json(
format!("The response is [{}]", status.as_u16()),
json.errors.unwrap_or_default(),
));
}
// Check if error messages have been received
if json.errors.is_some() {
return Err(GraphQLError::with_json(json.errors.unwrap_or_default()));
}
if json.data.is_none() {
tracing::warn!(
target = "gql-client",
response_text = response_body_text,
"The deserialized data is none, the response",
);
}
return Ok(json.data);
}
}
}