clevercloud_sdk/
lib.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
//! # Clever-Cloud Sdk
//!
//! This module provide a client and structures to interact with clever-cloud
//! api.

use std::{fmt::Debug, marker::PhantomData};

pub use oauth10a::client as oauth10a;

use async_trait::async_trait;
use hyper::{Body, Method, Response};
use serde::{de::DeserializeOwned, Serialize};

use crate::oauth10a::{
    connector::{Connect, GaiResolver, HttpConnector, HttpsConnector, HttpsConnectorBuilder},
    ClientError, Credentials, Request, RestClient,
};

pub mod v2;
pub mod v4;

// -----------------------------------------------------------------------------
// Constants

pub const PUBLIC_ENDPOINT: &str = "https://api.clever-cloud.com";

// -----------------------------------------------------------------------------
// Builder structure

#[derive(Clone, Debug)]
pub struct Builder<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    endpoint: Option<String>,
    credentials: Option<Credentials>,
    phantom: PhantomData<C>,
}

impl<C> Default for Builder<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    fn default() -> Self {
        Self {
            endpoint: None,
            credentials: None,
            phantom: Default::default(),
        }
    }
}

impl<C> Builder<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn with_endpoint(mut self, endpoint: String) -> Self {
        self.endpoint = Some(endpoint);
        self
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn with_credentials(mut self, credentials: Credentials) -> Self {
        self.credentials = Some(credentials);
        self
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn build(self, connector: C) -> Client<C> {
        let endpoint = match self.endpoint {
            Some(endpoint) => endpoint,
            None => PUBLIC_ENDPOINT.to_string(),
        };

        Client::<C>::new(connector, endpoint, self.credentials)
    }
}

// -----------------------------------------------------------------------------
// Client structure

#[derive(Clone, Debug)]
pub struct Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    inner: oauth10a::Client<C>,
    endpoint: String,
}

#[async_trait]
impl<C> Request for Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    type Error = ClientError;

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn request<T, U>(
        &self,
        method: &Method,
        endpoint: &str,
        payload: &T,
    ) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        self.inner.request(method, endpoint, payload).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn execute(&self, request: hyper::Request<Body>) -> Result<Response<Body>, Self::Error> {
        self.inner.execute(request).await
    }
}

#[async_trait]
impl<C> RestClient for Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    type Error = ClientError;

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn get<T>(&self, endpoint: &str) -> Result<T, Self::Error>
    where
        T: DeserializeOwned + Debug + Send + Sync,
    {
        self.inner.get(endpoint).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn post<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        self.inner.post(endpoint, payload).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn put<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        self.inner.put(endpoint, payload).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn patch<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        self.inner.patch(endpoint, payload).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn delete(&self, endpoint: &str) -> Result<(), Self::Error> {
        self.inner.delete(endpoint).await
    }
}

impl<C> From<C> for Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn from(connector: C) -> Self {
        Self::builder().build(connector)
    }
}

impl From<Credentials> for Client<HttpsConnector<HttpConnector<GaiResolver>>> {
    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn from(credentials: Credentials) -> Self {
        Self::builder().with_credentials(credentials).build(
            HttpsConnectorBuilder::new()
                .with_webpki_roots()
                .https_or_http()
                .enable_http1()
                .build(),
        )
    }
}

impl Default for Client<HttpsConnector<HttpConnector<GaiResolver>>> {
    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn default() -> Self {
        Self::builder().build(
            HttpsConnectorBuilder::new()
                .with_webpki_roots()
                .https_or_http()
                .enable_http1()
                .build(),
        )
    }
}

impl<C> Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn new(connector: C, endpoint: String, credentials: Option<Credentials>) -> Self {
        Self {
            inner: oauth10a::Client::<C>::new(connector, credentials),
            endpoint,
        }
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn builder() -> Builder<C> {
        Builder::<C>::default()
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn set_endpoint(&mut self, endpoint: String) {
        self.endpoint = endpoint;
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn set_credentials(&mut self, credentials: Option<Credentials>) {
        self.inner.set_credentials(credentials);
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn inner(&self) -> &hyper::Client<C> {
        self.inner.inner()
    }
}