mirror of
https://github.com/kjuulh/dagger-rs.git
synced 2024-11-22 07:12:12 +01:00
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:
parent
384294b390
commit
11d20935c6
61
Cargo.lock
generated
61
Cargo.lock
generated
@ -36,6 +36,17 @@ dependencies = [
|
|||||||
"tokio",
|
"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]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@ -241,7 +252,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
|
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -287,9 +298,12 @@ dependencies = [
|
|||||||
name = "dagger-core"
|
name = "dagger-core"
|
||||||
version = "0.2.8"
|
version = "0.2.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
|
"base64",
|
||||||
"dirs",
|
"dirs",
|
||||||
"eyre",
|
"eyre",
|
||||||
"flate2",
|
"flate2",
|
||||||
|
"gql_client",
|
||||||
"graphql-introspection-query",
|
"graphql-introspection-query",
|
||||||
"graphql_client",
|
"graphql_client",
|
||||||
"hex",
|
"hex",
|
||||||
@ -310,13 +324,11 @@ dependencies = [
|
|||||||
name = "dagger-sdk"
|
name = "dagger-sdk"
|
||||||
version = "0.2.19"
|
version = "0.2.19"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64",
|
|
||||||
"dagger-core",
|
"dagger-core",
|
||||||
"derive_builder",
|
"derive_builder",
|
||||||
"eyre",
|
"eyre",
|
||||||
"futures",
|
"futures",
|
||||||
"genco",
|
"genco",
|
||||||
"gql_client",
|
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
"rand",
|
"rand",
|
||||||
"serde",
|
"serde",
|
||||||
@ -348,7 +360,7 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"strsim",
|
"strsim",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -359,7 +371,7 @@ checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"darling_core",
|
"darling_core",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -380,7 +392,7 @@ dependencies = [
|
|||||||
"darling",
|
"darling",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -390,7 +402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
|
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"derive_builder_core",
|
"derive_builder_core",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -593,7 +605,7 @@ checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -645,7 +657,7 @@ checksum = "c85fd34848b1f708e6344a4af6f7bfc05172ae20ce4b35c8e417efffb4f306aa"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -732,7 +744,7 @@ dependencies = [
|
|||||||
"quote",
|
"quote",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -743,7 +755,7 @@ checksum = "d52fc9cde811f44b15ec0692b31e56a3067f6f431c5ace712f286e47c1dacc98"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"graphql_client_codegen",
|
"graphql_client_codegen",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1134,7 +1146,7 @@ checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1477,7 +1489,7 @@ checksum = "d7e29c4601e36bcec74a223228dce795f4cd3616341a4af93520ca1a837c087d"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1574,6 +1586,17 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"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]]
|
[[package]]
|
||||||
name = "tar"
|
name = "tar"
|
||||||
version = "0.4.38"
|
version = "0.4.38"
|
||||||
@ -1624,7 +1647,7 @@ checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1680,7 +1703,7 @@ checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1734,7 +1757,7 @@ checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1806,7 +1829,7 @@ checksum = "258bc1c4f8e2e73a977812ab339d503e6feeb92700f6d07a6de4d321522d5c08"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1929,7 +1952,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -1963,7 +1986,7 @@ checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 1.0.109",
|
||||||
"wasm-bindgen-backend",
|
"wasm-bindgen-backend",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
@ -247,7 +247,7 @@ fn render_execution(funcs: &CommonFunctions, field: &FullTypeFields) -> rust::To
|
|||||||
return $(output_type) {
|
return $(output_type) {
|
||||||
proc: self.proc.clone(),
|
proc: self.proc.clone(),
|
||||||
selection: query,
|
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) {
|
return vec![$(output_type) {
|
||||||
proc: self.proc.clone(),
|
proc: self.proc.clone(),
|
||||||
selection: query,
|
selection: query,
|
||||||
conn: self.conn.clone(),
|
graphql_client: self.graphql_client.clone(),
|
||||||
}]
|
}]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let graphql_client = rust::import("crate::client", "graphql_client");
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
query.execute(&$graphql_client(&self.conn)).await
|
query.execute(self.graphql_client.clone()).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,15 +12,15 @@ use crate::utility::OptionExt;
|
|||||||
pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
|
pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
|
||||||
let selection = rust::import("crate::querybuilder", "Selection");
|
let selection = rust::import("crate::querybuilder", "Selection");
|
||||||
let child = rust::import("tokio::process", "Child");
|
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");
|
let arc = rust::import("std::sync", "Arc");
|
||||||
|
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct $(t.name.pipe(|s| format_name(s))) {
|
pub struct $(t.name.pipe(|s| format_name(s))) {
|
||||||
pub proc: $arc<$child>,
|
pub proc: $arc<$child>,
|
||||||
pub selection: $selection,
|
pub selection: $selection,
|
||||||
pub conn: $conn,
|
pub graphql_client: $graphql_client
|
||||||
}
|
}
|
||||||
|
|
||||||
$(t.fields.pipe(|f| render_optional_args(funcs, f)))
|
$(t.fields.pipe(|f| render_optional_args(funcs, f)))
|
||||||
|
@ -17,6 +17,8 @@ tokio = { workspace = true }
|
|||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
tracing-subscriber = { workspace = true }
|
tracing-subscriber = { workspace = true }
|
||||||
|
|
||||||
|
base64 = "0.21.0"
|
||||||
|
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 = ["zlib"] }
|
||||||
graphql-introspection-query = "0.2.0"
|
graphql-introspection-query = "0.2.0"
|
||||||
@ -28,3 +30,4 @@ 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"
|
||||||
|
async-trait = "0.1.67"
|
||||||
|
52
crates/dagger-core/src/graphql_client.rs
Normal file
52
crates/dagger-core/src/graphql_client.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ pub mod config;
|
|||||||
pub mod connect_params;
|
pub mod connect_params;
|
||||||
pub mod downloader;
|
pub mod downloader;
|
||||||
pub mod engine;
|
pub mod engine;
|
||||||
|
pub mod graphql_client;
|
||||||
pub mod introspection;
|
pub mod introspection;
|
||||||
pub mod logger;
|
pub mod logger;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
@ -20,9 +20,7 @@ serde_json = { workspace = true }
|
|||||||
tracing.workspace = true
|
tracing.workspace = true
|
||||||
tracing-subscriber.workspace = true
|
tracing-subscriber.workspace = true
|
||||||
|
|
||||||
base64 = "0.21.0"
|
|
||||||
futures = "0.3.27"
|
futures = "0.3.27"
|
||||||
gql_client = "1.0.7"
|
|
||||||
derive_builder = "0.12.0"
|
derive_builder = "0.12.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use base64::engine::general_purpose;
|
use dagger_core::graphql_client::DefaultGraphQLClient;
|
||||||
use base64::Engine;
|
|
||||||
use gql_client::ClientConfig;
|
|
||||||
|
|
||||||
use dagger_core::config::Config;
|
use dagger_core::config::Config;
|
||||||
use dagger_core::connect_params::ConnectParams;
|
|
||||||
use dagger_core::engine::Engine as DaggerEngine;
|
use dagger_core::engine::Engine as DaggerEngine;
|
||||||
|
|
||||||
use crate::gen::Query;
|
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?;
|
let (conn, proc) = DaggerEngine::new().start(&cfg).await?;
|
||||||
|
|
||||||
Ok(Arc::new(Query {
|
Ok(Arc::new(Query {
|
||||||
conn,
|
|
||||||
proc: Arc::new(proc),
|
proc: Arc::new(proc),
|
||||||
selection: query(),
|
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
|
// Conn will automatically close on drop of proc
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
|||||||
use std::{collections::HashMap, ops::Add, sync::Arc};
|
use std::{collections::HashMap, ops::Add, sync::Arc};
|
||||||
|
|
||||||
|
use dagger_core::graphql_client::DynGraphQLClient;
|
||||||
use eyre::Context;
|
use eyre::Context;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@ -116,7 +117,7 @@ impl Selection {
|
|||||||
Ok(fields.join("{") + &"}".repeat(fields.len() - 1))
|
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
|
where
|
||||||
D: for<'de> Deserialize<'de>,
|
D: for<'de> Deserialize<'de>,
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user