-
Notifications
You must be signed in to change notification settings - Fork 11.4k
/
Copy pathsimple_client.rs
183 lines (165 loc) · 5.49 KB
/
simple_client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::ClientError;
use reqwest::header;
use reqwest::header::HeaderValue;
use reqwest::Response;
use serde_json::Value;
use std::collections::BTreeMap;
use sui_graphql_rpc_headers::LIMITS_HEADER;
use super::response::GraphqlResponse;
#[derive(Clone, Debug)]
pub struct GraphqlQueryVariable {
pub name: String,
pub ty: String,
pub value: Value,
}
#[derive(Clone)]
pub struct SimpleClient {
inner: reqwest::Client,
url: String,
}
impl SimpleClient {
pub fn new<S: Into<String>>(base_url: S) -> Self {
Self {
inner: reqwest::Client::new(),
url: base_url.into(),
}
}
pub async fn execute(
&self,
query: String,
headers: Vec<(header::HeaderName, header::HeaderValue)>,
) -> Result<serde_json::Value, ClientError> {
self.execute_impl(query, vec![], headers, false)
.await?
.json()
.await
.map_err(|e| e.into())
}
pub async fn execute_to_graphql(
&self,
query: String,
get_usage: bool,
variables: Vec<GraphqlQueryVariable>,
mut headers: Vec<(header::HeaderName, header::HeaderValue)>,
) -> Result<GraphqlResponse, ClientError> {
if get_usage {
headers.push((
LIMITS_HEADER.clone().as_str().try_into().unwrap(),
HeaderValue::from_static("true"),
));
}
GraphqlResponse::from_resp(self.execute_impl(query, variables, headers, false).await?).await
}
async fn execute_impl(
&self,
query: String,
variables: Vec<GraphqlQueryVariable>,
headers: Vec<(header::HeaderName, header::HeaderValue)>,
is_mutation: bool,
) -> Result<Response, ClientError> {
let (type_defs, var_vals) = resolve_variables(&variables)?;
let body = if type_defs.is_empty() {
serde_json::json!({
"query": query,
})
} else {
// Make type defs which is a csv is the form of $var_name: $var_type
let type_defs_csv = type_defs
.iter()
.map(|(name, ty)| format!("${}: {}", name, ty))
.collect::<Vec<_>>()
.join(", ");
let query = format!(
"{} ({}) {}",
if is_mutation { "mutation" } else { "query" },
type_defs_csv,
query
);
serde_json::json!({
"query": query,
"variables": var_vals,
})
};
let mut builder = self.inner.post(&self.url).json(&body);
for (key, value) in headers {
builder = builder.header(key, value);
}
builder.send().await.map_err(|e| e.into())
}
pub async fn execute_mutation_to_graphql(
&self,
mutation: String,
variables: Vec<GraphqlQueryVariable>,
) -> Result<GraphqlResponse, ClientError> {
GraphqlResponse::from_resp(self.execute_impl(mutation, variables, vec![], true).await?)
.await
}
/// Send a request to the GraphQL server to check if it is alive.
pub async fn ping(&self) -> Result<(), ClientError> {
self.inner
.get(format!("{}/health", self.url))
.send()
.await?;
Ok(())
}
pub fn url(&self) -> String {
self.url.clone()
}
}
#[allow(clippy::type_complexity)]
pub fn resolve_variables(
vars: &[GraphqlQueryVariable],
) -> Result<(BTreeMap<String, String>, BTreeMap<String, Value>), ClientError> {
let mut type_defs: BTreeMap<String, String> = BTreeMap::new();
let mut var_vals: BTreeMap<String, Value> = BTreeMap::new();
for (idx, GraphqlQueryVariable { name, ty, value }) in vars.iter().enumerate() {
if !is_valid_variable_name(name) {
return Err(ClientError::InvalidVariableName {
var_name: name.to_owned(),
});
}
if name.trim().is_empty() {
return Err(ClientError::InvalidEmptyItem {
item_type: "Variable name".to_owned(),
idx,
});
}
if ty.trim().is_empty() {
return Err(ClientError::InvalidEmptyItem {
item_type: "Variable type".to_owned(),
idx,
});
}
if let Some(var_type_prev) = type_defs.get(name) {
if var_type_prev != ty {
return Err(ClientError::VariableDefinitionConflict {
var_name: name.to_owned(),
var_type_prev: var_type_prev.to_owned(),
var_type_curr: ty.to_owned(),
});
}
if var_vals[name] != *value {
return Err(ClientError::VariableValueConflict {
var_name: name.to_owned(),
var_val_prev: var_vals[name].clone(),
var_val_curr: value.clone(),
});
}
}
type_defs.insert(name.to_owned(), ty.to_owned());
var_vals.insert(name.to_owned(), value.to_owned());
}
Ok((type_defs, var_vals))
}
pub fn is_valid_variable_name(s: &str) -> bool {
let mut cs = s.chars();
let Some(fst) = cs.next() else { return false };
match fst {
'_' => if s.len() > 1 {},
'a'..='z' | 'A'..='Z' => {}
_ => return false,
}
cs.all(|c| matches!(c, '_' | 'a' ..= 'z' | 'A' ..= 'Z' | '0' ..= '9'))
}