Expand description
Rust client SDK for the AWS Lambda Runtime APIs. This crate defines
a RuntimeClient that encapsulates interactions with AWS Lambda’s Runtime
APIs.
To return errors to the Runtime APIs through the event_error() or
fail_init() methods the Error objects must implement the error::RuntimeApiError
trait from this crate. The RuntimeApiError trait defines a single method
called to_response(). The method must return an error::RuntimeError object.
See the error::ApiError object in this crate for an example.
§Examples
extern crate lambda_runtime_client;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use lambda_runtime_client::{RuntimeClient, EventContext};
#[derive(Serialize, Deserialize, Debug)]
struct CustomEvent {
name: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct CustomResponse {
surname: String,
}
fn main() {
let client = RuntimeClient::new("http://localhost:8080", None, None)
.expect("Could not initialize client");
let (event_data, event_context) = client.next_event()
.expect("Could not retrieve next event");
let custom_event: CustomEvent = serde_json::from_slice(&event_data)
.expect("Could not turn Vec<u8> into CustomEvent object");
println!("Event for {}", custom_event.name);
if custom_event.name == "John" {
let resp_object = CustomResponse{ surname: String::from("Doe")};
let resp_vec = serde_json::to_vec(&resp_object)
.expect("Could not serialize CustomResponse to Vec<u8>");
client.event_response(&event_context.aws_request_id, &resp_vec)
.expect("Response sent successfully");
} else {
// return a custom error by implementing the RuntimeApiError trait.
// See the error module for examples.
//client.event_error(&event_context.aws_request_id, CustomErrorType::new("Invalid first name"))
// .expect("Could not send error response");
}
}Modules§
- error
- This module defines the
RuntimeApiErrortrait that developers should implement to send their custom errors to the AWS Lambda Runtime Client SDK. The module also defines theApiErrortype returned by theRuntimeClientimplementations.
Structs§
- Client
Application - AWS Moble SDK client properties
- Client
Context - Client context sent by the AWS Mobile SDK.
- Cognito
Identity - Cognito identity information sent with the event
- Event
Context - The Lambda function execution context. The values in this struct
are populated using the Lambda environment variables
and the headers returned by the poll request to the Runtime APIs.
A new instance of the
Contextobject is passed to each handler invocation. - Runtime
Client - Used by the Runtime to communicate with the internal endpoint.
Enums§
- Lambda
Headers - Enum of the headers returned by Lambda’s
/nextAPI call.