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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use crate::{CondSend, IntoParams};
use anyhow::anyhow;
use bytes::Bytes;
use futures::{TryStream, TryStreamExt};
use prost::Message;
use reqwest::header::{HeaderValue, CONTENT_TYPE};
use reqwest::Response;
use reqwest_middleware::ClientWithMiddleware;
use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use crate::error::{Error, Result};
use super::request_builder::RequestBuilder;
/// API client, used to query CDF.
pub struct ApiClient {
api_base_url: String,
app_name: String,
client: ClientWithMiddleware,
api_version: Option<String>,
}
impl ApiClient {
/// Create a new api client.
///
/// # Arguments
///
/// * `api_base_url` - Base URL for CDF. For example `https://api.cognitedata.com`
/// * `app_name` - App name added to the `x-cdp-app` header.
/// * `client` - Underlying reqwest client.
pub fn new(api_base_url: &str, app_name: &str, client: ClientWithMiddleware) -> ApiClient {
ApiClient {
api_base_url: String::from(api_base_url),
app_name: String::from(app_name),
client,
api_version: None,
}
}
/// Create a new api client with a custom API version.
/// This will set the `cdf-version` header to the given value.
///
/// # Arguments
///
/// * `api_version` - API version to use.
pub fn clone_with_api_version(&self, api_version: &str) -> ApiClient {
// We do clone the base URL here, but note that the client is internally
// cloneable, so we do still share it cross resources, which is important.
ApiClient {
api_base_url: self.api_base_url.clone(),
app_name: self.app_name.clone(),
client: self.client.clone(),
api_version: Some(api_version.to_string()),
}
}
/// Perform a get request to the given path, deserializing the result from JSON.
///
/// # Arguments
///
/// * `path` - Request path, without leading slash.
pub async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
RequestBuilder::get(self, format!("{}/{}", self.api_base_url, path))
.accept_json()
.send()
.await
}
/// Perform a get request to the given path, with a query given by `params`,
/// then deserialize the result from JSON.
///
/// # Arguments
///
/// * `path` - Request path, without leading slash.
/// * `params` - Optional object converted to query parameters.
pub async fn get_with_params<T: DeserializeOwned, R: IntoParams>(
&self,
path: &str,
params: Option<R>,
) -> Result<T> {
let mut b =
RequestBuilder::get(self, format!("{}/{}", self.api_base_url, path)).accept_json();
if let Some(params) = params {
b = b.query(¶ms.into_params());
}
b.send().await
}
/// Perform a get request to the given URL, returning a stream.
///
/// # Arguments
///
/// * `url` - Full URL to get stream from.
pub async fn get_stream(
&self,
url: &str,
) -> Result<impl TryStream<Ok = bytes::Bytes, Error = reqwest::Error>> {
let r = RequestBuilder::get(self, url)
.omit_auth_headers()
.accept_raw()
.send()
.await?;
Ok(r.bytes_stream())
}
/// Perform a post request to the given path, serializing `object` to JSON and sending it
/// as the body, then deserialize the response from JSON.
///
/// # Arguments
///
/// * `path` - Request path, without leading slash.
/// * `object` - Object converted to JSON body.
pub async fn post<D, S>(&self, path: &str, object: &S) -> Result<D>
where
D: DeserializeOwned,
S: Serialize,
{
RequestBuilder::post(self, format!("{}/{}", self.api_base_url, path))
.json(object)?
.accept_json()
.send()
.await
}
/// Create a request builder for a `GET` request to `path`.
pub fn get_request(&self, path: &str) -> RequestBuilder<'_, ()> {
RequestBuilder::get(self, format!("{}/{}", self.api_base_url, path))
}
/// Create a request builder for a `POST` request to `path`.
pub fn post_request(&self, path: &str) -> RequestBuilder<'_, ()> {
RequestBuilder::post(self, format!("{}/{}", self.api_base_url, path))
}
/// Create a request builder for a `PUT` request to `path`.
pub fn put_request(&self, path: &str) -> RequestBuilder<'_, ()> {
RequestBuilder::put(self, format!("{}/{}", self.api_base_url, path))
}
/// Create a request builder for a `Delete` request to `path`.
pub fn delete_request(&self, path: &str) -> RequestBuilder<'_, ()> {
RequestBuilder::delete(self, format!("{}/{}", self.api_base_url, path))
}
/// Perform a post request to the given path, with query parameters given by `params`.
/// Deserialize the response from JSON.
///
/// * `path` - Request path, without leading slash.
/// * `object` - Object converted to JSON body.
/// * `params` - Object converted to query parameters.
pub async fn post_with_query<D: DeserializeOwned, S: Serialize, R: IntoParams>(
&self,
path: &str,
object: &S,
params: Option<R>,
) -> Result<D> {
let mut b = self.post_request(path).json(object)?;
if let Some(params) = params {
b = b.query(¶ms.into_params());
}
b.accept_json().send().await
}
/// Perform a post request to the given path, posting `value` as protobuf.
/// Expects JSON as response.
///
/// # Arguments
///
/// * `path` - Request path without leading slash.
/// * `value` - Protobuf value to post.
pub async fn post_protobuf<D: DeserializeOwned + Send + Sync, T: Message>(
&self,
path: &str,
value: &T,
) -> Result<D> {
self.post_request(path)
.protobuf(value)
.accept_json()
.send()
.await
}
/// Perform a post request to the given path, send `object` as JSON in the body,
/// then expect protobuf as response.
///
/// # Arguments
///
/// * `path` - Request path without leading slash.
/// * `object` - Object to convert to JSON and post.
pub async fn post_expect_protobuf<D: Message + Default, S: Serialize>(
&self,
path: &str,
object: &S,
) -> Result<D> {
self.post_request(path)
.json(object)?
.accept_protobuf()
.send()
.await
}
/// Perform a put request with the data in `data`.
///
/// # Arguments
///
/// * `url` - URL to stream blob to.
/// * `mime_type` - What to put in the `X-Upload_Content-Type` header.
/// * `data` - Data to upload.
pub async fn put_blob(&self, url: &str, mime_type: &str, data: impl Into<Bytes>) -> Result<()> {
let bytes: Bytes = data.into();
let mut b = RequestBuilder::put(self, url)
.body(bytes)
.omit_auth_headers()
.accept_nothing();
if !mime_type.is_empty() {
b = b
.header(CONTENT_TYPE, HeaderValue::from_str(mime_type)?)
.header("X-Upload-Content-Type", HeaderValue::from_str(mime_type)?);
}
b.send().await
}
/// Perform a put request, streaming data to `url`.
///
/// # Arguments
///
/// * `url` - URL to stream blob to.
/// * `mime_type` - What to put in the `X-Upload_Content-Type` header.
/// * `stream` - Stream to upload.
/// * `stream_chunked` - If `true`, use chunked streaming to upload the data.
/// * `known_size` - Set the `Content-Length` header to this value.
///
/// If `known_size` is `None` and `stream_chunked` is `true`, the request will be uploaded using
/// special chunked streaming logic. Some backends do not support this.
///
/// If `stream_chunked` is true and `known_size` is Some, this will include a content length header,
/// it is highly recommended to set this whenever possible.
///
/// # Warning
/// If `stream_chunked` is false, this will collect the input stream into a memory, which can
/// be _very_ expensive.
///
/// # Note on WASM
///
/// Note that wasm32-unknown-unknown targets do not support chunked streaming uploads,
/// so effectively `stream_chunked` is always false.
#[cfg_attr(target_arch = "wasm32", allow(unused_variables))]
pub async fn put_stream<S>(
&self,
url: &str,
mime_type: &str,
stream: S,
stream_chunked: bool,
known_size: Option<u64>,
) -> Result<()>
where
S: futures::TryStream + CondSend + 'static,
S::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
bytes::Bytes: From<S::Ok>,
{
let mut b = RequestBuilder::put(self, url)
.omit_auth_headers()
.accept_nothing();
if !mime_type.is_empty() {
b = b
.header(CONTENT_TYPE, HeaderValue::from_str(mime_type)?)
.header("X-Upload-Content-Type", HeaderValue::from_str(mime_type)?);
}
#[cfg(not(target_arch = "wasm32"))]
if stream_chunked {
if let Some(size) = known_size {
b = b.header(
reqwest::header::CONTENT_LENGTH,
HeaderValue::from_str(&size.to_string())?,
);
}
b = b.body(reqwest::Body::wrap_stream(stream));
return b.send().await;
}
let body: Vec<S::Ok> = stream
.try_collect()
.await
.map_err(|e| Error::StreamError(anyhow!(e.into())))?;
let body: Vec<u8> = body
.into_iter()
.flat_map(Into::<bytes::Bytes>::into)
.collect();
b = b.body(body);
b.send().await
}
/// Perform a put request to `path` with `object` as JSON, expecting JSON in return.
///
/// # Arguments
///
/// * `path` - Request path without leading slash.
/// * `object` - Object to send as JSON.
pub async fn put<D, S>(&self, path: &str, object: &S) -> Result<D>
where
D: DeserializeOwned,
S: Serialize,
{
self.put_request(path)
.json(object)?
.accept_json()
.send()
.await
}
/// Perform a delete request to `path`, expecting JSON as response.
///
/// # Arguments
///
/// * `path` - Request path without leading slash.
pub async fn delete<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
self.delete_request(path).accept_json().send().await
}
/// Perform a delete request to `path`, with query parameters given by `params`.
///
/// # Arguments
///
/// * `path` - Request path without leading slash.
/// * `params` - Object converted to query parameters.
pub async fn delete_with_params<T: DeserializeOwned, R: IntoParams>(
&self,
path: &str,
params: Option<R>,
) -> Result<T> {
let mut b = self.delete_request(path).accept_json();
if let Some(params) = params {
b = b.query(¶ms.into_params());
}
b.send().await
}
/// Send an arbitrary HTTP request using the client. This will not parse the response,
/// but will append authentication headers and retry with the same semantics as any
/// normal API call.
///
/// # Arguments
///
/// * `request_builder` - Request to send.
pub async fn send_request(
&self,
mut request_builder: reqwest_middleware::RequestBuilder,
) -> Result<Response> {
request_builder.extensions().insert(self.client.clone());
Ok(request_builder.send().await?)
}
/// Get the inner HTTP client.
pub fn client(&self) -> &ClientWithMiddleware {
&self.client
}
/// Get the configured app name.
pub fn app_name(&self) -> &str {
&self.app_name
}
/// Get the configured API base URL.
pub fn api_base_url(&self) -> &str {
&self.api_base_url
}
/// Get the CDF API version this client will use.
/// Any requests made with the request builder will append a header
/// cdf-version with this value, if it is set.
pub fn api_version(&self) -> Option<&str> {
self.api_version.as_deref()
}
}