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
#![crate_name = "sanity"]
extern crate reqwest;
extern crate serde;
extern crate serde_json;
pub mod helpers;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Client;
use serde_json::Value;
#[derive(Debug, Clone)]
pub struct SanityConfig {
project_id: String,
access_token: String,
data_set: String,
url: String,
pub query: Query,
}
pub fn construct_headers(auth_token: &str) -> HeaderMap {
let mut headers = HeaderMap::new();
let auth_header_val = HeaderValue::from_str(auth_token);
if auth_header_val.is_ok() {
headers.insert("Authorization", auth_header_val.unwrap());
} else {
panic!("Invalid header. {}", auth_header_val.unwrap_err());
}
headers
}
pub fn get_url(project_id: &str, data_set: &str) -> String {
format!(
"https://{}.api.sanity.io/v1/data/query/{}",
project_id, data_set
)
}
pub fn create(project_id: &str, data_set: &str, token: &str, use_prod: bool) -> SanityConfig {
SanityConfig {
project_id: project_id.to_string(),
access_token: token.to_string(),
data_set: data_set.to_string(),
url: get_url(project_id, data_set),
query: Query {
base_url: if use_prod {
format!(
"https://{}.apicdn.sanity.io/v1/data/query/{}/",
project_id, data_set
)
} else {
format!(
"https://{}.api.sanity.io/v1/data/query/{}/",
project_id, data_set
)
},
query: None,
},
}
}
#[derive(Debug, Clone)]
pub struct Query {
base_url: String,
pub query: Option<String>,
}
impl Query {
pub fn execute(&self) -> Result<Value, Box<dyn std::error::Error>> {
let url = format!("{}?query={}", self.base_url, self.query.as_ref().unwrap());
let mut res: _ = reqwest::get(&url)?;
let data: Value = serde_json::from_str(&res.text()?)?;
Ok(data)
}
}
impl SanityConfig {
pub fn build_url(&mut self, query: Option<&str>) -> String {
match query {
Some(query) => format!("{}?query={}", self.query.base_url, query),
None => format!(
"{}?query={}",
self.query.base_url,
self.query.query.as_ref().unwrap()
),
}
}
pub fn get(&mut self, query: &str) -> Result<reqwest::Response, reqwest::Error> {
let client = Client::new();
let url = self.build_url(Some(query));
let res = client.get(&url).bearer_auth(&self.access_token).send();
res
}
}