-
Notifications
You must be signed in to change notification settings - Fork 0
/
calling_web_api.rs
244 lines (229 loc) · 6.2 KB
/
calling_web_api.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use reqwest::{blocking, header, Client, ClientBuilder, Result, StatusCode};
use serde_json::Value;
use shared::{ApiResponse, Dependency, Gist, User};
use std::time::Duration;
/// Make a GET request to the GitHub API
///
/// # Arguments
///
/// * `owner` - The owner of the repository
/// * `repo` - The repository name
///
/// # Returns
///
/// A tuple containing the status code, headers, and a vector of users
///
/// # Example
///
/// ```rust
/// use web_programming::query_github_api;
///
/// #[tokio::main]
/// async fn main() {
/// let owner = "rust-lang-nursery";
/// let repo = "rust-cookbook";
/// let (status, headers, body) = query_github_api(owner, repo).await;
/// assert_eq!(status, 200);
/// assert!(headers.contains_key("date"));
/// assert!(body.len() > 0);
/// }
pub async fn query_github_api(
owner: &str,
repo: &str,
) -> (StatusCode, header::HeaderMap, Vec<User>) {
let request_url = format!("https://api.github.com/repos/{owner}/{repo}/stargazers");
let client = reqwest::Client::new();
let res = client
.get(&request_url)
.header("User-Agent", "Rust Cookbook Client")
.send()
.await
.unwrap();
let status = res.status();
let headers = res.headers().clone();
let body: Vec<User> = res.json().await.unwrap();
(status, headers, body)
}
/// Check if an API exists
///
/// # Arguments
///
/// * `request_url` - The URL to check
///
/// # Returns
///
/// A boolean indicating if the API exists
///
/// # Example
///
/// ```rust
/// use web_programming::check_if_api_exists;
///
/// #[tokio::main]
/// async fn main() {
/// let owner = "fonttools";
/// let repo = "fontbakery";
/// let request_url = format!("https://api.github.com/repos/{owner}/{repo}");
/// let exists = check_if_api_exists(&request_url).await;
/// assert!(exists);
/// }
pub async fn check_if_api_exists(request_url: &str) -> bool {
let timeout = Duration::new(5, 0);
let client = ClientBuilder::new().timeout(timeout).build().unwrap();
let response = client
.head(request_url)
.header("User-Agent", "Rust Cookbook Client")
.send()
.await
.unwrap();
response.status().is_success()
}
/// Create Gist using GitHub API
///
/// # Arguments
///
/// * `gh_user` - GitHub username
/// * `gh_pass` - GitHub API token
/// * `gist_body` - JSON body of the Gist
///
/// # Returns
///
/// A Gist struct
///
/// # Example
///
/// ```ignore
/// use web_programming::create_gist;
///
/// #[tokio::main]
/// async fn main() {
/// let gh_user = env::var("GH_USER").unwrap();
/// let gh_pass = env::var("GH_PASS").unwrap();
///
/// let gist_body = json!({
/// "description": "Hello World Example",
/// "public": true,
/// "files": {
/// "main.py": {
/// "content": r#"print("hello world!")"#
/// }
/// }});
///
/// let gist = create_gist(&gh_user, &gh_pass, gist_body).await;
/// assert_eq!(
/// gist.html_url,
/// format!("https://gist.github.com/{}/{}", gh_user, gist.id)
/// );
/// }
/// ```
pub async fn create_gist(gh_user: &str, gh_pass: &str, gist_body: Value) -> Gist {
let request_url = "https://api.github.com/gists";
let response = Client::new()
.post(request_url)
.header("User-Agent", "Rust Cookbook Client")
.basic_auth(gh_user, Some(gh_pass))
.json(&gist_body)
.send()
.await
.unwrap();
let gist: Gist = response.json().await.unwrap();
gist
}
/// Delete Gist using GitHub API
///
/// # Arguments
///
/// * `gist_id` - The ID of the Gist
/// * `gh_user` - GitHub username
/// * `gh_pass` - GitHub API token
///
/// # Returns
///
/// A status code
///
/// # Example
///
/// ```ignore
/// use web_programming::delete_gist;
///
/// #[tokio::main]
/// async fn main() {
/// let gh_user = env::var("GH_USER").unwrap();
/// let gh_pass = env::var("GH_PASS").unwrap();
///
/// let response_status = delete_gist("gist_id", &gh_user, &gh_pass).await;
/// assert_eq!(response_status, 204);
/// }
/// ```
pub async fn delete_gist(gist_id: &str, gh_user: &str, gh_pass: &str) -> StatusCode {
let request_url = format!("https://api.github.com/gists/{}", gist_id);
let response = Client::new()
.delete(&request_url)
.header("User-Agent", "Rust Cookbook Client")
.basic_auth(gh_user, Some(gh_pass))
.send()
.await
.unwrap();
response.status()
}
/// Get the reverse dependencies of a crate
///
/// # Example
///
/// ```rust
/// use web_programming::ReverseDependencies;
///
/// assert!(ReverseDependencies::of("ring").is_ok());
/// ```
pub struct ReverseDependencies {
pub crate_id: String,
pub dependencies: <Vec<Dependency> as IntoIterator>::IntoIter,
pub client: blocking::Client,
pub page: u32,
pub per_page: u32,
pub total: u32,
}
impl ReverseDependencies {
pub fn of(crate_id: &str) -> Result<Self> {
Ok(ReverseDependencies {
crate_id: crate_id.to_owned(),
dependencies: vec![].into_iter(),
client: blocking::Client::new(),
page: 0,
per_page: 100,
total: 0,
})
}
pub fn try_next(&mut self) -> Result<Option<Dependency>> {
if let Some(dep) = self.dependencies.next() {
return Ok(Some(dep));
}
if self.page > 0 && self.page * self.per_page >= self.total {
return Ok(None);
}
self.page += 1;
let url = format!(
"https://crates.io/api/v1/crates/{}/reverse_dependencies?page={}&per_page={}",
self.crate_id, self.page, self.per_page
);
let response = self
.client
.get(&url)
.header("User-Agent", "Rust Cookbook Client")
.send()?
.json::<ApiResponse>()?;
self.dependencies = response.dependencies.into_iter();
self.total = response.meta.total;
Ok(self.dependencies.next())
}
}
impl Iterator for ReverseDependencies {
type Item = Result<Dependency>;
fn next(&mut self) -> Option<Self::Item> {
match self.try_next() {
Ok(Some(dep)) => Some(Ok(dep)),
Ok(None) => None,
Err(err) => Some(Err(err)),
}
}
}