Skip to content

Commit

Permalink
Improve extensions API support
Browse files Browse the repository at this point in the history
- Set request content-types for JSON payloads
- Print the returned status codes in case of errors

Signed-off-by: David Calavera <david.calavera@gmail.com>
  • Loading branch information
calavera committed Feb 1, 2023
1 parent cccce9d commit 2f322ae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lambda-extension/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ where
)?;
let res = client.call(req).await?;
if res.status() != http::StatusCode::OK {
return Err(ExtensionError::boxed("unable to initialize the logs api"));
let err = format!("unable to initialize the logs api: {}", res.status());
return Err(ExtensionError::boxed(err));
}
trace!("Registered extension with Logs API");
}
Expand Down Expand Up @@ -288,7 +289,8 @@ where
)?;
let res = client.call(req).await?;
if res.status() != http::StatusCode::OK {
return Err(ExtensionError::boxed("unable to initialize the telemetry api"));
let err = format!("unable to initialize the telemetry api: {}", res.status());
return Err(ExtensionError::boxed(err));
}
trace!("Registered extension with Telemetry API");
}
Expand Down Expand Up @@ -422,7 +424,8 @@ async fn register<'a>(
let req = requests::register_request(&name, events)?;
let res = client.call(req).await?;
if res.status() != http::StatusCode::OK {
return Err(ExtensionError::boxed("unable to register the extension"));
let err = format!("unable to register the extension: {}", res.status());
return Err(ExtensionError::boxed(err));
}

let header = res
Expand Down
4 changes: 4 additions & 0 deletions lambda-extension/src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use serde::Serialize;
const EXTENSION_NAME_HEADER: &str = "Lambda-Extension-Name";
pub(crate) const EXTENSION_ID_HEADER: &str = "Lambda-Extension-Identifier";
const EXTENSION_ERROR_TYPE_HEADER: &str = "Lambda-Extension-Function-Error-Type";
const CONTENT_TYPE_HEADER_NAME: &str = "Content-Type";
const CONTENT_TYPE_HEADER_VALUE: &str = "application/json";

pub(crate) fn next_event_request(extension_id: &str) -> Result<Request<Body>, Error> {
let req = build_request()
Expand All @@ -24,6 +26,7 @@ pub(crate) fn register_request(extension_name: &str, events: &[&str]) -> Result<
.method(Method::POST)
.uri("/2020-01-01/extension/register")
.header(EXTENSION_NAME_HEADER, extension_name)
.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE)
.body(Body::from(serde_json::to_string(&events)?))?;

Ok(req)
Expand Down Expand Up @@ -73,6 +76,7 @@ pub(crate) fn subscribe_request(
.method(Method::PUT)
.uri(api.uri())
.header(EXTENSION_ID_HEADER, extension_id)
.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE)
.body(Body::from(serde_json::to_string(&data)?))?;

Ok(req)
Expand Down

0 comments on commit 2f322ae

Please sign in to comment.