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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
// 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. #[cfg(feature = "stream")] use futures::Stream; use reqwest::{Method, RequestBuilder, Response, Url}; use serde::de::DeserializeOwned; use serde::Serialize; use super::loading; use super::request; use super::services::ServiceType; #[cfg(feature = "stream")] use super::stream::{paginated, Resource}; use super::{ApiVersion, AuthType, EndpointFilters, Error, InterfaceType, Session}; /// 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: Session, service: Srv, default_api_version: Option<ApiVersion>, } impl<Srv> From<Adapter<Srv>> for Session { fn from(value: Adapter<Srv>) -> Session { value.inner } } 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::from_session(Session::new(auth_type), service) } /// Create a new adapter from a `clouds.yaml` configuration file. /// /// See [Session::from_config](struct.Session.html#method.from_config) for details. #[inline] pub fn from_config<S: AsRef<str>>(cloud_name: S, service: Srv) -> Result<Adapter<Srv>, Error> { Ok(loading::from_config(cloud_name)?.into_adapter(service)) } /// Create a new adapter with information from environment variables. /// /// See [Session::from_env](struct.Session.html#method.from_env) for details. #[inline] pub fn from_env(service: Srv) -> Result<Adapter<Srv>, Error> { Ok(loading::from_env()?.into_adapter(service)) } /// Create a new adapter from a `Session`. #[inline] pub fn from_session(session: Session, service: Srv) -> Adapter<Srv> { Adapter { inner: session, service, default_api_version: None, } } /// Get a reference to the authentication type in use. #[inline] pub fn auth_type(&self) -> &dyn AuthType { self.inner.auth_type() } /// Default API version used when no version is specified. #[inline] pub fn default_api_version(&self) -> Option<ApiVersion> { self.default_api_version } /// Endpoint filters in use. #[inline] pub fn endpoint_filters(&self) -> &EndpointFilters { self.inner.endpoint_filters() } /// Modify endpoint filters. /// /// This call clears the cached service information for this `Adapter`. /// It does not, however, affect clones of this `Adapter`. #[inline] pub fn endpoint_filters_mut(&mut self) -> &mut EndpointFilters { self.inner.endpoint_filters_mut() } /// 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 async fn refresh(&mut self) -> Result<(), Error> { self.inner.refresh().await } /// Session used for this adapter. #[inline] pub fn session(&self) -> &Session { &self.inner } /// 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 the default API version. /// /// This version will be used when no version is specified. No checks are done against this /// version inside of this call. If it is not valid, the subsequent `request` calls will fail. #[inline] pub fn set_default_api_version(&mut self, api_version: Option<ApiVersion>) { self.default_api_version = api_version; } /// A convenience call to set an endpoint interface. /// /// This call clears the cached service information for this `Adapter`. /// It does not, however, affect clones of this `Adapter`. #[inline] pub fn set_endpoint_interface(&mut self, endpoint_interface: InterfaceType) { self.inner.set_endpoint_interface(endpoint_interface); } /// 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 default API version. #[inline] pub fn with_default_api_version(mut self, api_version: Option<ApiVersion>) -> Adapter<Srv> { self.set_default_api_version(api_version); self } /// Convert this adapter into one using the given endpoint filters. #[inline] pub fn with_endpoint_filters(mut self, endpoint_filters: EndpointFilters) -> Adapter<Srv> { *self.endpoint_filters_mut() = endpoint_filters; self } /// Convert this adapter into one using the given endpoint filters. #[inline] pub fn with_endpoint_interface(mut self, endpoint_interface: InterfaceType) -> Adapter<Srv> { 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 /// # async fn example() -> Result<(), osauth::Error> { /// let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE) /// .expect("Failed to create an identity provider from the environment"); /// let maybe_versions = adapter /// .get_api_versions() /// .await?; /// 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"); /// } /// # Ok(()) } /// # #[tokio::main] /// # async fn main() { example().await.unwrap(); } /// ``` #[inline] pub async fn get_api_versions(&self) -> Result<Option<(ApiVersion, ApiVersion)>, Error> { self.inner.get_api_versions(self.service.clone()).await } /// 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 async fn get_endpoint<I>(&self, path: I) -> Result<Url, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, { self.inner.get_endpoint(self.service.clone(), path).await } /// Get the currently used major version from the given service. /// /// Can return `None` if the service does not support API version discovery at all. #[inline] pub async fn get_major_version(&self) -> Result<Option<ApiVersion>, Error> { self.inner.get_major_version(self.service.clone()).await } /// Pick the highest API version supported by the service. /// /// Returns `None` if none of the requested versions are available. /// /// ```rust,no_run /// # async fn example() -> Result<(), osauth::Error> { /// 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 maybe_version = adapter /// .pick_api_version(candidates) /// .await?; /// if let Some(version) = maybe_version { /// println!("Using version {}", version); /// } else { /// println!("Using the base version"); /// } /// let response = adapter.get(&["servers"], maybe_version).await?; /// # Ok(()) } /// # #[tokio::main] /// # async fn main() { example().await.unwrap(); } /// ``` pub async fn pick_api_version<I>(&self, versions: I) -> Result<Option<ApiVersion>, Error> where I: IntoIterator<Item = ApiVersion>, I::IntoIter: Send, { self.inner .pick_api_version(self.service.clone(), versions) .await } /// Check if the service supports the API version. #[inline] pub async fn supports_api_version(&self, version: ApiVersion) -> Result<bool, Error> { self.pick_api_version(Some(version)) .await .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 /// # async fn example() -> Result<(), osauth::Error> { /// let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE) /// .expect("Failed to create an identity provider from the environment"); /// let response = osauth::request::send_checked( /// adapter /// .request(reqwest::Method::HEAD, &["servers", "1234"], None) /// .await? /// ) /// .await?; /// println!("Response: {:?}", response); /// # Ok(()) } /// # #[tokio::main] /// # async fn main() { example().await.unwrap(); } /// ``` /// /// 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 async fn request<I>( &self, method: Method, path: I, api_version: Option<ApiVersion>, ) -> Result<RequestBuilder, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, { let real_version = api_version.or(self.default_api_version); self.inner .request(self.service.clone(), method, path, real_version) .await } /// Issue a GET request. /// /// See [request](#method.request) for an explanation of the parameters. #[inline] pub async fn get<I>(&self, path: I, api_version: Option<ApiVersion>) -> Result<Response, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, { request::send_checked(self.request(Method::GET, path, api_version).await?).await } /// Fetch a JSON using the GET request. /// /// ```rust,no_run /// # async fn example() -> Result<(), osauth::Error> { /// 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::Adapter::from_env(osauth::services::COMPUTE) /// .expect("Failed to create an identity provider from the environment"); /// let servers: ServersRoot = adapter /// .get_json(&["servers"], None) /// .await?; /// for srv in servers.servers { /// println!("ID = {}, Name = {}", srv.id, srv.name); /// } /// # Ok(()) } /// # #[tokio::main] /// # async fn main() { example().await.unwrap(); } /// ``` /// /// See [request](#method.request) for an explanation of the parameters. #[inline] pub async fn get_json<I, T>(&self, path: I, api_version: Option<ApiVersion>) -> Result<T, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, T: DeserializeOwned + Send, { request::fetch_json(self.request(Method::GET, path, api_version).await?).await } /// Fetch a paginated list of JSON objects using the GET request. /// /// ```rust,no_run /// # async fn example() -> Result<(), osauth::Error> { /// use futures::pin_mut; /// use futures::stream::TryStreamExt; /// 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>, /// } /// /// // This implementatin defines the relationship between the root resource and its items. /// impl osauth::stream::Resource for Server { /// type Id = String; /// type Root = ServersRoot; /// fn resource_id(&self) -> Self::Id { /// self.id.clone() /// } /// } /// /// // This is another required part of the pagination contract. /// impl From<ServersRoot> for Vec<Server> { /// fn from(value: ServersRoot) -> Vec<Server> { /// value.servers /// } /// } /// /// let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE) /// .expect("Failed to create an identity provider from the environment"); /// /// let servers = adapter /// .get_json_paginated::<_, Server>(&["servers"], None, None, None) /// .await?; /// /// pin_mut!(servers); /// while let Some(srv) = servers.try_next().await? { /// println!("ID = {}, Name = {}", srv.id, srv.name); /// } /// # Ok(()) } /// # #[tokio::main] /// # async fn main() { example().await.unwrap(); } /// ``` /// /// See [request](#method.request) for an explanation of the parameters. #[cfg(feature = "stream")] pub async fn get_json_paginated<I, T>( &self, path: I, api_version: Option<ApiVersion>, limit: Option<usize>, starting_with: Option<<T as Resource>::Id>, ) -> Result<impl Stream<Item = Result<T, Error>>, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, T: Resource + Unpin, <T as Resource>::Root: Into<Vec<T>> + Send, { let builder = self.request(Method::GET, path, api_version).await?; Ok(paginated(builder, limit, starting_with)) } /// 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 async fn get_json_query<I, Q, T>( &self, path: I, query: Q, api_version: Option<ApiVersion>, ) -> Result<T, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, Q: Serialize + Send, T: DeserializeOwned + Send, { request::fetch_json( self.request(Method::GET, path, api_version) .await? .query(&query), ) .await } /// Fetch a paginated list of JSON objects 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. #[cfg(feature = "stream")] pub async fn get_json_query_paginated<I, Q, T>( &self, path: I, query: Q, api_version: Option<ApiVersion>, limit: Option<usize>, starting_with: Option<<T as Resource>::Id>, ) -> Result<impl Stream<Item = Result<T, Error>>, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, Q: Serialize + Send, T: Resource + Unpin, <T as Resource>::Root: Into<Vec<T>> + Send, { let builder = self .request(Method::GET, path, api_version) .await? .query(&query); Ok(paginated(builder, limit, starting_with)) } /// 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 async fn get_query<I, Q>( &self, path: I, query: Q, api_version: Option<ApiVersion>, ) -> Result<Response, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, Q: Serialize + Send, { request::send_checked( self.request(Method::GET, path, api_version) .await? .query(&query), ) .await } /// 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 async fn post<I, T>( &self, path: I, body: T, api_version: Option<ApiVersion>, ) -> Result<Response, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, T: Serialize + Send, { request::send_checked( self.request(Method::POST, path, api_version) .await? .json(&body), ) .await } /// 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 async fn post_json<I, T, R>( &self, path: I, body: T, api_version: Option<ApiVersion>, ) -> Result<R, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, T: Serialize + Send, R: DeserializeOwned + Send, { request::fetch_json( self.request(Method::POST, path, api_version) .await? .json(&body), ) .await } /// 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 async fn put<I, T>( &self, path: I, body: T, api_version: Option<ApiVersion>, ) -> Result<Response, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, T: Serialize + Send, { request::send_checked( self.request(Method::PUT, path, api_version) .await? .json(&body), ) .await } /// Issue an empty PUT request. /// /// See [request](#method.request) for an explanation of the parameters. #[inline] pub async fn put_empty<I>( &self, path: I, api_version: Option<ApiVersion>, ) -> Result<Response, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, { request::send_checked(self.request(Method::PUT, path, api_version).await?).await } /// 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 async fn put_json<I, T, R>( &self, path: I, body: T, api_version: Option<ApiVersion>, ) -> Result<R, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, T: Serialize + Send, R: DeserializeOwned + Send, { request::fetch_json( self.request(Method::PUT, path, api_version) .await? .json(&body), ) .await } /// Issue a DELETE request. /// /// See [request](#method.request) for an explanation of the parameters. #[inline] pub async fn delete<I>( &self, path: I, api_version: Option<ApiVersion>, ) -> Result<Response, Error> where I: IntoIterator, I::Item: AsRef<str>, I::IntoIter: Send, { request::send_checked(self.request(Method::DELETE, path, api_version).await?).await } }