use std::collections::HashMap;
use serde_json::{json, Value};
use crate::core::types::{ExchangeResult};
use super::client::HttpClient;
pub struct GraphQlClient {
http: HttpClient,
endpoint: String,
}
impl GraphQlClient {
pub fn new(http: HttpClient, endpoint: &str) -> Self {
Self {
http,
endpoint: endpoint.to_string(),
}
}
pub async fn query(&self, query: &str, variables: Option<Value>) -> ExchangeResult<Value> {
let body = json!({
"query": query,
"variables": variables.unwrap_or(json!({}))
});
let empty_headers = HashMap::new();
self.http.post(&self.endpoint, &body, &empty_headers).await
}
pub async fn query_with_headers(
&self,
query: &str,
variables: Option<Value>,
headers: HashMap<String, String>,
) -> ExchangeResult<Value> {
let body = json!({
"query": query,
"variables": variables.unwrap_or(json!({}))
});
self.http.post(&self.endpoint, &body, &headers).await
}
pub fn http(&self) -> &HttpClient {
&self.http
}
}