mirror of
https://github.com/kjuulh/dagger-rs.git
synced 2025-08-04 06:43:26 +02:00
can get schema
This commit is contained in:
@@ -1,34 +1,79 @@
|
||||
use graphql_client::reqwest::post_graphql_blocking;
|
||||
use graphql_client::GraphQLQuery;
|
||||
use graphql_introspection_query::introspection_response::IntrospectionResponse;
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::{
|
||||
blocking::{Client, RequestBuilder},
|
||||
header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE},
|
||||
};
|
||||
|
||||
use crate::{config::Config, connect_params::ConnectParams};
|
||||
|
||||
pub struct Session {}
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "src/graphql/introspection_schema.graphql",
|
||||
query_path = "src/graphql/introspection_query.graphql",
|
||||
responsive_path = "Serialize",
|
||||
variable_derive = "Deserialize"
|
||||
)]
|
||||
struct IntrospectionQuery;
|
||||
|
||||
pub struct Session;
|
||||
|
||||
impl Session {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub fn start(&self, cfg: Config, conn: &ConnectParams) -> eyre::Result<Client> {
|
||||
pub fn start(&self, cfg: Config, conn: &ConnectParams) -> eyre::Result<RequestBuilder> {
|
||||
let client = Client::builder()
|
||||
.user_agent("graphql-rust/0.10.0")
|
||||
.default_headers(
|
||||
std::iter::once((
|
||||
reqwest::header::AUTHORIZATION,
|
||||
reqwest::header::HeaderValue::from_str(&format!(
|
||||
"Bearer {}",
|
||||
conn.session_token
|
||||
))
|
||||
.unwrap(),
|
||||
))
|
||||
.collect(),
|
||||
)
|
||||
.connection_verbose(true)
|
||||
//.danger_accept_invalid_certs(true)
|
||||
.build()?;
|
||||
|
||||
let schema = post_graphql_blocking::<IntrospectionResponse, _>(&client, conn.url(), vec![]);
|
||||
let req_builder = client
|
||||
.post(conn.url())
|
||||
.headers(construct_headers())
|
||||
.basic_auth::<String, String>(conn.session_token.to_string(), None);
|
||||
|
||||
Ok(client)
|
||||
Ok(req_builder)
|
||||
}
|
||||
|
||||
pub fn schema(&self, req_builder: RequestBuilder) -> eyre::Result<IntrospectionResponse> {
|
||||
let request_body: graphql_client::QueryBody<()> = graphql_client::QueryBody {
|
||||
variables: (),
|
||||
query: introspection_query::QUERY,
|
||||
operation_name: introspection_query::OPERATION_NAME,
|
||||
};
|
||||
|
||||
let res = req_builder.json(&request_body).send()?;
|
||||
|
||||
if res.status().is_success() {
|
||||
// do nothing
|
||||
} else if res.status().is_server_error() {
|
||||
return Err(eyre::anyhow!("server error!"));
|
||||
} else {
|
||||
let status = res.status();
|
||||
let error_message = match res.text() {
|
||||
Ok(msg) => match serde_json::from_str::<serde_json::Value>(&msg) {
|
||||
Ok(json) => {
|
||||
format!("HTTP {}\n{}", status, serde_json::to_string_pretty(&json)?)
|
||||
}
|
||||
Err(_) => format!("HTTP {}: {}", status, msg),
|
||||
},
|
||||
Err(_) => format!("HTTP {}", status),
|
||||
};
|
||||
return Err(eyre::anyhow!(error_message));
|
||||
}
|
||||
|
||||
let json: IntrospectionResponse = res.json()?;
|
||||
|
||||
Ok(json)
|
||||
}
|
||||
}
|
||||
|
||||
fn construct_headers() -> HeaderMap {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
|
||||
headers
|
||||
}
|
||||
|
Reference in New Issue
Block a user