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::proto;
use rusoto_core::signature::SignedRequest;
#[allow(unused_imports)]
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct GetIceServerConfigRequest {
#[serde(rename = "ChannelARN")]
pub channel_arn: String,
#[serde(rename = "ClientId")]
#[serde(skip_serializing_if = "Option::is_none")]
pub client_id: Option<String>,
#[serde(rename = "Service")]
#[serde(skip_serializing_if = "Option::is_none")]
pub service: Option<String>,
#[serde(rename = "Username")]
#[serde(skip_serializing_if = "Option::is_none")]
pub username: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct GetIceServerConfigResponse {
#[serde(rename = "IceServerList")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ice_server_list: Option<Vec<IceServer>>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct IceServer {
#[serde(rename = "Password")]
#[serde(skip_serializing_if = "Option::is_none")]
pub password: Option<String>,
#[serde(rename = "Ttl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ttl: Option<i64>,
#[serde(rename = "Uris")]
#[serde(skip_serializing_if = "Option::is_none")]
pub uris: Option<Vec<String>>,
#[serde(rename = "Username")]
#[serde(skip_serializing_if = "Option::is_none")]
pub username: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct SendAlexaOfferToMasterRequest {
#[serde(rename = "ChannelARN")]
pub channel_arn: String,
#[serde(rename = "MessagePayload")]
pub message_payload: String,
#[serde(rename = "SenderClientId")]
pub sender_client_id: String,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct SendAlexaOfferToMasterResponse {
#[serde(rename = "Answer")]
#[serde(skip_serializing_if = "Option::is_none")]
pub answer: Option<String>,
}
#[derive(Debug, PartialEq)]
pub enum GetIceServerConfigError {
ClientLimitExceeded(String),
InvalidArgument(String),
InvalidClient(String),
NotAuthorized(String),
ResourceNotFound(String),
SessionExpired(String),
}
impl GetIceServerConfigError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<GetIceServerConfigError> {
if let Some(err) = proto::json::Error::parse_rest(&res) {
match err.typ.as_str() {
"ClientLimitExceededException" => {
return RusotoError::Service(GetIceServerConfigError::ClientLimitExceeded(
err.msg,
))
}
"InvalidArgumentException" => {
return RusotoError::Service(GetIceServerConfigError::InvalidArgument(err.msg))
}
"InvalidClientException" => {
return RusotoError::Service(GetIceServerConfigError::InvalidClient(err.msg))
}
"NotAuthorizedException" => {
return RusotoError::Service(GetIceServerConfigError::NotAuthorized(err.msg))
}
"ResourceNotFoundException" => {
return RusotoError::Service(GetIceServerConfigError::ResourceNotFound(err.msg))
}
"SessionExpiredException" => {
return RusotoError::Service(GetIceServerConfigError::SessionExpired(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for GetIceServerConfigError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
GetIceServerConfigError::ClientLimitExceeded(ref cause) => write!(f, "{}", cause),
GetIceServerConfigError::InvalidArgument(ref cause) => write!(f, "{}", cause),
GetIceServerConfigError::InvalidClient(ref cause) => write!(f, "{}", cause),
GetIceServerConfigError::NotAuthorized(ref cause) => write!(f, "{}", cause),
GetIceServerConfigError::ResourceNotFound(ref cause) => write!(f, "{}", cause),
GetIceServerConfigError::SessionExpired(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for GetIceServerConfigError {}
#[derive(Debug, PartialEq)]
pub enum SendAlexaOfferToMasterError {
ClientLimitExceeded(String),
InvalidArgument(String),
NotAuthorized(String),
ResourceNotFound(String),
}
impl SendAlexaOfferToMasterError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<SendAlexaOfferToMasterError> {
if let Some(err) = proto::json::Error::parse_rest(&res) {
match err.typ.as_str() {
"ClientLimitExceededException" => {
return RusotoError::Service(SendAlexaOfferToMasterError::ClientLimitExceeded(
err.msg,
))
}
"InvalidArgumentException" => {
return RusotoError::Service(SendAlexaOfferToMasterError::InvalidArgument(
err.msg,
))
}
"NotAuthorizedException" => {
return RusotoError::Service(SendAlexaOfferToMasterError::NotAuthorized(
err.msg,
))
}
"ResourceNotFoundException" => {
return RusotoError::Service(SendAlexaOfferToMasterError::ResourceNotFound(
err.msg,
))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for SendAlexaOfferToMasterError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SendAlexaOfferToMasterError::ClientLimitExceeded(ref cause) => write!(f, "{}", cause),
SendAlexaOfferToMasterError::InvalidArgument(ref cause) => write!(f, "{}", cause),
SendAlexaOfferToMasterError::NotAuthorized(ref cause) => write!(f, "{}", cause),
SendAlexaOfferToMasterError::ResourceNotFound(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for SendAlexaOfferToMasterError {}
#[async_trait]
pub trait KinesisVideoSignaling {
async fn get_ice_server_config(
&self,
input: GetIceServerConfigRequest,
) -> Result<GetIceServerConfigResponse, RusotoError<GetIceServerConfigError>>;
async fn send_alexa_offer_to_master(
&self,
input: SendAlexaOfferToMasterRequest,
) -> Result<SendAlexaOfferToMasterResponse, RusotoError<SendAlexaOfferToMasterError>>;
}
#[derive(Clone)]
pub struct KinesisVideoSignalingClient {
client: Client,
region: region::Region,
}
impl KinesisVideoSignalingClient {
pub fn new(region: region::Region) -> KinesisVideoSignalingClient {
KinesisVideoSignalingClient {
client: Client::shared(),
region,
}
}
pub fn new_with<P, D>(
request_dispatcher: D,
credentials_provider: P,
region: region::Region,
) -> KinesisVideoSignalingClient
where
P: ProvideAwsCredentials + Send + Sync + 'static,
D: DispatchSignedRequest + Send + Sync + 'static,
{
KinesisVideoSignalingClient {
client: Client::new_with(credentials_provider, request_dispatcher),
region,
}
}
pub fn new_with_client(client: Client, region: region::Region) -> KinesisVideoSignalingClient {
KinesisVideoSignalingClient { client, region }
}
}
#[async_trait]
impl KinesisVideoSignaling for KinesisVideoSignalingClient {
#[allow(unused_mut)]
async fn get_ice_server_config(
&self,
input: GetIceServerConfigRequest,
) -> Result<GetIceServerConfigResponse, RusotoError<GetIceServerConfigError>> {
let request_uri = "/v1/get-ice-server-config";
let mut request = SignedRequest::new("POST", "kinesisvideo", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
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.is_success() {
let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
let result = proto::json::ResponsePayload::new(&response)
.deserialize::<GetIceServerConfigResponse, _>()?;
Ok(result)
} else {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
Err(GetIceServerConfigError::from_response(response))
}
}
#[allow(unused_mut)]
async fn send_alexa_offer_to_master(
&self,
input: SendAlexaOfferToMasterRequest,
) -> Result<SendAlexaOfferToMasterResponse, RusotoError<SendAlexaOfferToMasterError>> {
let request_uri = "/v1/send-alexa-offer-to-master";
let mut request = SignedRequest::new("POST", "kinesisvideo", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
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.is_success() {
let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
let result = proto::json::ResponsePayload::new(&response)
.deserialize::<SendAlexaOfferToMasterResponse, _>()?;
Ok(result)
} else {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
Err(SendAlexaOfferToMasterError::from_response(response))
}
}
}