mirror of
https://github.com/kjuulh/dagger-rs.git
synced 2025-07-25 19:09:22 +02: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:
@@ -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"
|
||||
|
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 downloader;
|
||||
pub mod engine;
|
||||
pub mod graphql_client;
|
||||
pub mod introspection;
|
||||
pub mod logger;
|
||||
pub mod schema;
|
||||
|
Reference in New Issue
Block a user