-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmod.rs
43 lines (38 loc) · 1.39 KB
/
mod.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
use std::sync::Arc;
use super::config::Configuration;
use failure::{format_err, Error};
use http;
use serde::de::DeserializeOwned;
/// APIClient requires `config::Configuration` includes client to connect with kubernetes cluster.
pub struct APIClient {
configuration: Arc<Configuration>,
}
impl APIClient {
pub fn new(configuration: Configuration) -> Self {
let arc = Arc::new(configuration);
APIClient { configuration: arc }
}
/// Returns kubernetes resources binded `Arnavion/k8s-openapi-codegen` APIs.
pub async fn request<T>(&self, request: http::Request<Vec<u8>>) -> Result<T, Error>
where
T: DeserializeOwned,
{
let (parts, body) = request.into_parts();
let uri_str = format!("{}{}", self.configuration.base_path, parts.uri);
let req = match parts.method {
http::Method::GET => self.configuration.client.get(&uri_str),
http::Method::POST => self.configuration.client.post(&uri_str),
http::Method::DELETE => self.configuration.client.delete(&uri_str),
http::Method::PUT => self.configuration.client.put(&uri_str),
other => {
return Err(Error::from(format_err!("Invalid method: {}", other)));
}
};
req.body(body)
.send()
.await?
.json()
.await
.map_err(Error::from)
}
}