use crate::types::lambda::*;
use crate::{AwsHttpClient, Result};
use urlencoding::encode;
pub struct LambdaOps<'a> {
pub(crate) client: &'a AwsHttpClient,
}
impl<'a> LambdaOps<'a> {
pub(crate) fn new(client: &'a AwsHttpClient) -> Self {
Self { client }
}
fn base_url(&self) -> String {
#[cfg(any(test, feature = "test-support"))]
{
if let Some(ref base) = self.client.base_url {
return base.trim_end_matches('/').to_string();
}
}
"https://lambda.{region}.amazonaws.com".replace("{region}", self.client.region())
}
#[allow(dead_code)]
pub(crate) async fn list_functions(
&self,
master_region: &str,
function_version: &str,
marker: &str,
max_items: &str,
) -> Result<ListFunctionsResponse> {
let url = format!("{}/2015-03-31/functions", self.base_url(),);
let url = crate::append_query_params(
url,
&[
("MasterRegion", master_region),
("FunctionVersion", function_version),
("Marker", marker),
("MaxItems", max_items),
],
);
let response = self.client.get_json(&url, "lambda").await?;
let response = response.error_for_status("json").await?;
let response_bytes =
response
.bytes()
.await
.map_err(|e| crate::AwsError::InvalidResponse {
message: format!("Failed to read list_functions response: {e}"),
body: None,
})?;
serde_json::from_slice(&response_bytes).map_err(|e| crate::AwsError::InvalidResponse {
message: format!("Failed to parse list_functions response: {e}"),
body: Some(String::from_utf8_lossy(&response_bytes).to_string()),
})
}
#[allow(dead_code)]
pub(crate) async fn get_function_configuration(
&self,
function_name: &str,
qualifier: &str,
) -> Result<FunctionConfiguration> {
let url = format!(
"{}/2015-03-31/functions/{}/configuration",
self.base_url(),
encode(function_name),
);
let url = crate::append_query_params(url, &[("Qualifier", qualifier)]);
let response = self.client.get_json(&url, "lambda").await?;
let response = response.error_for_status("json").await?;
let response_bytes =
response
.bytes()
.await
.map_err(|e| crate::AwsError::InvalidResponse {
message: format!("Failed to read get_function_configuration response: {e}"),
body: None,
})?;
serde_json::from_slice(&response_bytes).map_err(|e| crate::AwsError::InvalidResponse {
message: format!("Failed to parse get_function_configuration response: {e}"),
body: Some(String::from_utf8_lossy(&response_bytes).to_string()),
})
}
#[allow(dead_code)]
pub(crate) async fn update_function_configuration(
&self,
function_name: &str,
body: &UpdateFunctionConfigurationRequest,
) -> Result<FunctionConfiguration> {
let url = format!(
"{}/2015-03-31/functions/{}/configuration",
self.base_url(),
encode(function_name),
);
let body_bytes =
serde_json::to_vec(body).map_err(|e| crate::AwsError::InvalidResponse {
message: format!("Failed to serialize update_function_configuration request: {e}"),
body: None,
})?;
let response = self
.client
.put(&url, "lambda", &body_bytes, "application/json")
.await?;
let response = response.error_for_status("json").await?;
let response_bytes =
response
.bytes()
.await
.map_err(|e| crate::AwsError::InvalidResponse {
message: format!("Failed to read update_function_configuration response: {e}"),
body: None,
})?;
serde_json::from_slice(&response_bytes).map_err(|e| crate::AwsError::InvalidResponse {
message: format!("Failed to parse update_function_configuration response: {e}"),
body: Some(String::from_utf8_lossy(&response_bytes).to_string()),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_list_functions() {
let mut mock = crate::MockClient::new();
mock.expect_get("/2015-03-31/functions?MasterRegion=test-MasterRegion&FunctionVersion=test-FunctionVersion&Marker=test-Marker&MaxItems=test-MaxItems")
.returning_json(serde_json::to_value(ListFunctionsResponse::fixture()).unwrap());
let client = crate::AwsHttpClient::from_mock(mock);
let ops = LambdaOps::new(&client);
let result = ops
.list_functions(
"test-MasterRegion",
"test-FunctionVersion",
"test-Marker",
"test-MaxItems",
)
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_get_function_configuration() {
let mut mock = crate::MockClient::new();
mock.expect_get(
"/2015-03-31/functions/test-FunctionName/configuration?Qualifier=test-Qualifier",
)
.returning_json(serde_json::to_value(FunctionConfiguration::fixture()).unwrap());
let client = crate::AwsHttpClient::from_mock(mock);
let ops = LambdaOps::new(&client);
let result = ops
.get_function_configuration("test-FunctionName", "test-Qualifier")
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_update_function_configuration() {
let mut mock = crate::MockClient::new();
mock.expect_put("/2015-03-31/functions/test-FunctionName/configuration")
.returning_json(serde_json::to_value(FunctionConfiguration::fixture()).unwrap());
let client = crate::AwsHttpClient::from_mock(mock);
let ops = LambdaOps::new(&client);
let body = UpdateFunctionConfigurationRequest::fixture();
let result = ops
.update_function_configuration("test-FunctionName", &body)
.await;
assert!(result.is_ok());
}
}