feat: extract client (#48)

This extracts the client (strategy pattern), this is so that it is will be possible to test the actual querier, without hitting / requiring the dagger-engine running.
This commit is contained in:
Kasper Juul Hermansen 2023-03-19 01:01:24 +01:00 committed by GitHub
parent 384294b390
commit 11d20935c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 289 additions and 232 deletions

61
Cargo.lock generated
View File

@ -36,6 +36,17 @@ dependencies = [
"tokio",
]
[[package]]
name = "async-trait"
version = "0.1.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86ea188f25f0255d8f92797797c97ebf5631fa88178beb1a46fdf5622c9a00e4"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.0",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -241,7 +252,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
dependencies = [
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -287,9 +298,12 @@ dependencies = [
name = "dagger-core"
version = "0.2.8"
dependencies = [
"async-trait",
"base64",
"dirs",
"eyre",
"flate2",
"gql_client",
"graphql-introspection-query",
"graphql_client",
"hex",
@ -310,13 +324,11 @@ dependencies = [
name = "dagger-sdk"
version = "0.2.19"
dependencies = [
"base64",
"dagger-core",
"derive_builder",
"eyre",
"futures",
"genco",
"gql_client",
"pretty_assertions",
"rand",
"serde",
@ -348,7 +360,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim",
"syn",
"syn 1.0.109",
]
[[package]]
@ -359,7 +371,7 @@ checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
dependencies = [
"darling_core",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -380,7 +392,7 @@ dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -390,7 +402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
dependencies = [
"derive_builder_core",
"syn",
"syn 1.0.109",
]
[[package]]
@ -593,7 +605,7 @@ checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -645,7 +657,7 @@ checksum = "c85fd34848b1f708e6344a4af6f7bfc05172ae20ce4b35c8e417efffb4f306aa"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -732,7 +744,7 @@ dependencies = [
"quote",
"serde",
"serde_json",
"syn",
"syn 1.0.109",
]
[[package]]
@ -743,7 +755,7 @@ checksum = "d52fc9cde811f44b15ec0692b31e56a3067f6f431c5ace712f286e47c1dacc98"
dependencies = [
"graphql_client_codegen",
"proc-macro2",
"syn",
"syn 1.0.109",
]
[[package]]
@ -1134,7 +1146,7 @@ checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -1477,7 +1489,7 @@ checksum = "d7e29c4601e36bcec74a223228dce795f4cd3616341a4af93520ca1a837c087d"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -1574,6 +1586,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tar"
version = "0.4.38"
@ -1624,7 +1647,7 @@ checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -1680,7 +1703,7 @@ checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -1734,7 +1757,7 @@ checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -1806,7 +1829,7 @@ checksum = "258bc1c4f8e2e73a977812ab339d503e6feeb92700f6d07a6de4d321522d5c08"
dependencies = [
"lazy_static",
"quote",
"syn",
"syn 1.0.109",
]
[[package]]
@ -1929,7 +1952,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
"wasm-bindgen-shared",
]
@ -1963,7 +1986,7 @@ checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.109",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]

View File

@ -247,7 +247,7 @@ fn render_execution(funcs: &CommonFunctions, field: &FullTypeFields) -> rust::To
return $(output_type) {
proc: self.proc.clone(),
selection: query,
conn: self.conn.clone(),
graphql_client: self.graphql_client.clone(),
}
};
}
@ -273,15 +273,13 @@ fn render_execution(funcs: &CommonFunctions, field: &FullTypeFields) -> rust::To
return vec![$(output_type) {
proc: self.proc.clone(),
selection: query,
conn: self.conn.clone(),
graphql_client: self.graphql_client.clone(),
}]
};
}
let graphql_client = rust::import("crate::client", "graphql_client");
quote! {
query.execute(&$graphql_client(&self.conn)).await
query.execute(self.graphql_client.clone()).await
}
}

View File

@ -12,15 +12,15 @@ use crate::utility::OptionExt;
pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
let selection = rust::import("crate::querybuilder", "Selection");
let child = rust::import("tokio::process", "Child");
let conn = rust::import("dagger_core::connect_params", "ConnectParams");
let graphql_client = rust::import("dagger_core::graphql_client", "DynGraphQLClient");
let arc = rust::import("std::sync", "Arc");
Ok(quote! {
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct $(t.name.pipe(|s| format_name(s))) {
pub proc: $arc<$child>,
pub selection: $selection,
pub conn: $conn,
pub graphql_client: $graphql_client
}
$(t.fields.pipe(|f| render_optional_args(funcs, f)))

View File

@ -17,6 +17,8 @@ tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
base64 = "0.21.0"
gql_client = "1.0.7"
dirs = "4.0.0"
flate2 = { version = "1.0.25", features = ["zlib"] }
graphql-introspection-query = "0.2.0"
@ -28,3 +30,4 @@ reqwest = { version = "0.11.14", features = ["stream", "deflate"] }
sha2 = "0.10.6"
tar = "0.4.38"
tempfile = "3.3.0"
async-trait = "0.1.67"

View File

@ -0,0 +1,52 @@
use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;
use base64::engine::general_purpose;
use base64::Engine;
use gql_client::ClientConfig;
use crate::connect_params::ConnectParams;
#[async_trait]
pub trait GraphQLClient {
async fn query(&self, query: &str) -> eyre::Result<Option<serde_json::Value>>;
}
pub type DynGraphQLClient = Arc<dyn GraphQLClient + Send + Sync>;
#[derive(Debug)]
pub struct DefaultGraphQLClient {
client: gql_client::Client,
}
impl DefaultGraphQLClient {
pub fn new(conn: &ConnectParams) -> Self {
let token = general_purpose::URL_SAFE.encode(format!("{}:", conn.session_token));
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), format!("Basic {}", token));
Self {
client: gql_client::Client::new_with_config(ClientConfig {
endpoint: conn.url(),
timeout: Some(1000),
headers: Some(headers),
proxy: None,
}),
}
}
}
#[async_trait]
impl GraphQLClient for DefaultGraphQLClient {
async fn query(&self, query: &str) -> eyre::Result<Option<serde_json::Value>> {
let res: Option<serde_json::Value> = self
.client
.query(&query)
.await
.map_err(|r| eyre::anyhow!(r.to_string()))?;
return Ok(res);
}
}

View File

@ -7,6 +7,7 @@ pub mod config;
pub mod connect_params;
pub mod downloader;
pub mod engine;
pub mod graphql_client;
pub mod introspection;
pub mod logger;
pub mod schema;

View File

@ -20,9 +20,7 @@ serde_json = { workspace = true }
tracing.workspace = true
tracing-subscriber.workspace = true
base64 = "0.21.0"
futures = "0.3.27"
gql_client = "1.0.7"
derive_builder = "0.12.0"
[dev-dependencies]

View File

@ -1,12 +1,8 @@
use std::collections::HashMap;
use std::sync::Arc;
use base64::engine::general_purpose;
use base64::Engine;
use gql_client::ClientConfig;
use dagger_core::graphql_client::DefaultGraphQLClient;
use dagger_core::config::Config;
use dagger_core::connect_params::ConnectParams;
use dagger_core::engine::Engine as DaggerEngine;
use crate::gen::Query;
@ -25,26 +21,12 @@ pub async fn connect_opts(cfg: Config) -> eyre::Result<DaggerConn> {
let (conn, proc) = DaggerEngine::new().start(&cfg).await?;
Ok(Arc::new(Query {
conn,
proc: Arc::new(proc),
selection: query(),
graphql_client: Arc::new(DefaultGraphQLClient::new(&conn)),
}))
}
pub fn graphql_client(conn: &ConnectParams) -> gql_client::Client {
let token = general_purpose::URL_SAFE.encode(format!("{}:", conn.session_token));
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), format!("Basic {}", token));
gql_client::Client::new_with_config(ClientConfig {
endpoint: conn.url(),
timeout: Some(1000),
headers: Some(headers),
proxy: None,
})
}
// Conn will automatically close on drop of proc
#[cfg(test)]

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
use std::{collections::HashMap, ops::Add, sync::Arc};
use dagger_core::graphql_client::DynGraphQLClient;
use eyre::Context;
use serde::{Deserialize, Serialize};
@ -116,7 +117,7 @@ impl Selection {
Ok(fields.join("{") + &"}".repeat(fields.len() - 1))
}
pub async fn execute<D>(&self, gql_client: &gql_client::Client) -> eyre::Result<D>
pub async fn execute<D>(&self, gql_client: DynGraphQLClient) -> eyre::Result<D>
where
D: for<'de> Deserialize<'de>,
{