use std::error::Error;
use std::fmt;
use async_trait::async_trait;
use rusoto_core::credential::ProvideAwsCredentials;
use rusoto_core::region;
use rusoto_core::request::{BufferedHttpResponse, DispatchSignedRequest};
use rusoto_core::{Client, RusotoError};
use rusoto_core::param::{Params, ServiceParams};
use rusoto_core::proto;
use rusoto_core::signature::SignedRequest;
#[allow(unused_imports)]
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct BatchPutMessageErrorEntry {
#[serde(rename = "errorCode")]
#[serde(skip_serializing_if = "Option::is_none")]
pub error_code: Option<String>,
#[serde(rename = "errorMessage")]
#[serde(skip_serializing_if = "Option::is_none")]
pub error_message: Option<String>,
#[serde(rename = "messageId")]
#[serde(skip_serializing_if = "Option::is_none")]
pub message_id: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct BatchPutMessageRequest {
#[serde(rename = "messages")]
pub messages: Vec<Message>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct BatchPutMessageResponse {
#[serde(rename = "BatchPutMessageErrorEntries")]
#[serde(skip_serializing_if = "Option::is_none")]
pub batch_put_message_error_entries: Option<Vec<BatchPutMessageErrorEntry>>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct BatchUpdateDetectorErrorEntry {
#[serde(rename = "errorCode")]
#[serde(skip_serializing_if = "Option::is_none")]
pub error_code: Option<String>,
#[serde(rename = "errorMessage")]
#[serde(skip_serializing_if = "Option::is_none")]
pub error_message: Option<String>,
#[serde(rename = "messageId")]
#[serde(skip_serializing_if = "Option::is_none")]
pub message_id: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct BatchUpdateDetectorRequest {
#[serde(rename = "detectors")]
pub detectors: Vec<UpdateDetectorRequest>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct BatchUpdateDetectorResponse {
#[serde(rename = "batchUpdateDetectorErrorEntries")]
#[serde(skip_serializing_if = "Option::is_none")]
pub batch_update_detector_error_entries: Option<Vec<BatchUpdateDetectorErrorEntry>>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct DescribeDetectorRequest {
#[serde(rename = "detectorModelName")]
pub detector_model_name: String,
#[serde(rename = "keyValue")]
#[serde(skip_serializing_if = "Option::is_none")]
pub key_value: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct DescribeDetectorResponse {
#[serde(rename = "detector")]
#[serde(skip_serializing_if = "Option::is_none")]
pub detector: Option<Detector>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct Detector {
#[serde(rename = "creationTime")]
#[serde(skip_serializing_if = "Option::is_none")]
pub creation_time: Option<f64>,
#[serde(rename = "detectorModelName")]
#[serde(skip_serializing_if = "Option::is_none")]
pub detector_model_name: Option<String>,
#[serde(rename = "detectorModelVersion")]
#[serde(skip_serializing_if = "Option::is_none")]
pub detector_model_version: Option<String>,
#[serde(rename = "keyValue")]
#[serde(skip_serializing_if = "Option::is_none")]
pub key_value: Option<String>,
#[serde(rename = "lastUpdateTime")]
#[serde(skip_serializing_if = "Option::is_none")]
pub last_update_time: Option<f64>,
#[serde(rename = "state")]
#[serde(skip_serializing_if = "Option::is_none")]
pub state: Option<DetectorState>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct DetectorState {
#[serde(rename = "stateName")]
pub state_name: String,
#[serde(rename = "timers")]
pub timers: Vec<Timer>,
#[serde(rename = "variables")]
pub variables: Vec<Variable>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct DetectorStateDefinition {
#[serde(rename = "stateName")]
pub state_name: String,
#[serde(rename = "timers")]
pub timers: Vec<TimerDefinition>,
#[serde(rename = "variables")]
pub variables: Vec<VariableDefinition>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct DetectorStateSummary {
#[serde(rename = "stateName")]
#[serde(skip_serializing_if = "Option::is_none")]
pub state_name: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct DetectorSummary {
#[serde(rename = "creationTime")]
#[serde(skip_serializing_if = "Option::is_none")]
pub creation_time: Option<f64>,
#[serde(rename = "detectorModelName")]
#[serde(skip_serializing_if = "Option::is_none")]
pub detector_model_name: Option<String>,
#[serde(rename = "detectorModelVersion")]
#[serde(skip_serializing_if = "Option::is_none")]
pub detector_model_version: Option<String>,
#[serde(rename = "keyValue")]
#[serde(skip_serializing_if = "Option::is_none")]
pub key_value: Option<String>,
#[serde(rename = "lastUpdateTime")]
#[serde(skip_serializing_if = "Option::is_none")]
pub last_update_time: Option<f64>,
#[serde(rename = "state")]
#[serde(skip_serializing_if = "Option::is_none")]
pub state: Option<DetectorStateSummary>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct ListDetectorsRequest {
#[serde(rename = "detectorModelName")]
pub detector_model_name: String,
#[serde(rename = "maxResults")]
#[serde(skip_serializing_if = "Option::is_none")]
pub max_results: Option<i64>,
#[serde(rename = "nextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
#[serde(rename = "stateName")]
#[serde(skip_serializing_if = "Option::is_none")]
pub state_name: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct ListDetectorsResponse {
#[serde(rename = "detectorSummaries")]
#[serde(skip_serializing_if = "Option::is_none")]
pub detector_summaries: Option<Vec<DetectorSummary>>,
#[serde(rename = "nextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct Message {
#[serde(rename = "inputName")]
pub input_name: String,
#[serde(rename = "messageId")]
pub message_id: String,
#[serde(rename = "payload")]
#[serde(
deserialize_with = "::rusoto_core::serialization::SerdeBlob::deserialize_blob",
serialize_with = "::rusoto_core::serialization::SerdeBlob::serialize_blob",
default
)]
pub payload: bytes::Bytes,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct Timer {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "timestamp")]
pub timestamp: f64,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct TimerDefinition {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "seconds")]
pub seconds: i64,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct UpdateDetectorRequest {
#[serde(rename = "detectorModelName")]
pub detector_model_name: String,
#[serde(rename = "keyValue")]
#[serde(skip_serializing_if = "Option::is_none")]
pub key_value: Option<String>,
#[serde(rename = "messageId")]
pub message_id: String,
#[serde(rename = "state")]
pub state: DetectorStateDefinition,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct Variable {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "value")]
pub value: String,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct VariableDefinition {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "value")]
pub value: String,
}
#[derive(Debug, PartialEq)]
pub enum BatchPutMessageError {
InternalFailure(String),
InvalidRequest(String),
ServiceUnavailable(String),
Throttling(String),
}
impl BatchPutMessageError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<BatchPutMessageError> {
if let Some(err) = proto::json::Error::parse_rest(&res) {
match err.typ.as_str() {
"InternalFailureException" => {
return RusotoError::Service(BatchPutMessageError::InternalFailure(err.msg))
}
"InvalidRequestException" => {
return RusotoError::Service(BatchPutMessageError::InvalidRequest(err.msg))
}
"ServiceUnavailableException" => {
return RusotoError::Service(BatchPutMessageError::ServiceUnavailable(err.msg))
}
"ThrottlingException" => {
return RusotoError::Service(BatchPutMessageError::Throttling(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for BatchPutMessageError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BatchPutMessageError::InternalFailure(ref cause) => write!(f, "{}", cause),
BatchPutMessageError::InvalidRequest(ref cause) => write!(f, "{}", cause),
BatchPutMessageError::ServiceUnavailable(ref cause) => write!(f, "{}", cause),
BatchPutMessageError::Throttling(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for BatchPutMessageError {}
#[derive(Debug, PartialEq)]
pub enum BatchUpdateDetectorError {
InternalFailure(String),
InvalidRequest(String),
ServiceUnavailable(String),
Throttling(String),
}
impl BatchUpdateDetectorError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<BatchUpdateDetectorError> {
if let Some(err) = proto::json::Error::parse_rest(&res) {
match err.typ.as_str() {
"InternalFailureException" => {
return RusotoError::Service(BatchUpdateDetectorError::InternalFailure(err.msg))
}
"InvalidRequestException" => {
return RusotoError::Service(BatchUpdateDetectorError::InvalidRequest(err.msg))
}
"ServiceUnavailableException" => {
return RusotoError::Service(BatchUpdateDetectorError::ServiceUnavailable(
err.msg,
))
}
"ThrottlingException" => {
return RusotoError::Service(BatchUpdateDetectorError::Throttling(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for BatchUpdateDetectorError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BatchUpdateDetectorError::InternalFailure(ref cause) => write!(f, "{}", cause),
BatchUpdateDetectorError::InvalidRequest(ref cause) => write!(f, "{}", cause),
BatchUpdateDetectorError::ServiceUnavailable(ref cause) => write!(f, "{}", cause),
BatchUpdateDetectorError::Throttling(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for BatchUpdateDetectorError {}
#[derive(Debug, PartialEq)]
pub enum DescribeDetectorError {
InternalFailure(String),
InvalidRequest(String),
ResourceNotFound(String),
ServiceUnavailable(String),
Throttling(String),
}
impl DescribeDetectorError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<DescribeDetectorError> {
if let Some(err) = proto::json::Error::parse_rest(&res) {
match err.typ.as_str() {
"InternalFailureException" => {
return RusotoError::Service(DescribeDetectorError::InternalFailure(err.msg))
}
"InvalidRequestException" => {
return RusotoError::Service(DescribeDetectorError::InvalidRequest(err.msg))
}
"ResourceNotFoundException" => {
return RusotoError::Service(DescribeDetectorError::ResourceNotFound(err.msg))
}
"ServiceUnavailableException" => {
return RusotoError::Service(DescribeDetectorError::ServiceUnavailable(err.msg))
}
"ThrottlingException" => {
return RusotoError::Service(DescribeDetectorError::Throttling(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for DescribeDetectorError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DescribeDetectorError::InternalFailure(ref cause) => write!(f, "{}", cause),
DescribeDetectorError::InvalidRequest(ref cause) => write!(f, "{}", cause),
DescribeDetectorError::ResourceNotFound(ref cause) => write!(f, "{}", cause),
DescribeDetectorError::ServiceUnavailable(ref cause) => write!(f, "{}", cause),
DescribeDetectorError::Throttling(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for DescribeDetectorError {}
#[derive(Debug, PartialEq)]
pub enum ListDetectorsError {
InternalFailure(String),
InvalidRequest(String),
ResourceNotFound(String),
ServiceUnavailable(String),
Throttling(String),
}
impl ListDetectorsError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<ListDetectorsError> {
if let Some(err) = proto::json::Error::parse_rest(&res) {
match err.typ.as_str() {
"InternalFailureException" => {
return RusotoError::Service(ListDetectorsError::InternalFailure(err.msg))
}
"InvalidRequestException" => {
return RusotoError::Service(ListDetectorsError::InvalidRequest(err.msg))
}
"ResourceNotFoundException" => {
return RusotoError::Service(ListDetectorsError::ResourceNotFound(err.msg))
}
"ServiceUnavailableException" => {
return RusotoError::Service(ListDetectorsError::ServiceUnavailable(err.msg))
}
"ThrottlingException" => {
return RusotoError::Service(ListDetectorsError::Throttling(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for ListDetectorsError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ListDetectorsError::InternalFailure(ref cause) => write!(f, "{}", cause),
ListDetectorsError::InvalidRequest(ref cause) => write!(f, "{}", cause),
ListDetectorsError::ResourceNotFound(ref cause) => write!(f, "{}", cause),
ListDetectorsError::ServiceUnavailable(ref cause) => write!(f, "{}", cause),
ListDetectorsError::Throttling(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for ListDetectorsError {}
#[async_trait]
pub trait IotEventsData {
async fn batch_put_message(
&self,
input: BatchPutMessageRequest,
) -> Result<BatchPutMessageResponse, RusotoError<BatchPutMessageError>>;
async fn batch_update_detector(
&self,
input: BatchUpdateDetectorRequest,
) -> Result<BatchUpdateDetectorResponse, RusotoError<BatchUpdateDetectorError>>;
async fn describe_detector(
&self,
input: DescribeDetectorRequest,
) -> Result<DescribeDetectorResponse, RusotoError<DescribeDetectorError>>;
async fn list_detectors(
&self,
input: ListDetectorsRequest,
) -> Result<ListDetectorsResponse, RusotoError<ListDetectorsError>>;
}
#[derive(Clone)]
pub struct IotEventsDataClient {
client: Client,
region: region::Region,
}
impl IotEventsDataClient {
pub fn new(region: region::Region) -> IotEventsDataClient {
IotEventsDataClient {
client: Client::shared(),
region,
}
}
pub fn new_with<P, D>(
request_dispatcher: D,
credentials_provider: P,
region: region::Region,
) -> IotEventsDataClient
where
P: ProvideAwsCredentials + Send + Sync + 'static,
D: DispatchSignedRequest + Send + Sync + 'static,
{
IotEventsDataClient {
client: Client::new_with(credentials_provider, request_dispatcher),
region,
}
}
pub fn new_with_client(client: Client, region: region::Region) -> IotEventsDataClient {
IotEventsDataClient { client, region }
}
}
#[async_trait]
impl IotEventsData for IotEventsDataClient {
#[allow(unused_mut)]
async fn batch_put_message(
&self,
input: BatchPutMessageRequest,
) -> Result<BatchPutMessageResponse, RusotoError<BatchPutMessageError>> {
let request_uri = "/inputs/messages";
let mut request = SignedRequest::new("POST", "ioteventsdata", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("data.iotevents".to_string());
let encoded = Some(serde_json::to_vec(&input).unwrap());
request.set_payload(encoded);
let mut response = self
.client
.sign_and_dispatch(request)
.await
.map_err(RusotoError::from)?;
if response.status.as_u16() == 200 {
let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
let result = proto::json::ResponsePayload::new(&response)
.deserialize::<BatchPutMessageResponse, _>()?;
Ok(result)
} else {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
Err(BatchPutMessageError::from_response(response))
}
}
#[allow(unused_mut)]
async fn batch_update_detector(
&self,
input: BatchUpdateDetectorRequest,
) -> Result<BatchUpdateDetectorResponse, RusotoError<BatchUpdateDetectorError>> {
let request_uri = "/detectors";
let mut request = SignedRequest::new("POST", "ioteventsdata", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("data.iotevents".to_string());
let encoded = Some(serde_json::to_vec(&input).unwrap());
request.set_payload(encoded);
let mut response = self
.client
.sign_and_dispatch(request)
.await
.map_err(RusotoError::from)?;
if response.status.as_u16() == 200 {
let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
let result = proto::json::ResponsePayload::new(&response)
.deserialize::<BatchUpdateDetectorResponse, _>()?;
Ok(result)
} else {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
Err(BatchUpdateDetectorError::from_response(response))
}
}
#[allow(unused_mut)]
async fn describe_detector(
&self,
input: DescribeDetectorRequest,
) -> Result<DescribeDetectorResponse, RusotoError<DescribeDetectorError>> {
let request_uri = format!(
"/detectors/{detector_model_name}/keyValues/",
detector_model_name = input.detector_model_name
);
let mut request = SignedRequest::new("GET", "ioteventsdata", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("data.iotevents".to_string());
let mut params = Params::new();
if let Some(ref x) = input.key_value {
params.put("keyValue", x);
}
request.set_params(params);
let mut response = self
.client
.sign_and_dispatch(request)
.await
.map_err(RusotoError::from)?;
if response.status.is_success() {
let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
let result = proto::json::ResponsePayload::new(&response)
.deserialize::<DescribeDetectorResponse, _>()?;
Ok(result)
} else {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
Err(DescribeDetectorError::from_response(response))
}
}
#[allow(unused_mut)]
async fn list_detectors(
&self,
input: ListDetectorsRequest,
) -> Result<ListDetectorsResponse, RusotoError<ListDetectorsError>> {
let request_uri = format!(
"/detectors/{detector_model_name}",
detector_model_name = input.detector_model_name
);
let mut request = SignedRequest::new("GET", "ioteventsdata", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("data.iotevents".to_string());
let mut params = Params::new();
if let Some(ref x) = input.max_results {
params.put("maxResults", x);
}
if let Some(ref x) = input.next_token {
params.put("nextToken", x);
}
if let Some(ref x) = input.state_name {
params.put("stateName", x);
}
request.set_params(params);
let mut response = self
.client
.sign_and_dispatch(request)
.await
.map_err(RusotoError::from)?;
if response.status.is_success() {
let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
let result = proto::json::ResponsePayload::new(&response)
.deserialize::<ListDetectorsResponse, _>()?;
Ok(result)
} else {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
Err(ListDetectorsError::from_response(response))
}
}
}