Skip to content

Commit

Permalink
Rename use-property-bag to use-config-bag, add request-interceptor ex…
Browse files Browse the repository at this point in the history
…ample
  • Loading branch information
Fahad Zubair committed Oct 9, 2023
1 parent 174f02f commit 8f3512c
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/pokemon-service-client-usage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ hyper-native-tls = "0.3.0"
openssl = "0.10.57"
hyper-tls = "0.5.0"
http = "0.2.9"
uuid = "1.4.1"
aws-smithy-runtime-api = { version = "0.56.1", features = ["client"] }
14 changes: 5 additions & 9 deletions examples/pokemon-service-client-usage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ cargo run --example simple-client

| Rust Example | Description |
| ----------------------------- | --------------------------------------------------------------------------------------------------------- |
| simple-client | DONE: Creates a Smithy Client and calls an operation on it. |
| simple-client | Creates a Smithy Client and calls an operation on it. |
| endpoint-resolver | How to set a custom endpoint resolver |
| endpoint-using-middleware | FIX-LATER: A custom middleware layer for setting endpoint on a request. |
| handling-errors | FIX-LATER: How to send an input parameter to an operation, and to handle errors. |
| endpoint-using-interceptor | A custom middleware layer for setting endpoint on a request. |
| handling-errors | How to send an input parameter to an operation, and to handle errors. |
| custom-header | How to add headers to a request. |
| custom-header-using-interceptor | How to add headers to all request using an inteceptor |
| custom-header-using-interceptor | How to access operation name being called in an interceptor. |
| response-header-interceptor | A middleware that is called after the response has been received. |
| property-bag-usage | How to use the property bag to pass data across interceptors. |
| retries-customize | Customize retry settings. |
| retries-disable | How to disable retries by default. |
|retries-disable | How to disable retries by default. |
| timeout-config | How to set configuration for different timeout. |
| mock-request | Use a custom connector to generate responses without sending them on the wire. |
|Testing using MockSmithyConnector|TODO|
|Runtime Components|TODO|
|Handling redirects|TODO|
|Raising error in interceptor|TODO|
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/// In this example, a custom header x-amzn-requestid is set for all outgoing requests.
///
/// The example assumes that the Pokemon service is running on the localhost on TCP port 13734.
/// Refer to the [README.md](https://github.com/awslabs/smithy-rs/tree/main/examples/pokemon-service-client-usage/README.md)
/// file for instructions on how to launch the service locally.
///
/// The example can be run using `cargo run --example custom-header-using-interceptor`.
///
use aws_smithy_types::config_bag::ConfigBag;
use pokemon_service_client::Client as PokemonClient;
use pokemon_service_client::{
config::{interceptors::BeforeTransmitInterceptorContextMut, Interceptor, RuntimeComponents},
error::BoxError,
};
use tracing::info;
use uuid::Uuid;

// URL where example Pokemon service is running.
static BASE_URL: &str = "http://localhost:13734";
// Header to send with each operation.
const HEADER_TO_SEND: hyper::header::HeaderName =
hyper::header::HeaderName::from_static("x-amzn-requestid");

// The RequestIdInterceptor keeps a map of operation specific value to send
// for the header.
#[derive(Debug, Clone)]
pub struct RequestIdInterceptor;

impl Interceptor for RequestIdInterceptor {
fn name(&self) -> &'static str {
"RequestIdInterceptor"
}

/// Before the request is signed, add the header to the outgoing request.
fn modify_before_signing(
&self,
context: &mut BeforeTransmitInterceptorContextMut<'_>,
_runtime_components: &RuntimeComponents,
_cfg: &mut ConfigBag,
) -> Result<(), BoxError> {
let request_id = hyper::header::HeaderValue::from_str(&Uuid::new_v4().to_string())
.expect("failed to construct a header value from UUID");

context
.request_mut()
.headers_mut()
.insert(&HEADER_TO_SEND, request_id);

Ok(())
}
}

/// Creates a new Smithy client that is configured to communicate with a locally running Pokemon service on TCP port 13734.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let client = create_client();
/// ```
fn create_client() -> PokemonClient {
// The generated client has a type config::Builder that can be used to build a Config, which
// allows configuring endpoint-resolver, timeouts, retries etc.
let config = pokemon_service_client::Config::builder()
.endpoint_resolver(BASE_URL)
.interceptor(RequestIdInterceptor {})
.build();

pokemon_service_client::Client::from_conf(config)
}

#[tokio::main]
async fn main() {
tracing_setup();

// Create a configured Smithy client.
let client = create_client();

// Call the operation `get_storage` on Pokemon service.
let response = client
.get_storage()
.user("ash")
.passcode("pikachu123")
.send()
.await
.expect("Operation could not be called");

info!(%BASE_URL, ?response, "Response for get_storage()");
}

fn tracing_setup() {
if std::env::var("RUST_LIB_BACKTRACE").is_err() {
std::env::set_var("RUST_LIB_BACKTRACE", "1");
}
let _ = color_eyre::install();

if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
}
tracing_subscriber::fmt::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// Refer to the [README.md](https://github.com/awslabs/smithy-rs/tree/main/examples/pokemon-service-client-usage/README.md)
/// file for instructions on how to launch the service locally.
///
/// The example can be run using `cargo run --example use-property-bag`.
/// The example can be run using `cargo run --example use-config-bag`.
///
use aws_smithy_types::config_bag::{Storable, StoreReplace};
use std::time::Instant;
Expand Down

0 comments on commit 8f3512c

Please sign in to comment.