lambda_runtime_client/
lib.rs

1#![warn(missing_docs)]
2#![deny(warnings)]
3#![allow(clippy::new_ret_no_self)]
4//! Rust client SDK for the AWS Lambda Runtime APIs. This crate defines
5//! a `RuntimeClient` that encapsulates interactions with AWS Lambda's Runtime
6//! APIs.
7//!
8//! To return errors to the Runtime APIs through the `event_error()` or
9//! `fail_init()` methods the `Error` objects must implement the `error::RuntimeApiError`
10//! trait from this crate. The RuntimeApiError trait defines a single method
11//! called `to_response()`. The method must return an `error::RuntimeError` object.
12//! See the `error::ApiError` object in this crate for an example.
13//!
14//! # Examples
15//!
16//! ```rust,no_run
17//! extern crate lambda_runtime_client;
18//! #[macro_use]
19//! extern crate serde_derive;
20//! extern crate serde_json;
21//!
22//! use lambda_runtime_client::{RuntimeClient, EventContext};
23//!
24//! #[derive(Serialize, Deserialize, Debug)]
25//! struct CustomEvent {
26//!     name: String,
27//! }
28//!
29//! #[derive(Serialize, Deserialize, Debug)]
30//! struct CustomResponse {
31//!     surname: String,
32//! }
33//!
34//! fn main() {
35//!     let client = RuntimeClient::new("http://localhost:8080", None, None)
36//!         .expect("Could not initialize client");
37//!
38//!     let (event_data, event_context) = client.next_event()
39//!         .expect("Could not retrieve next event");
40//!     let custom_event: CustomEvent = serde_json::from_slice(&event_data)
41//!         .expect("Could not turn Vec<u8> into CustomEvent object");
42//!
43//!     println!("Event for {}", custom_event.name);
44//!     if custom_event.name == "John" {
45//!         let resp_object = CustomResponse{ surname: String::from("Doe")};
46//!         let resp_vec = serde_json::to_vec(&resp_object)
47//!             .expect("Could not serialize CustomResponse to Vec<u8>");
48//!         client.event_response(&event_context.aws_request_id, &resp_vec)
49//!             .expect("Response sent successfully");
50//!     } else {
51//!         // return a custom error by implementing the RuntimeApiError trait.
52//!         // See the error module for examples.
53//!         //client.event_error(&event_context.aws_request_id, CustomErrorType::new("Invalid first name"))
54//!         //    .expect("Could not send error response");
55//!     }
56//! }
57//! ```
58
59mod client;
60pub mod error;
61
62pub use crate::client::*;