From 572ad1994cac1162b96f9de991fa906b0c2c5f96 Mon Sep 17 00:00:00 2001 From: Harold Sun Date: Tue, 22 Mar 2022 11:08:17 +0800 Subject: [PATCH 1/2] allow customized User_Agent for lambda runtime api client --- lambda-runtime-api-client/README.md | 5 +++++ lambda-runtime-api-client/src/lib.rs | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lambda-runtime-api-client/README.md b/lambda-runtime-api-client/README.md index 530fefdd..2a5c9e63 100644 --- a/lambda-runtime-api-client/README.md +++ b/lambda-runtime-api-client/README.md @@ -33,3 +33,8 @@ async fn main() -> Result<(), Error> { client.call(request).await } ``` + +## Custom User Agent + +To customize User Agent header sent to Lambda Runtime API, you can configure an environment variable `LAMBDA_RUNTIME_USER_AGENT` at compiling time. +This will overide the default User Agent. \ No newline at end of file diff --git a/lambda-runtime-api-client/src/lib.rs b/lambda-runtime-api-client/src/lib.rs index 83d789cc..253d561c 100644 --- a/lambda-runtime-api-client/src/lib.rs +++ b/lambda-runtime-api-client/src/lib.rs @@ -14,7 +14,8 @@ use tokio::io::{AsyncRead, AsyncWrite}; use tower_service::Service; const USER_AGENT_HEADER: &str = "User-Agent"; -const USER_AGENT: &str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION")); +const DEFAULT_USER_AGENT: &str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION")); +const CUSTOM_USER_AGENT: Option<&str> = option_env!("LAMBDA_RUNTIME_USER_AGENT"); /// Error type that lambdas may result in pub type Error = Box; @@ -128,7 +129,13 @@ where /// Create a request builder. /// This builder uses `aws-lambda-rust/CRATE_VERSION` as /// the default User-Agent. +/// Configure environment variable `LAMBDA_RUNTIME_USER_AGENT` +/// at compile time to modify User-Agent value. pub fn build_request() -> http::request::Builder { + const USER_AGENT: &str = match CUSTOM_USER_AGENT { + Some(value) => value, + None => DEFAULT_USER_AGENT, + }; http::Request::builder().header(USER_AGENT_HEADER, USER_AGENT) } From 59415b747e3a858032c7f18daac4512652fa01e4 Mon Sep 17 00:00:00 2001 From: Harold Sun Date: Wed, 23 Mar 2022 08:13:57 +0800 Subject: [PATCH 2/2] update the README --- lambda-runtime-api-client/README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lambda-runtime-api-client/README.md b/lambda-runtime-api-client/README.md index 2a5c9e63..530fefdd 100644 --- a/lambda-runtime-api-client/README.md +++ b/lambda-runtime-api-client/README.md @@ -33,8 +33,3 @@ async fn main() -> Result<(), Error> { client.call(request).await } ``` - -## Custom User Agent - -To customize User Agent header sent to Lambda Runtime API, you can configure an environment variable `LAMBDA_RUNTIME_USER_AGENT` at compiling time. -This will overide the default User Agent. \ No newline at end of file