sui_package_dump/
client.rs1use anyhow::{Context, Result, anyhow};
5use cynic::{Operation, QueryBuilder, http::ReqwestExt};
6use reqwest::IntoUrl;
7use serde::{Serialize, de::DeserializeOwned};
8
9pub(crate) struct Client {
10 inner: reqwest::Client,
11 url: reqwest::Url,
12}
13
14impl Client {
15 pub(crate) fn new(url: impl IntoUrl) -> Result<Self> {
17 Ok(Self {
18 inner: reqwest::Client::builder()
19 .user_agent(concat!("sui-package-dump/", env!("CARGO_PKG_VERSION")))
20 .build()
21 .context("Failed to create GraphQL client")?,
22 url: url.into_url().context("Invalid RPC URL")?,
23 })
24 }
25
26 pub(crate) async fn query<Q, V>(&self, query: Operation<Q, V>) -> Result<Q>
27 where
28 V: Serialize,
29 Q: DeserializeOwned + QueryBuilder<V> + 'static,
30 {
31 self.inner
32 .post(self.url.clone())
33 .run_graphql(query)
34 .await
35 .context("Failed to send GraphQL query")?
36 .data
37 .ok_or_else(|| anyhow!("Empty response to query"))
38 }
39}