seaplane 0.8.0

The Seaplane Rust SDK
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! The `/restrict` endpoint APIs which allows working with [`Restriction`]s
pub mod models;

use std::str::FromStr;

use reqwest::{
    header::{self, CONTENT_TYPE},
    Url,
};

pub use self::models::*;
use crate::{
    api::{
        map_api_error,
        restrict::{error::RestrictError, RESTRICT_API_URL},
        shared::v1::RangeQueryContext,
        ApiRequest, RequestBuilder,
    },
    error::Result,
};

static RESTRICT_API_BASE_PATH: &str = "v1/restrict/";

/// A builder struct for creating a [`RestrictRequest`] which will then be used for making a
/// request against the `/restrict` APIs
#[derive(Debug)]
pub struct RestrictRequestBuilder {
    builder: RequestBuilder<RequestTarget>,
}

impl From<RequestBuilder<RequestTarget>> for RestrictRequestBuilder {
    fn from(builder: RequestBuilder<RequestTarget>) -> Self { Self { builder } }
}

impl Default for RestrictRequestBuilder {
    fn default() -> Self { Self::new() }
}

impl RestrictRequestBuilder {
    /// Create a new RestrictRequestBuilder
    pub fn new() -> Self { RequestBuilder::new(RESTRICT_API_URL, RESTRICT_API_BASE_PATH).into() }

    /// Build a RestrictRequest from the given parameters
    pub fn build(self) -> Result<RestrictRequest> { Ok(self.builder.build()?.into()) }

    /// Set the token used in Bearer Authorization
    ///
    /// **NOTE:** This is required for all endpoints
    #[must_use]
    pub fn token<U: Into<String>>(self, token: U) -> Self { self.builder.token(token).into() }

    /// Allow non-HTTPS endpoints for this request (default: `false`)
    #[cfg(any(feature = "allow_insecure_urls", feature = "danger_zone"))]
    #[cfg_attr(docsrs, doc(cfg(any(feature = "allow_insecure_urls", feature = "danger_zone"))))]
    pub fn allow_http(self, yes: bool) -> Self { self.builder.allow_http(yes).into() }

    /// Allow invalid TLS certificates (default: `false`)
    #[cfg(any(feature = "allow_invalid_certs", feature = "danger_zone"))]
    #[cfg_attr(docsrs, doc(cfg(any(feature = "allow_invalid_certs", feature = "danger_zone"))))]
    pub fn allow_invalid_certs(self, yes: bool) -> Self {
        self.builder.allow_invalid_certs(yes).into()
    }

    // Used in testing and development to manually set the URL
    #[doc(hidden)]
    pub fn base_url<U: AsRef<str>>(self, url: U) -> Self { self.builder.base_url(url).into() }

    /// The restricted directory, encoded in url-safe base64.
    ///
    /// **NOTE:** This is not required for all endpoints
    #[must_use]
    pub fn single_restriction<S: Into<String>>(mut self, api: S, directory: S) -> Self {
        self.builder.target = Some(RequestTarget::Single {
            api: api.into(),
            directory: RestrictedDirectory::from_encoded(directory.into()),
        });
        self
    }

    /// The context with which to perform a range query within an API
    ///
    /// **NOTE:** This is not required for all endpoints
    #[must_use]
    pub fn api_range<S: Into<String>>(
        mut self,
        api: S,
        context: RangeQueryContext<RestrictedDirectory>,
    ) -> Self {
        self.builder.target = Some(RequestTarget::ApiRange { api: api.into(), context });
        self
    }

    /// The context with which to perform a range query across all APIs
    ///
    /// **NOTE:** This is not required for all endpoints
    #[must_use]
    pub fn all_range<S: Into<String>>(
        mut self,
        from_api: Option<S>,
        context: RangeQueryContext<RestrictedDirectory>,
    ) -> Self {
        self.builder.target =
            Some(RequestTarget::AllRange { from_api: from_api.map(|a| a.into()), context });
        self
    }
}

/// For making requests against the `/request` APIs.
#[derive(Debug)]
pub struct RestrictRequest {
    request: ApiRequest<RequestTarget>,
}

impl From<ApiRequest<RequestTarget>> for RestrictRequest {
    fn from(request: ApiRequest<RequestTarget>) -> Self { Self { request } }
}

impl RestrictRequest {
    /// Create a new request builder
    pub fn builder() -> RestrictRequestBuilder { RestrictRequestBuilder::new() }

    // Internal method creating the URL for single key endpoints
    fn single_url(&self) -> Result<Url> {
        match &self.request.target {
            Some(RequestTarget::Single { api, directory }) => Ok(self
                .request
                .endpoint_url
                .join(&format!("{}/base64:{}/", api, directory.encoded()))?),
            _ => Err(RestrictError::IncorrectRestrictRequestTarget)?,
        }
    }

    // Internal method creating the URL for all range endpoints
    fn range_url(&self) -> Result<Url> {
        match &self.request.target {
            Some(RequestTarget::AllRange { from_api, context }) => {
                let mut url = self.request.endpoint_url.clone();

                match (from_api, context.from()) {
                    (None, None) => Ok(url),
                    (Some(api), Some(from)) => {
                        url.set_query(Some(&format!(
                            "from_api={}&from=base64:{}",
                            api,
                            from.encoded()
                        )));
                        Ok(url)
                    }
                    (..) => Err(RestrictError::IncorrectRestrictRequestTarget)?,
                }
            }

            Some(RequestTarget::ApiRange { api, context }) => {
                let api = Api::from_str(api)
                    .map_err(|_| RestrictError::IncorrectRestrictRequestTarget)?;

                let mut url = self.request.endpoint_url.join(&format!("{api}/"))?;

                match context.from() {
                    None => Ok(url),
                    Some(from) => {
                        url.set_query(Some(&format!("from=base64:{}", from.encoded())));
                        Ok(url)
                    }
                }
            }
            _ => Err(RestrictError::IncorrectRestrictRequestTarget)?,
        }
    }

    /// Returns restriction details for an API-directory combination
    ///
    /// **NOTE:** This endpoint requires the `RequestTarget` be a `Single`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use seaplane::api::restrict::v1::{RestrictRequest, RestrictRequestBuilder};
    ///
    /// let req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .single_restriction("config", "bW9ieQo")
    ///     .build()
    ///     .unwrap();
    ///
    /// let resp = req.get_restriction().unwrap();
    /// dbg!(resp);
    /// ```
    pub fn get_restriction(&self) -> Result<Restriction> {
        let url = self.single_url()?;
        let resp = self
            .request
            .client
            .get(url)
            .bearer_auth(&self.request.token)
            .send()?;
        map_api_error(resp)?
            .json::<Restriction>()
            .map_err(Into::into)
    }

    /// Returns a single page of restrictions, starting from `from_api` and
    /// `from_key` combination.
    ///
    /// If more pages are desired, perform another range request using the
    /// `next_api` and `next_key` values from the first request as the
    /// `from_api` and `from_key` values of the following request, or use
    /// `get_all_pages`.
    ///
    /// **NOTE:** This endpoint requires the `RequestTarget` be an `ApiRange` or
    /// `AllRange`.
    ///
    /// # Examples
    ///
    /// ## Paging through single API restrictions
    ///
    /// ```no_run
    /// use seaplane::api::{
    ///     restrict::v1::{RestrictRequest, RestrictRequestBuilder},
    ///     shared::v1::RangeQueryContext,
    /// };
    ///
    /// let context = RangeQueryContext::new();
    /// let req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .api_range("config", context)
    ///     .build()
    ///     .unwrap();
    ///
    /// let resp = req.get_page().unwrap();
    /// dbg!(&resp);
    ///
    /// // To get next page:
    ///
    /// if let Some(next_key) = resp.next_key {
    ///     let mut context = RangeQueryContext::new();
    ///     context.set_from(next_key);
    ///
    ///     let req = RestrictRequestBuilder::new()
    ///         .token("abc123_token")
    ///         .api_range("config", context)
    ///         .build()
    ///         .unwrap();
    ///
    ///     let next_page_resp = req.get_page().unwrap();
    ///     dbg!(next_page_resp);
    /// }
    /// ```
    ///
    /// ## Paging through all restrictions
    ///
    /// ```no_run
    /// use seaplane::api::{
    ///     restrict::v1::{RestrictRequest, RestrictRequestBuilder},
    ///     shared::v1::RangeQueryContext,
    /// };
    ///
    /// let context = RangeQueryContext::new();
    /// let req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .all_range::<String>(None, context)
    ///     .build()
    ///     .unwrap();
    ///
    /// let resp = req.get_page().unwrap();
    /// dbg!(&resp);
    ///
    /// // To get next page:
    ///
    /// if let Some(next_key) = resp.next_key {
    ///     let api = resp.next_api.map(|a| a.to_string());
    ///     let mut context = RangeQueryContext::new();
    ///     context.set_from(next_key);
    ///
    ///     let req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .all_range::<String>(api, context)
    ///     .build()
    ///     .unwrap();
    ///
    ///     let next_page_resp = req.get_page().unwrap();
    ///     dbg!(next_page_resp);
    /// }

    /// ```
    pub fn get_page(&self) -> Result<RestrictionRange> {
        match &self.request.target {
            None | Some(RequestTarget::Single { .. }) => {
                Err(RestrictError::IncorrectRestrictRequestTarget)?
            }
            Some(RequestTarget::ApiRange { .. }) => {
                let url = self.range_url()?;

                let resp = self
                    .request
                    .client
                    .get(url)
                    .bearer_auth(&self.request.token)
                    .send()?;
                map_api_error(resp)?
                    .json::<RestrictionRange>()
                    .map_err(Into::into)
            }
            Some(RequestTarget::AllRange { .. }) => {
                let url = self.range_url()?;

                let resp = self
                    .request
                    .client
                    .get(url)
                    .bearer_auth(&self.request.token)
                    .send()?;
                map_api_error(resp)?
                    .json::<RestrictionRange>()
                    .map_err(Into::into)
            }
        }
    }

    /// Returns all restrictions within for a tenant or API.
    /// May perform multiple requests.
    ///
    /// If no directory is given, the root directory is used.
    /// If no `from` is given, the range begins from the start.
    ///
    /// **NOTE:** This endpoint requires the `RequestTarget` be a `ApiRange` or
    /// `AllRange`.
    ///
    /// # Examples
    ///
    /// ## Getting all restrictions for an API
    ///
    /// ```no_run
    /// use seaplane::api::{
    ///     restrict::v1::{RestrictRequest, RestrictRequestBuilder},
    ///     shared::v1::RangeQueryContext,
    /// };
    ///
    /// let context = RangeQueryContext::new();
    /// let mut req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .api_range("config", context)
    ///     .build()
    ///     .unwrap();
    ///
    /// let resp = req.get_all_pages().unwrap();
    /// dbg!(resp);
    /// ```
    ///
    /// ## Getting all restrictions across all APIs
    ///
    /// ```no_run
    /// use seaplane::api::{
    ///     restrict::v1::{RestrictRequest, RestrictRequestBuilder},
    ///     shared::v1::RangeQueryContext,
    /// };
    ///
    /// let context = RangeQueryContext::new();
    /// let mut req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .all_range::<String>(None, context)
    ///     .build()
    ///     .unwrap();
    ///
    /// let resp = req.get_all_pages().unwrap();
    /// dbg!(resp);
    /// ```

    // TODO: Replace this with a collect on a Pages/Entries iterator
    pub fn get_all_pages(&mut self) -> Result<Vec<Restriction>> {
        let mut pages = Vec::new();
        loop {
            let mut rr = self.get_page()?;
            pages.append(&mut rr.restrictions);
            if let Some(next_key) = rr.next_key {
                match &mut self.request.target {
                    None | Some(RequestTarget::Single { .. }) => {
                        Err(RestrictError::IncorrectRestrictRequestTarget)?
                    }
                    Some(RequestTarget::ApiRange { api: _, context }) => {
                        context.set_from(next_key);
                    }
                    Some(RequestTarget::AllRange { from_api: _, context }) => {
                        context.set_from(next_key);
                        self.request.target = Some(RequestTarget::AllRange {
                            from_api: rr.next_api.map(|a| a.to_string()),
                            context: context.to_owned(),
                        });
                    }
                }
            } else {
                break;
            }
        }
        Ok(pages)
    }

    /// Sets a restriction for an API-directory combination
    ///
    /// **NOTE:** This endpoint requires the `RequestTarget` be a `Single`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::str::FromStr;
    ///
    /// use seaplane::api::{
    ///     restrict::v1::{RestrictRequest, RestrictRequestBuilder, RestrictionDetails},
    ///     shared::v1::Region,
    /// };
    ///
    /// let req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .single_restriction("config", "bW9ieQo")
    ///     .build()
    ///     .unwrap();
    ///
    /// let details = RestrictionDetails::builder()
    ///     .add_allowed_region(Region::from_str("xe").unwrap())
    ///     .build()
    ///     .unwrap();
    ///
    /// let resp = req.set_restriction(details).unwrap();
    /// dbg!(resp);
    /// ```
    pub fn set_restriction(&self, details: RestrictionDetails) -> Result<()> {
        let url = self.single_url()?;
        let resp = self
            .request
            .client
            .put(url)
            .bearer_auth(&self.request.token)
            .header(CONTENT_TYPE, header::HeaderValue::from_static("application/json"))
            .body(serde_json::to_string(&details)?)
            .send()?;
        map_api_error(resp)?
            .text()
            .map(|_| ()) // TODO: for now we drop the "success" message to control it ourselves
            .map_err(Into::into)
    }

    /// Removes a restriction for an API-directory combination
    ///
    /// **NOTE:** This endpoint requires the `RequestTarget` be a `Single`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use seaplane::api::restrict::v1::{RestrictRequest, RestrictRequestBuilder};
    ///
    /// let req = RestrictRequestBuilder::new()
    ///     .token("abc123_token")
    ///     .single_restriction("config", "bW9ieQo")
    ///     .build()
    ///     .unwrap();
    ///
    /// let resp = req.delete_restriction().unwrap();
    /// dbg!(resp);
    /// ```
    pub fn delete_restriction(&self) -> Result<()> {
        let url = self.single_url()?;
        let resp = self
            .request
            .client
            .delete(url)
            .bearer_auth(&self.request.token)
            .send()?;
        map_api_error(resp)?
            .text()
            .map(|_| ()) // TODO: for now we drop the "success" message to control it ourselves
            .map_err(Into::into)
    }
}