From 11d20935c697e28caaa671e8da0e70a99d4310fc Mon Sep 17 00:00:00 2001 From: Kasper Juul Hermansen Date: Sun, 19 Mar 2023 01:01:24 +0100 Subject: [PATCH] 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. --- Cargo.lock | 61 ++- crates/dagger-codegen/src/rust/functions.rs | 8 +- .../src/rust/templates/object_tmpl.rs | 6 +- crates/dagger-core/Cargo.toml | 3 + crates/dagger-core/src/graphql_client.rs | 52 +++ crates/dagger-core/src/lib.rs | 1 + crates/dagger-sdk/Cargo.toml | 2 - crates/dagger-sdk/src/client.rs | 22 +- crates/dagger-sdk/src/gen.rs | 363 +++++++++--------- crates/dagger-sdk/src/querybuilder.rs | 3 +- 10 files changed, 289 insertions(+), 232 deletions(-) create mode 100644 crates/dagger-core/src/graphql_client.rs diff --git a/Cargo.lock b/Cargo.lock index 29ab0a5..95837c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/crates/dagger-codegen/src/rust/functions.rs b/crates/dagger-codegen/src/rust/functions.rs index 4726b10..7ce020f 100644 --- a/crates/dagger-codegen/src/rust/functions.rs +++ b/crates/dagger-codegen/src/rust/functions.rs @@ -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 } } diff --git a/crates/dagger-codegen/src/rust/templates/object_tmpl.rs b/crates/dagger-codegen/src/rust/templates/object_tmpl.rs index 96ef908..60b4c01 100644 --- a/crates/dagger-codegen/src/rust/templates/object_tmpl.rs +++ b/crates/dagger-codegen/src/rust/templates/object_tmpl.rs @@ -12,15 +12,15 @@ use crate::utility::OptionExt; pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result { 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))) diff --git a/crates/dagger-core/Cargo.toml b/crates/dagger-core/Cargo.toml index dd22c7f..cde1e4a 100644 --- a/crates/dagger-core/Cargo.toml +++ b/crates/dagger-core/Cargo.toml @@ -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" diff --git a/crates/dagger-core/src/graphql_client.rs b/crates/dagger-core/src/graphql_client.rs new file mode 100644 index 0000000..77548a2 --- /dev/null +++ b/crates/dagger-core/src/graphql_client.rs @@ -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>; +} + +pub type DynGraphQLClient = Arc; + +#[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> { + let res: Option = self + .client + .query(&query) + .await + .map_err(|r| eyre::anyhow!(r.to_string()))?; + + return Ok(res); + } +} diff --git a/crates/dagger-core/src/lib.rs b/crates/dagger-core/src/lib.rs index dc6ffee..9363c24 100644 --- a/crates/dagger-core/src/lib.rs +++ b/crates/dagger-core/src/lib.rs @@ -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; diff --git a/crates/dagger-sdk/Cargo.toml b/crates/dagger-sdk/Cargo.toml index 2954751..67eb00e 100644 --- a/crates/dagger-sdk/Cargo.toml +++ b/crates/dagger-sdk/Cargo.toml @@ -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] diff --git a/crates/dagger-sdk/src/client.rs b/crates/dagger-sdk/src/client.rs index f58dacd..913f8f1 100644 --- a/crates/dagger-sdk/src/client.rs +++ b/crates/dagger-sdk/src/client.rs @@ -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 { 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)] diff --git a/crates/dagger-sdk/src/gen.rs b/crates/dagger-sdk/src/gen.rs index 4122415..22c9ff4 100644 --- a/crates/dagger-sdk/src/gen.rs +++ b/crates/dagger-sdk/src/gen.rs @@ -1,6 +1,5 @@ -use crate::client::graphql_client; use crate::querybuilder::Selection; -use dagger_core::connect_params::ConnectParams; +use dagger_core::graphql_client::DynGraphQLClient; use derive_builder::Builder; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -114,25 +113,25 @@ pub struct PipelineLabel { pub name: String, pub value: String, } -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct CacheVolume { pub proc: Arc, pub selection: Selection, - pub conn: ConnectParams, + pub graphql_client: DynGraphQLClient, } impl CacheVolume { pub async fn id(&self) -> eyre::Result { let query = self.selection.select("id"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } } -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct Container { pub proc: Arc, pub selection: Selection, - pub conn: ConnectParams, + pub graphql_client: DynGraphQLClient, } #[derive(Builder, Debug, PartialEq)] @@ -295,7 +294,7 @@ impl Container { return Container { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } @@ -322,14 +321,14 @@ impl Container { return Container { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } /// Retrieves default arguments for future commands. pub async fn default_args(&self) -> eyre::Result> { let query = self.selection.select("defaultArgs"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves a directory at the given path. /// Mounts are included. @@ -345,7 +344,7 @@ impl Container { return Directory { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } /// Retrieves an endpoint that clients can use to reach this container. @@ -359,7 +358,7 @@ impl Container { pub async fn endpoint(&self) -> eyre::Result { let query = self.selection.select("endpoint"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves an endpoint that clients can use to reach this container. @@ -380,13 +379,13 @@ impl Container { query = query.arg("scheme", scheme); } - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves entrypoint to be prepended to the arguments of all commands. pub async fn entrypoint(&self) -> eyre::Result> { let query = self.selection.select("entrypoint"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves the value of the specified environment variable. /// @@ -398,7 +397,7 @@ impl Container { query = query.arg("name", name.into()); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves the list of environment variables passed to commands. pub fn env_variables(&self) -> Vec { @@ -407,7 +406,7 @@ impl Container { return vec![EnvVariable { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }]; } /// Retrieves this container after executing the specified command inside it. @@ -421,7 +420,7 @@ impl Container { return Container { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } @@ -455,7 +454,7 @@ impl Container { return Container { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } /// Exit code of the last executed command. Zero means success. @@ -463,7 +462,7 @@ impl Container { pub async fn exit_code(&self) -> eyre::Result { let query = self.selection.select("exitCode"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Writes the container as an OCI tarball to the destination file path on the host for the specified platform variants. /// Return true on success. @@ -479,7 +478,7 @@ impl Container { query = query.arg("path", path.into()); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Writes the container as an OCI tarball to the destination file path on the host for the specified platform variants. @@ -503,7 +502,7 @@ impl Container { query = query.arg("platformVariants", platform_variants); } - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves the list of exposed ports. /// Currently experimental; set _EXPERIMENTAL_DAGGER_SERVICES_DNS=0 to disable. @@ -513,7 +512,7 @@ impl Container { return vec![Port { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }]; } /// Retrieves a file at the given path. @@ -530,7 +529,7 @@ impl Container { return File { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } /// Initializes this container from a pulled base image. @@ -548,7 +547,7 @@ impl Container { return Container { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } /// Retrieves this container's root filesystem. Mounts are not included. @@ -558,7 +557,7 @@ impl Container { return Directory { proc: self.proc.clone(), selection: query, - conn: self.conn.clone(), + graphql_client: self.graphql_client.clone(), }; } /// Retrieves a hostname which can be used by clients to reach this container. @@ -566,19 +565,19 @@ impl Container { pub async fn hostname(&self) -> eyre::Result { let query = self.selection.select("hostname"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// A unique identifier for this container. pub async fn id(&self) -> eyre::Result { let query = self.selection.select("id"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// The unique image reference which can only be retrieved immediately after the 'Container.From' call. pub async fn image_ref(&self) -> eyre::Result { let query = self.selection.select("imageRef"); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves the value of the specified label. pub async fn label(&self, name: impl Into) -> eyre::Result { @@ -586,7 +585,7 @@ impl Container { query = query.arg("name", name.into()); - query.execute(&graphql_client(&self.conn)).await + query.execute(self.graphql_client.clone()).await } /// Retrieves the list of labels passed to container. pub fn labels(&self) -> Vec