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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Copyright 2019 Dmitry Tantsur <divius.inside@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Adapter for a specific service.

use futures::Future;
use reqwest::r#async::{RequestBuilder, Response};
use reqwest::{Method, Url};
use serde::de::DeserializeOwned;
use serde::Serialize;

use super::config;
use super::request;
use super::services::ServiceType;
use super::sessioninner::SessionInner;
use super::{ApiVersion, AuthType, Error};

/// Adapter for a specific service.
///
/// An `Adapter` is very similar to a [Session](struct.Session.html), but is tied to a specific
/// service, and thus does not require passing a `service` argument to all calls.
#[derive(Debug, Clone)]
pub struct Adapter<Srv> {
    inner: SessionInner,
    service: Srv,
    endpoint_interface: Option<String>,
}

impl<Srv> Adapter<Srv> {
    /// Create a new adapter with a given authentication plugin.
    pub fn new<Auth: AuthType + 'static>(auth_type: Auth, service: Srv) -> Adapter<Srv> {
        Adapter {
            inner: SessionInner::new(auth_type),
            service,
            endpoint_interface: None,
        }
    }

    /// Create a new adapter from a `clouds.yaml` configuration file.
    pub fn from_config<S: AsRef<str>>(cloud_name: S, service: Srv) -> Result<Adapter<Srv>, Error> {
        Ok(config::from_config(cloud_name)?.into_adapter(service))
    }

    /// Create a new adapter with information from environment variables.
    ///
    /// Uses some of `OS_*` variables recognized by `python-openstackclient`.
    pub fn from_env(service: Srv) -> Result<Adapter<Srv>, Error> {
        Ok(config::from_env()?.into_adapter(service))
    }

    /// Create a new adapter from its components.
    #[inline]
    pub(crate) fn new_from(
        inner: SessionInner,
        service: Srv,
        endpoint_interface: Option<String>,
    ) -> Adapter<Srv> {
        Adapter {
            inner,
            service,
            endpoint_interface,
        }
    }

    /// Get a reference to the authentication type in use.
    #[inline]
    pub fn auth_type(&self) -> &AuthType {
        self.inner.auth_type()
    }

    /// Endpoint interface in use (if any).
    #[inline]
    pub fn endpoint_interface(&self) -> &Option<String> {
        &self.endpoint_interface
    }

    /// Update the authentication and purges cached endpoint information.
    ///
    /// # Warning
    ///
    /// Authentication will also be updated for clones of this `Adapter` and its parent `Session`,
    /// since they share the same authentication object.
    #[inline]
    pub fn refresh(&mut self) -> impl Future<Item = (), Error = Error> + Send {
        self.inner.refresh()
    }

    /// Set a new authentication for this `Adapter`.
    ///
    /// This call clears the cached service information for this `Adapter`.
    /// It does not, however, affect clones of this `Adapter`.
    #[inline]
    pub fn set_auth_type<Auth: AuthType + 'static>(&mut self, auth_type: Auth) {
        self.inner.set_auth_type(auth_type)
    }

    /// Set endpoint interface to use.
    ///
    /// This call clears the cached service information for this `Adapter`.
    /// It does not, however, affect clones of this `Adapter`.
    pub fn set_endpoint_interface<S>(&mut self, endpoint_interface: S)
    where
        S: Into<String>,
    {
        self.inner.reset_cache();
        self.endpoint_interface = Some(endpoint_interface.into());
    }

    /// Convert this adapter into one using the given authentication.
    #[inline]
    pub fn with_auth_type<Auth: AuthType + 'static>(mut self, auth_method: Auth) -> Adapter<Srv> {
        self.set_auth_type(auth_method);
        self
    }

    /// Convert this adapter into one using the given endpoint interface.
    #[inline]
    pub fn with_endpoint_interface<S>(mut self, endpoint_interface: S) -> Adapter<Srv>
    where
        S: Into<String>,
    {
        self.set_endpoint_interface(endpoint_interface);
        self
    }
}

impl<Srv: ServiceType + Send + Clone> Adapter<Srv> {
    /// Get minimum/maximum API (micro)version information.
    ///
    /// Returns `None` if the range cannot be determined, which usually means
    /// that microversioning is not supported.
    ///
    /// ```rust,no_run
    /// use futures::Future;
    ///
    /// let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE)
    ///     .expect("Failed to create an identity provider from the environment");
    /// let future = adapter
    ///     .get_api_versions()
    ///     .map(|maybe_versions| {
    ///         if let Some((min, max)) = maybe_versions {
    ///             println!("The compute service supports versions {} to {}", min, max);
    ///         } else {
    ///             println!("The compute service does not support microversioning");
    ///         }
    ///     });
    /// ```
    pub fn get_api_versions(
        &self,
    ) -> impl Future<Item = Option<(ApiVersion, ApiVersion)>, Error = Error> + Send {
        self.inner
            .get_api_versions(self.service.clone(), &self.endpoint_interface)
    }

    /// Construct an endpoint from the path;
    ///
    /// You won't need to use this call most of the time, since all request calls can fetch the
    /// endpoint automatically.
    pub fn get_endpoint<I>(&self, path: I) -> impl Future<Item = Url, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.inner
            .get_endpoint(self.service.clone(), &self.endpoint_interface, path)
    }

    /// Get the currently used major version from the given service.
    ///
    /// Can return `None` if the service does not support API version discovery at all.
    pub fn get_major_version(
        &self,
    ) -> impl Future<Item = Option<ApiVersion>, Error = Error> + Send {
        self.inner
            .get_major_version(self.service.clone(), &self.endpoint_interface)
    }

    /// Pick the highest API version supported by the service.
    ///
    /// Returns `None` if none of the requested versions are available.
    ///
    /// ```rust,no_run
    /// use futures::Future;
    ///
    /// let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE)
    ///     .expect("Failed to create an identity provider from the environment");
    /// let candidates = vec![osauth::ApiVersion(1, 2), osauth::ApiVersion(1, 42)];
    /// let future = adapter
    ///     .pick_api_version(candidates)
    ///     .and_then(|maybe_version| {
    ///         if let Some(version) = maybe_version {
    ///             println!("Using version {}", version);
    ///         } else {
    ///             println!("Using the base version");
    ///         }
    ///         adapter.get(&["servers"], maybe_version)
    ///     });
    /// ```
    pub fn pick_api_version<I>(
        &self,
        versions: I,
    ) -> impl Future<Item = Option<ApiVersion>, Error = Error> + Send
    where
        I: IntoIterator<Item = ApiVersion>,
        I::IntoIter: Send,
    {
        self.inner
            .pick_api_version(self.service.clone(), &self.endpoint_interface, versions)
    }

    /// Check if the service supports the API version.
    pub fn supports_api_version(
        &self,
        version: ApiVersion,
    ) -> impl Future<Item = bool, Error = Error> + Send {
        self.pick_api_version(Some(version)).map(|x| x.is_some())
    }

    /// Make an HTTP request.
    ///
    /// The `path` argument is a URL path without the service endpoint (e.g. `/servers/1234`).
    ///
    /// If `api_version` is set, it is send with the request to enable a higher API version.
    /// Otherwise the base API version is used. You can use
    /// [pick_api_version](#method.pick_api_version) to choose an API version to use.
    ///
    /// The result is a `RequestBuilder` that can be customized further. Error checking and response
    /// parsing can be done using functions from the [request](request/index.html) module.
    ///
    /// ```rust,no_run
    /// use futures::Future;
    /// use reqwest::Method;
    ///
    /// let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE)
    ///     .expect("Failed to create an identity provider from the environment");
    /// let future = adapter
    ///     .request(Method::HEAD, &["servers", "1234"], None)
    ///     .then(osauth::request::send_checked)
    ///     .map(|response| {
    ///         println!("Response: {:?}", response);
    ///     });
    /// ```
    ///
    /// This is the most generic call to make a request. You may prefer to use more specific `get`,
    /// `post`, `put` or `delete` calls instead.
    pub fn request<I>(
        &self,
        method: Method,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = RequestBuilder, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.inner.request(
            self.service.clone(),
            &self.endpoint_interface,
            method,
            path,
            api_version,
        )
    }

    /// Start a GET request.
    ///
    /// Use this call if you need some advanced features of the resulting `RequestBuilder`.
    /// Otherwise use:
    /// * [get](#method.get) to issue a generic GET without a query.
    /// * [get_query](#method.get_query) to issue a generic GET with a query.
    /// * [get_json](#method.get_json) to issue GET and parse a JSON result.
    /// * [get_json_query](#method.get_json_query) to issue GET with a query and parse a JSON
    ///   result.
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn start_get<I>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = RequestBuilder, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.request(Method::GET, path, api_version)
    }

    /// Issue a GET request.
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn get<I>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = Response, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.start_get(path, api_version)
            .then(request::send_checked)
    }

    /// Fetch a JSON using the GET request.
    ///
    /// ```rust,no_run
    /// use futures::Future;
    /// use serde::Deserialize;
    ///
    /// #[derive(Debug, Deserialize)]
    /// pub struct Server {
    ///     pub id: String,
    ///     pub name: String,
    /// }
    ///
    /// #[derive(Debug, Deserialize)]
    /// pub struct ServersRoot {
    ///     pub servers: Vec<Server>,
    /// }
    ///
    /// let adapter = osauth::from_env()
    ///     .expect("Failed to create an identity provider from the environment")
    ///     .into_adapter(osauth::services::COMPUTE);
    /// let future = adapter
    ///     .get_json(&["servers"], None)
    ///     .map(|servers: ServersRoot| {
    ///         for srv in servers.servers {
    ///             println!("ID = {}, Name = {}", srv.id, srv.name);
    ///         }
    ///     });
    /// ```
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn get_json<I, T>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = T, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
        T: DeserializeOwned + Send,
    {
        self.start_get(path, api_version).then(request::fetch_json)
    }

    /// Fetch a JSON using the GET request with a query.
    ///
    /// See `reqwest` crate documentation for how to define a query.
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn get_json_query<I, Q, T>(
        &self,
        path: I,
        query: Q,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = T, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
        Q: Serialize + Send,
        T: DeserializeOwned + Send,
    {
        self.start_get(path, api_version)
            .map(move |builder| builder.query(&query))
            .then(request::fetch_json)
    }

    /// Issue a GET request with a query
    ///
    /// See `reqwest` crate documentation for how to define a query.
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn get_query<I, Q>(
        &self,
        path: I,
        query: Q,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = Response, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
        Q: Serialize + Send,
    {
        self.start_get(path, api_version)
            .map(move |builder| builder.query(&query))
            .then(request::send_checked)
    }

    /// Start a POST request.
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn start_post<I>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = RequestBuilder, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.request(Method::POST, path, api_version)
    }

    /// POST a JSON object.
    ///
    /// The `body` argument is anything that can be serialized into JSON.
    ///
    /// See [request](#method.request) for an explanation of the other parameters.
    #[inline]
    pub fn post<I, T>(
        &self,
        path: I,
        body: T,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = Response, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
        T: Serialize + Send,
    {
        self.start_post(path, api_version)
            .map(move |builder| builder.json(&body))
            .then(request::send_checked)
    }

    /// POST a JSON object and receive a JSON back.
    ///
    /// The `body` argument is anything that can be serialized into JSON.
    ///
    /// See [request](#method.request) for an explanation of the other parameters.
    #[inline]
    pub fn post_json<I, T, R>(
        &self,
        path: I,
        body: T,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = R, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
        T: Serialize + Send,
        R: DeserializeOwned + Send,
    {
        self.start_post(path, api_version)
            .map(move |builder| builder.json(&body))
            .then(request::fetch_json)
    }

    /// Start a PUT request.
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn start_put<I>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = RequestBuilder, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.request(Method::PUT, path, api_version)
    }

    /// PUT a JSON object.
    ///
    /// The `body` argument is anything that can be serialized into JSON.
    ///
    /// See [request](#method.request) for an explanation of the other parameters.
    #[inline]
    pub fn put<I, T>(
        &self,
        path: I,
        body: T,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = Response, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
        T: Serialize + Send,
    {
        self.start_put(path, api_version)
            .map(move |builder| builder.json(&body))
            .then(request::send_checked)
    }

    /// Issue an empty PUT request.
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn put_empty<I>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = Response, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.start_put(path, api_version)
            .then(request::send_checked)
    }

    /// PUT a JSON object and receive a JSON back.
    ///
    /// The `body` argument is anything that can be serialized into JSON.
    ///
    /// See [request](#method.request) for an explanation of the other parameters.
    #[inline]
    pub fn put_json<I, T, R>(
        &self,
        path: I,
        body: T,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = R, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
        T: Serialize + Send,
        R: DeserializeOwned + Send,
    {
        self.start_put(path, api_version)
            .map(move |builder| builder.json(&body))
            .then(request::fetch_json)
    }

    /// Start a DELETE request.
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn start_delete<I>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = RequestBuilder, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.request(Method::DELETE, path, api_version)
    }

    /// Issue a DELETE request.
    ///
    /// See [request](#method.request) for an explanation of the parameters.
    #[inline]
    pub fn delete<I>(
        &self,
        path: I,
        api_version: Option<ApiVersion>,
    ) -> impl Future<Item = Response, Error = Error> + Send
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
        I::IntoIter: Send,
    {
        self.start_delete(path, api_version)
            .then(request::send_checked)
    }
}