1#![allow(
3 clippy::all
4)]
5use serde::Deserialize;
16
17use roctokit::adapters::{AdapterError, Client, GitHubRequest, GitHubResponseExt};
18use crate::models::*;
19
20use super::PerPage;
21
22use std::collections::HashMap;
23use serde_json::value::Value;
24
25pub struct Meta<'api, C: Client> where AdapterError: From<<C as Client>::Err> {
26 client: &'api C
27}
28
29pub fn new<C: Client>(client: &C) -> Meta<C> where AdapterError: From<<C as Client>::Err> {
30 Meta { client }
31}
32
33#[derive(Debug, thiserror::Error)]
35pub enum MetaGetError {
36 #[error("Not modified")]
37 Status304,
38 #[error("Status code: {}", code)]
39 Generic { code: u16 },
40}
41
42impl From<MetaGetError> for AdapterError {
43 fn from(err: MetaGetError) -> Self {
44 let (description, status_code) = match err {
45 MetaGetError::Status304 => (String::from("Not modified"), 304),
46 MetaGetError::Generic { code } => (String::from("Generic"), code)
47 };
48
49 Self::Endpoint {
50 description,
51 status_code,
52 source: Some(Box::new(err))
53 }
54 }
55}
56
57#[derive(Debug, thiserror::Error)]
59pub enum MetaGetAllVersionsError {
60 #[error("Resource not found")]
61 Status404(BasicError),
62 #[error("Status code: {}", code)]
63 Generic { code: u16 },
64}
65
66impl From<MetaGetAllVersionsError> for AdapterError {
67 fn from(err: MetaGetAllVersionsError) -> Self {
68 let (description, status_code) = match err {
69 MetaGetAllVersionsError::Status404(_) => (String::from("Resource not found"), 404),
70 MetaGetAllVersionsError::Generic { code } => (String::from("Generic"), code)
71 };
72
73 Self::Endpoint {
74 description,
75 status_code,
76 source: Some(Box::new(err))
77 }
78 }
79}
80
81#[derive(Debug, thiserror::Error)]
83pub enum MetaGetOctocatError {
84 #[error("Status code: {}", code)]
85 Generic { code: u16 },
86}
87
88impl From<MetaGetOctocatError> for AdapterError {
89 fn from(err: MetaGetOctocatError) -> Self {
90 let (description, status_code) = match err {
91 MetaGetOctocatError::Generic { code } => (String::from("Generic"), code)
92 };
93
94 Self::Endpoint {
95 description,
96 status_code,
97 source: Some(Box::new(err))
98 }
99 }
100}
101
102#[derive(Debug, thiserror::Error)]
104pub enum MetaGetZenError {
105 #[error("Status code: {}", code)]
106 Generic { code: u16 },
107}
108
109impl From<MetaGetZenError> for AdapterError {
110 fn from(err: MetaGetZenError) -> Self {
111 let (description, status_code) = match err {
112 MetaGetZenError::Generic { code } => (String::from("Generic"), code)
113 };
114
115 Self::Endpoint {
116 description,
117 status_code,
118 source: Some(Box::new(err))
119 }
120 }
121}
122
123#[derive(Debug, thiserror::Error)]
125pub enum MetaRootError {
126 #[error("Status code: {}", code)]
127 Generic { code: u16 },
128}
129
130impl From<MetaRootError> for AdapterError {
131 fn from(err: MetaRootError) -> Self {
132 let (description, status_code) = match err {
133 MetaRootError::Generic { code } => (String::from("Generic"), code)
134 };
135
136 Self::Endpoint {
137 description,
138 status_code,
139 source: Some(Box::new(err))
140 }
141 }
142}
143
144
145#[derive(Default, Serialize)]
147pub struct MetaGetOctocatParams<'req> {
148 s: Option<&'req str>
150}
151
152impl<'req> MetaGetOctocatParams<'req> {
153 pub fn new() -> Self {
154 Self::default()
155 }
156
157 pub fn s(self, s: &'req str) -> Self {
159 Self {
160 s: Some(s),
161 }
162 }
163}
164
165
166impl<'api, C: Client> Meta<'api, C> where AdapterError: From<<C as Client>::Err> {
167 pub async fn get_async(&self) -> Result<ApiOverview, AdapterError> {
184
185 let request_uri = format!("{}/meta", super::GITHUB_BASE_API_URL);
186
187
188 let req = GitHubRequest {
189 uri: request_uri,
190 body: None::<C::Body>,
191 method: "GET",
192 headers: vec![]
193 };
194
195 let request = self.client.build(req)?;
196
197 let github_response = self.client.fetch_async(request).await?;
200
201 if github_response.is_success() {
204 Ok(github_response.to_json_async().await?)
205 } else {
206 match github_response.status_code() {
207 304 => Err(MetaGetError::Status304.into()),
208 code => Err(MetaGetError::Generic { code }.into()),
209 }
210 }
211 }
212
213 #[cfg(not(target_arch = "wasm32"))]
230 pub fn get(&self) -> Result<ApiOverview, AdapterError> {
231
232 let request_uri = format!("{}/meta", super::GITHUB_BASE_API_URL);
233
234
235 let req = GitHubRequest {
236 uri: request_uri,
237 body: None,
238 method: "GET",
239 headers: vec![]
240 };
241
242 let request = self.client.build(req)?;
243
244 let github_response = self.client.fetch(request)?;
247
248 if github_response.is_success() {
251 Ok(github_response.to_json()?)
252 } else {
253 match github_response.status_code() {
254 304 => Err(MetaGetError::Status304.into()),
255 code => Err(MetaGetError::Generic { code }.into()),
256 }
257 }
258 }
259
260 pub async fn get_all_versions_async(&self) -> Result<Vec<chrono::DateTime<chrono::Utc>>, AdapterError> {
270
271 let request_uri = format!("{}/versions", super::GITHUB_BASE_API_URL);
272
273
274 let req = GitHubRequest {
275 uri: request_uri,
276 body: None::<C::Body>,
277 method: "GET",
278 headers: vec![]
279 };
280
281 let request = self.client.build(req)?;
282
283 let github_response = self.client.fetch_async(request).await?;
286
287 if github_response.is_success() {
290 Ok(github_response.to_json_async().await?)
291 } else {
292 match github_response.status_code() {
293 404 => Err(MetaGetAllVersionsError::Status404(github_response.to_json_async().await?).into()),
294 code => Err(MetaGetAllVersionsError::Generic { code }.into()),
295 }
296 }
297 }
298
299 #[cfg(not(target_arch = "wasm32"))]
309 pub fn get_all_versions(&self) -> Result<Vec<chrono::DateTime<chrono::Utc>>, AdapterError> {
310
311 let request_uri = format!("{}/versions", super::GITHUB_BASE_API_URL);
312
313
314 let req = GitHubRequest {
315 uri: request_uri,
316 body: None,
317 method: "GET",
318 headers: vec![]
319 };
320
321 let request = self.client.build(req)?;
322
323 let github_response = self.client.fetch(request)?;
326
327 if github_response.is_success() {
330 Ok(github_response.to_json()?)
331 } else {
332 match github_response.status_code() {
333 404 => Err(MetaGetAllVersionsError::Status404(github_response.to_json()?).into()),
334 code => Err(MetaGetAllVersionsError::Generic { code }.into()),
335 }
336 }
337 }
338
339 pub async fn get_octocat_async(&self, query_params: Option<impl Into<MetaGetOctocatParams<'api>>>) -> Result<String, AdapterError> {
349
350 let mut request_uri = format!("{}/octocat", super::GITHUB_BASE_API_URL);
351
352 if let Some(params) = query_params {
353 request_uri.push_str("?");
354 request_uri.push_str(&serde_urlencoded::to_string(params.into())?);
355 }
356
357 let req = GitHubRequest {
358 uri: request_uri,
359 body: None::<C::Body>,
360 method: "GET",
361 headers: vec![]
362 };
363
364 let request = self.client.build(req)?;
365
366 let github_response = self.client.fetch_async(request).await?;
369
370 if github_response.is_success() {
373 Ok(github_response.to_json_async().await?)
374 } else {
375 match github_response.status_code() {
376 code => Err(MetaGetOctocatError::Generic { code }.into()),
377 }
378 }
379 }
380
381 #[cfg(not(target_arch = "wasm32"))]
391 pub fn get_octocat(&self, query_params: Option<impl Into<MetaGetOctocatParams<'api>>>) -> Result<String, AdapterError> {
392
393 let mut request_uri = format!("{}/octocat", super::GITHUB_BASE_API_URL);
394
395 if let Some(params) = query_params {
396 request_uri.push_str("?");
397 let qp: MetaGetOctocatParams = params.into();
398 request_uri.push_str(&serde_urlencoded::to_string(qp)?);
399 }
400
401 let req = GitHubRequest {
402 uri: request_uri,
403 body: None,
404 method: "GET",
405 headers: vec![]
406 };
407
408 let request = self.client.build(req)?;
409
410 let github_response = self.client.fetch(request)?;
413
414 if github_response.is_success() {
417 Ok(github_response.to_json()?)
418 } else {
419 match github_response.status_code() {
420 code => Err(MetaGetOctocatError::Generic { code }.into()),
421 }
422 }
423 }
424
425 pub async fn get_zen_async(&self) -> Result<String, AdapterError> {
435
436 let request_uri = format!("{}/zen", super::GITHUB_BASE_API_URL);
437
438
439 let req = GitHubRequest {
440 uri: request_uri,
441 body: None::<C::Body>,
442 method: "GET",
443 headers: vec![]
444 };
445
446 let request = self.client.build(req)?;
447
448 let github_response = self.client.fetch_async(request).await?;
451
452 if github_response.is_success() {
455 Ok(github_response.to_json_async().await?)
456 } else {
457 match github_response.status_code() {
458 code => Err(MetaGetZenError::Generic { code }.into()),
459 }
460 }
461 }
462
463 #[cfg(not(target_arch = "wasm32"))]
473 pub fn get_zen(&self) -> Result<String, AdapterError> {
474
475 let request_uri = format!("{}/zen", super::GITHUB_BASE_API_URL);
476
477
478 let req = GitHubRequest {
479 uri: request_uri,
480 body: None,
481 method: "GET",
482 headers: vec![]
483 };
484
485 let request = self.client.build(req)?;
486
487 let github_response = self.client.fetch(request)?;
490
491 if github_response.is_success() {
494 Ok(github_response.to_json()?)
495 } else {
496 match github_response.status_code() {
497 code => Err(MetaGetZenError::Generic { code }.into()),
498 }
499 }
500 }
501
502 pub async fn root_async(&self) -> Result<Root, AdapterError> {
512
513 let request_uri = format!("{}/", super::GITHUB_BASE_API_URL);
514
515
516 let req = GitHubRequest {
517 uri: request_uri,
518 body: None::<C::Body>,
519 method: "GET",
520 headers: vec![]
521 };
522
523 let request = self.client.build(req)?;
524
525 let github_response = self.client.fetch_async(request).await?;
528
529 if github_response.is_success() {
532 Ok(github_response.to_json_async().await?)
533 } else {
534 match github_response.status_code() {
535 code => Err(MetaRootError::Generic { code }.into()),
536 }
537 }
538 }
539
540 #[cfg(not(target_arch = "wasm32"))]
550 pub fn root(&self) -> Result<Root, AdapterError> {
551
552 let request_uri = format!("{}/", super::GITHUB_BASE_API_URL);
553
554
555 let req = GitHubRequest {
556 uri: request_uri,
557 body: None,
558 method: "GET",
559 headers: vec![]
560 };
561
562 let request = self.client.build(req)?;
563
564 let github_response = self.client.fetch(request)?;
567
568 if github_response.is_success() {
571 Ok(github_response.to_json()?)
572 } else {
573 match github_response.status_code() {
574 code => Err(MetaRootError::Generic { code }.into()),
575 }
576 }
577 }
578
579}