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
use either::Either;
use futures::{Stream, StreamExt};
use serde::{de::DeserializeOwned, Serialize};
use std::marker::PhantomData;
use crate::{
api::{DeleteParams, ListParams, Meta, ObjectList, PatchParams, PostParams, Resource, WatchEvent},
client::{APIClient, Status},
Result,
};
#[derive(Clone)]
pub struct Api<K> {
pub(crate) api: Resource,
pub(crate) client: APIClient,
pub(crate) phantom: PhantomData<K>,
}
impl<K> Api<K>
where
K: k8s_openapi::Resource,
{
pub fn all(client: APIClient) -> Self {
let api = Resource::all::<K>();
Self {
api,
client,
phantom: PhantomData,
}
}
pub fn namespaced(client: APIClient, ns: &str) -> Self {
let api = Resource::namespaced::<K>(ns);
Self {
api,
client,
phantom: PhantomData,
}
}
}
impl<K> Api<K>
where
K: Clone + DeserializeOwned + Meta,
{
pub async fn get(&self, name: &str) -> Result<K> {
let req = self.api.get(name)?;
self.client.request::<K>(req).await
}
pub async fn create(&self, pp: &PostParams, data: &K) -> Result<K>
where
K: Serialize,
{
let bytes = serde_json::to_vec(&data)?;
let req = self.api.create(&pp, bytes)?;
self.client.request::<K>(req).await
}
pub async fn delete(&self, name: &str, dp: &DeleteParams) -> Result<Either<K, Status>> {
let req = self.api.delete(name, &dp)?;
self.client.request_status::<K>(req).await
}
pub async fn list(&self, lp: &ListParams) -> Result<ObjectList<K>> {
let req = self.api.list(&lp)?;
self.client.request::<ObjectList<K>>(req).await
}
pub async fn delete_collection(&self, lp: &ListParams) -> Result<Either<ObjectList<K>, Status>> {
let req = self.api.delete_collection(&lp)?;
self.client.request_status::<ObjectList<K>>(req).await
}
pub async fn patch(&self, name: &str, pp: &PatchParams, patch: Vec<u8>) -> Result<K> {
let req = self.api.patch(name, &pp, patch)?;
self.client.request::<K>(req).await
}
pub async fn replace(&self, name: &str, pp: &PostParams, data: &K) -> Result<K>
where
K: Serialize,
{
let bytes = serde_json::to_vec(&data)?;
let req = self.api.replace(name, &pp, bytes)?;
self.client.request::<K>(req).await
}
pub async fn watch(&self, lp: &ListParams, version: &str) -> Result<impl Stream<Item = WatchEvent<K>>> {
let req = self.api.watch(&lp, &version)?;
self.client
.request_events::<WatchEvent<K>>(req)
.await
.map(|stream| stream.filter_map(|e| async move { e.ok() }))
}
}