Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

feat: extract client #48

Merged
merged 2 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 42 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions crates/dagger-codegen/src/rust/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
};
}
Expand All @@ -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
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/dagger-codegen/src/rust/templates/object_tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
3 changes: 3 additions & 0 deletions crates/dagger-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
52 changes: 52 additions & 0 deletions crates/dagger-core/src/graphql_client.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions crates/dagger-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions crates/dagger-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 2 additions & 20 deletions crates/dagger-sdk/src/client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)]
Expand Down
Loading