use std::error::Error;
use std::fmt;
use std::io;
#[allow(warnings)]
use futures::future;
use futures::Future;
use rusoto_core::region;
use rusoto_core::request::{BufferedHttpResponse, DispatchSignedRequest};
use rusoto_core::{Client, RusotoFuture};
use rusoto_core::credential::{CredentialsError, ProvideAwsCredentials};
use rusoto_core::request::HttpDispatchError;
use rusoto_core::param::{Params, ServiceParams};
use rusoto_core::signature::SignedRequest;
use serde_json;
use serde_json::from_slice;
use serde_json::Value as SerdeJsonValue;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CdnConfiguration {
#[serde(rename = "AdSegmentUrlPrefix")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ad_segment_url_prefix: Option<String>,
#[serde(rename = "ContentSegmentUrlPrefix")]
#[serde(skip_serializing_if = "Option::is_none")]
pub content_segment_url_prefix: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct DeletePlaybackConfigurationRequest {
#[serde(rename = "Name")]
pub name: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct DeletePlaybackConfigurationResponse {}
#[derive(Default, Debug, Clone, PartialEq)]
pub struct Empty {}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct GetPlaybackConfigurationRequest {
#[serde(rename = "Name")]
pub name: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct GetPlaybackConfigurationResponse {
#[serde(rename = "AdDecisionServerUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ad_decision_server_url: Option<String>,
#[serde(rename = "CdnConfiguration")]
#[serde(skip_serializing_if = "Option::is_none")]
pub cdn_configuration: Option<CdnConfiguration>,
#[serde(rename = "HlsConfiguration")]
#[serde(skip_serializing_if = "Option::is_none")]
pub hls_configuration: Option<HlsConfiguration>,
#[serde(rename = "Name")]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(rename = "PlaybackEndpointPrefix")]
#[serde(skip_serializing_if = "Option::is_none")]
pub playback_endpoint_prefix: Option<String>,
#[serde(rename = "SessionInitializationEndpointPrefix")]
#[serde(skip_serializing_if = "Option::is_none")]
pub session_initialization_endpoint_prefix: Option<String>,
#[serde(rename = "SlateAdUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub slate_ad_url: Option<String>,
#[serde(rename = "VideoContentSourceUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub video_content_source_url: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct HlsConfiguration {
#[serde(rename = "ManifestEndpointPrefix")]
#[serde(skip_serializing_if = "Option::is_none")]
pub manifest_endpoint_prefix: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct ListPlaybackConfigurationsRequest {
#[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>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct ListPlaybackConfigurationsResponse {
#[serde(rename = "Items")]
#[serde(skip_serializing_if = "Option::is_none")]
pub items: Option<Vec<PlaybackConfiguration>>,
#[serde(rename = "NextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct PlaybackConfiguration {
#[serde(rename = "AdDecisionServerUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ad_decision_server_url: Option<String>,
#[serde(rename = "CdnConfiguration")]
#[serde(skip_serializing_if = "Option::is_none")]
pub cdn_configuration: Option<CdnConfiguration>,
#[serde(rename = "Name")]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(rename = "SlateAdUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub slate_ad_url: Option<String>,
#[serde(rename = "VideoContentSourceUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub video_content_source_url: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct PutPlaybackConfigurationRequest {
#[serde(rename = "AdDecisionServerUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ad_decision_server_url: Option<String>,
#[serde(rename = "CdnConfiguration")]
#[serde(skip_serializing_if = "Option::is_none")]
pub cdn_configuration: Option<CdnConfiguration>,
#[serde(rename = "Name")]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(rename = "SlateAdUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub slate_ad_url: Option<String>,
#[serde(rename = "VideoContentSourceUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub video_content_source_url: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct PutPlaybackConfigurationResponse {
#[serde(rename = "AdDecisionServerUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ad_decision_server_url: Option<String>,
#[serde(rename = "CdnConfiguration")]
#[serde(skip_serializing_if = "Option::is_none")]
pub cdn_configuration: Option<CdnConfiguration>,
#[serde(rename = "HlsConfiguration")]
#[serde(skip_serializing_if = "Option::is_none")]
pub hls_configuration: Option<HlsConfiguration>,
#[serde(rename = "Name")]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(rename = "PlaybackEndpointPrefix")]
#[serde(skip_serializing_if = "Option::is_none")]
pub playback_endpoint_prefix: Option<String>,
#[serde(rename = "SessionInitializationEndpointPrefix")]
#[serde(skip_serializing_if = "Option::is_none")]
pub session_initialization_endpoint_prefix: Option<String>,
#[serde(rename = "SlateAdUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub slate_ad_url: Option<String>,
#[serde(rename = "VideoContentSourceUrl")]
#[serde(skip_serializing_if = "Option::is_none")]
pub video_content_source_url: Option<String>,
}
#[derive(Debug, PartialEq)]
pub enum DeletePlaybackConfigurationError {
HttpDispatch(HttpDispatchError),
Credentials(CredentialsError),
Validation(String),
ParseError(String),
Unknown(BufferedHttpResponse),
}
impl DeletePlaybackConfigurationError {
pub fn from_response(res: BufferedHttpResponse) -> DeletePlaybackConfigurationError {
if let Ok(json) = from_slice::<SerdeJsonValue>(&res.body) {
let error_type = match res.headers.get("x-amzn-errortype") {
Some(raw_error_type) => raw_error_type
.split(':')
.next()
.unwrap_or_else(|| "Unknown"),
_ => json
.get("code")
.or_else(|| json.get("Code"))
.and_then(|c| c.as_str())
.unwrap_or_else(|| "Unknown"),
};
let error_message = json
.get("message")
.or_else(|| json.get("Message"))
.and_then(|m| m.as_str())
.unwrap_or("");
match error_type {
"ValidationException" => {
return DeletePlaybackConfigurationError::Validation(error_message.to_string())
}
_ => {}
}
}
return DeletePlaybackConfigurationError::Unknown(res);
}
}
impl From<serde_json::error::Error> for DeletePlaybackConfigurationError {
fn from(err: serde_json::error::Error) -> DeletePlaybackConfigurationError {
DeletePlaybackConfigurationError::ParseError(err.description().to_string())
}
}
impl From<CredentialsError> for DeletePlaybackConfigurationError {
fn from(err: CredentialsError) -> DeletePlaybackConfigurationError {
DeletePlaybackConfigurationError::Credentials(err)
}
}
impl From<HttpDispatchError> for DeletePlaybackConfigurationError {
fn from(err: HttpDispatchError) -> DeletePlaybackConfigurationError {
DeletePlaybackConfigurationError::HttpDispatch(err)
}
}
impl From<io::Error> for DeletePlaybackConfigurationError {
fn from(err: io::Error) -> DeletePlaybackConfigurationError {
DeletePlaybackConfigurationError::HttpDispatch(HttpDispatchError::from(err))
}
}
impl fmt::Display for DeletePlaybackConfigurationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for DeletePlaybackConfigurationError {
fn description(&self) -> &str {
match *self {
DeletePlaybackConfigurationError::Validation(ref cause) => cause,
DeletePlaybackConfigurationError::Credentials(ref err) => err.description(),
DeletePlaybackConfigurationError::HttpDispatch(ref dispatch_error) => {
dispatch_error.description()
}
DeletePlaybackConfigurationError::ParseError(ref cause) => cause,
DeletePlaybackConfigurationError::Unknown(_) => "unknown error",
}
}
}
#[derive(Debug, PartialEq)]
pub enum GetPlaybackConfigurationError {
HttpDispatch(HttpDispatchError),
Credentials(CredentialsError),
Validation(String),
ParseError(String),
Unknown(BufferedHttpResponse),
}
impl GetPlaybackConfigurationError {
pub fn from_response(res: BufferedHttpResponse) -> GetPlaybackConfigurationError {
if let Ok(json) = from_slice::<SerdeJsonValue>(&res.body) {
let error_type = match res.headers.get("x-amzn-errortype") {
Some(raw_error_type) => raw_error_type
.split(':')
.next()
.unwrap_or_else(|| "Unknown"),
_ => json
.get("code")
.or_else(|| json.get("Code"))
.and_then(|c| c.as_str())
.unwrap_or_else(|| "Unknown"),
};
let error_message = json
.get("message")
.or_else(|| json.get("Message"))
.and_then(|m| m.as_str())
.unwrap_or("");
match error_type {
"ValidationException" => {
return GetPlaybackConfigurationError::Validation(error_message.to_string())
}
_ => {}
}
}
return GetPlaybackConfigurationError::Unknown(res);
}
}
impl From<serde_json::error::Error> for GetPlaybackConfigurationError {
fn from(err: serde_json::error::Error) -> GetPlaybackConfigurationError {
GetPlaybackConfigurationError::ParseError(err.description().to_string())
}
}
impl From<CredentialsError> for GetPlaybackConfigurationError {
fn from(err: CredentialsError) -> GetPlaybackConfigurationError {
GetPlaybackConfigurationError::Credentials(err)
}
}
impl From<HttpDispatchError> for GetPlaybackConfigurationError {
fn from(err: HttpDispatchError) -> GetPlaybackConfigurationError {
GetPlaybackConfigurationError::HttpDispatch(err)
}
}
impl From<io::Error> for GetPlaybackConfigurationError {
fn from(err: io::Error) -> GetPlaybackConfigurationError {
GetPlaybackConfigurationError::HttpDispatch(HttpDispatchError::from(err))
}
}
impl fmt::Display for GetPlaybackConfigurationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for GetPlaybackConfigurationError {
fn description(&self) -> &str {
match *self {
GetPlaybackConfigurationError::Validation(ref cause) => cause,
GetPlaybackConfigurationError::Credentials(ref err) => err.description(),
GetPlaybackConfigurationError::HttpDispatch(ref dispatch_error) => {
dispatch_error.description()
}
GetPlaybackConfigurationError::ParseError(ref cause) => cause,
GetPlaybackConfigurationError::Unknown(_) => "unknown error",
}
}
}
#[derive(Debug, PartialEq)]
pub enum ListPlaybackConfigurationsError {
HttpDispatch(HttpDispatchError),
Credentials(CredentialsError),
Validation(String),
ParseError(String),
Unknown(BufferedHttpResponse),
}
impl ListPlaybackConfigurationsError {
pub fn from_response(res: BufferedHttpResponse) -> ListPlaybackConfigurationsError {
if let Ok(json) = from_slice::<SerdeJsonValue>(&res.body) {
let error_type = match res.headers.get("x-amzn-errortype") {
Some(raw_error_type) => raw_error_type
.split(':')
.next()
.unwrap_or_else(|| "Unknown"),
_ => json
.get("code")
.or_else(|| json.get("Code"))
.and_then(|c| c.as_str())
.unwrap_or_else(|| "Unknown"),
};
let error_message = json
.get("message")
.or_else(|| json.get("Message"))
.and_then(|m| m.as_str())
.unwrap_or("");
match error_type {
"ValidationException" => {
return ListPlaybackConfigurationsError::Validation(error_message.to_string())
}
_ => {}
}
}
return ListPlaybackConfigurationsError::Unknown(res);
}
}
impl From<serde_json::error::Error> for ListPlaybackConfigurationsError {
fn from(err: serde_json::error::Error) -> ListPlaybackConfigurationsError {
ListPlaybackConfigurationsError::ParseError(err.description().to_string())
}
}
impl From<CredentialsError> for ListPlaybackConfigurationsError {
fn from(err: CredentialsError) -> ListPlaybackConfigurationsError {
ListPlaybackConfigurationsError::Credentials(err)
}
}
impl From<HttpDispatchError> for ListPlaybackConfigurationsError {
fn from(err: HttpDispatchError) -> ListPlaybackConfigurationsError {
ListPlaybackConfigurationsError::HttpDispatch(err)
}
}
impl From<io::Error> for ListPlaybackConfigurationsError {
fn from(err: io::Error) -> ListPlaybackConfigurationsError {
ListPlaybackConfigurationsError::HttpDispatch(HttpDispatchError::from(err))
}
}
impl fmt::Display for ListPlaybackConfigurationsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for ListPlaybackConfigurationsError {
fn description(&self) -> &str {
match *self {
ListPlaybackConfigurationsError::Validation(ref cause) => cause,
ListPlaybackConfigurationsError::Credentials(ref err) => err.description(),
ListPlaybackConfigurationsError::HttpDispatch(ref dispatch_error) => {
dispatch_error.description()
}
ListPlaybackConfigurationsError::ParseError(ref cause) => cause,
ListPlaybackConfigurationsError::Unknown(_) => "unknown error",
}
}
}
#[derive(Debug, PartialEq)]
pub enum PutPlaybackConfigurationError {
HttpDispatch(HttpDispatchError),
Credentials(CredentialsError),
Validation(String),
ParseError(String),
Unknown(BufferedHttpResponse),
}
impl PutPlaybackConfigurationError {
pub fn from_response(res: BufferedHttpResponse) -> PutPlaybackConfigurationError {
if let Ok(json) = from_slice::<SerdeJsonValue>(&res.body) {
let error_type = match res.headers.get("x-amzn-errortype") {
Some(raw_error_type) => raw_error_type
.split(':')
.next()
.unwrap_or_else(|| "Unknown"),
_ => json
.get("code")
.or_else(|| json.get("Code"))
.and_then(|c| c.as_str())
.unwrap_or_else(|| "Unknown"),
};
let error_message = json
.get("message")
.or_else(|| json.get("Message"))
.and_then(|m| m.as_str())
.unwrap_or("");
match error_type {
"ValidationException" => {
return PutPlaybackConfigurationError::Validation(error_message.to_string())
}
_ => {}
}
}
return PutPlaybackConfigurationError::Unknown(res);
}
}
impl From<serde_json::error::Error> for PutPlaybackConfigurationError {
fn from(err: serde_json::error::Error) -> PutPlaybackConfigurationError {
PutPlaybackConfigurationError::ParseError(err.description().to_string())
}
}
impl From<CredentialsError> for PutPlaybackConfigurationError {
fn from(err: CredentialsError) -> PutPlaybackConfigurationError {
PutPlaybackConfigurationError::Credentials(err)
}
}
impl From<HttpDispatchError> for PutPlaybackConfigurationError {
fn from(err: HttpDispatchError) -> PutPlaybackConfigurationError {
PutPlaybackConfigurationError::HttpDispatch(err)
}
}
impl From<io::Error> for PutPlaybackConfigurationError {
fn from(err: io::Error) -> PutPlaybackConfigurationError {
PutPlaybackConfigurationError::HttpDispatch(HttpDispatchError::from(err))
}
}
impl fmt::Display for PutPlaybackConfigurationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl Error for PutPlaybackConfigurationError {
fn description(&self) -> &str {
match *self {
PutPlaybackConfigurationError::Validation(ref cause) => cause,
PutPlaybackConfigurationError::Credentials(ref err) => err.description(),
PutPlaybackConfigurationError::HttpDispatch(ref dispatch_error) => {
dispatch_error.description()
}
PutPlaybackConfigurationError::ParseError(ref cause) => cause,
PutPlaybackConfigurationError::Unknown(_) => "unknown error",
}
}
}
pub trait MediaTailor {
fn delete_playback_configuration(
&self,
input: DeletePlaybackConfigurationRequest,
) -> RusotoFuture<DeletePlaybackConfigurationResponse, DeletePlaybackConfigurationError>;
fn get_playback_configuration(
&self,
input: GetPlaybackConfigurationRequest,
) -> RusotoFuture<GetPlaybackConfigurationResponse, GetPlaybackConfigurationError>;
fn list_playback_configurations(
&self,
input: ListPlaybackConfigurationsRequest,
) -> RusotoFuture<ListPlaybackConfigurationsResponse, ListPlaybackConfigurationsError>;
fn put_playback_configuration(
&self,
input: PutPlaybackConfigurationRequest,
) -> RusotoFuture<PutPlaybackConfigurationResponse, PutPlaybackConfigurationError>;
}
pub struct MediaTailorClient {
client: Client,
region: region::Region,
}
impl MediaTailorClient {
pub fn new(region: region::Region) -> MediaTailorClient {
MediaTailorClient {
client: Client::shared(),
region: region,
}
}
pub fn new_with<P, D>(
request_dispatcher: D,
credentials_provider: P,
region: region::Region,
) -> MediaTailorClient
where
P: ProvideAwsCredentials + Send + Sync + 'static,
P::Future: Send,
D: DispatchSignedRequest + Send + Sync + 'static,
D::Future: Send,
{
MediaTailorClient {
client: Client::new_with(credentials_provider, request_dispatcher),
region: region,
}
}
}
impl MediaTailor for MediaTailorClient {
fn delete_playback_configuration(
&self,
input: DeletePlaybackConfigurationRequest,
) -> RusotoFuture<DeletePlaybackConfigurationResponse, DeletePlaybackConfigurationError> {
let request_uri = format!("/playbackConfiguration/{name}", name = input.name);
let mut request = SignedRequest::new("DELETE", "mediatailor", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("api.mediatailor".to_string());
self.client.sign_and_dispatch(request, |response| {
if response.status.as_u16() == 204 {
Box::new(response.buffer().from_err().map(|response| {
let mut body = response.body;
if body == b"null" || body.is_empty() {
body = b"{}".to_vec();
}
debug!("Response body: {:?}", body);
debug!("Response status: {}", response.status);
let result =
serde_json::from_slice::<DeletePlaybackConfigurationResponse>(&body)
.unwrap();
result
}))
} else {
Box::new(response.buffer().from_err().and_then(|response| {
Err(DeletePlaybackConfigurationError::from_response(response))
}))
}
})
}
fn get_playback_configuration(
&self,
input: GetPlaybackConfigurationRequest,
) -> RusotoFuture<GetPlaybackConfigurationResponse, GetPlaybackConfigurationError> {
let request_uri = format!("/playbackConfiguration/{name}", name = input.name);
let mut request = SignedRequest::new("GET", "mediatailor", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("api.mediatailor".to_string());
self.client.sign_and_dispatch(request, |response| {
if response.status.as_u16() == 200 {
Box::new(response.buffer().from_err().map(|response| {
let mut body = response.body;
if body == b"null" || body.is_empty() {
body = b"{}".to_vec();
}
debug!("Response body: {:?}", body);
debug!("Response status: {}", response.status);
let result =
serde_json::from_slice::<GetPlaybackConfigurationResponse>(&body).unwrap();
result
}))
} else {
Box::new(response.buffer().from_err().and_then(|response| {
Err(GetPlaybackConfigurationError::from_response(response))
}))
}
})
}
fn list_playback_configurations(
&self,
input: ListPlaybackConfigurationsRequest,
) -> RusotoFuture<ListPlaybackConfigurationsResponse, ListPlaybackConfigurationsError> {
let request_uri = "/playbackConfigurations";
let mut request = SignedRequest::new("GET", "mediatailor", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("api.mediatailor".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);
}
request.set_params(params);
self.client.sign_and_dispatch(request, |response| {
if response.status.as_u16() == 200 {
Box::new(response.buffer().from_err().map(|response| {
let mut body = response.body;
if body == b"null" || body.is_empty() {
body = b"{}".to_vec();
}
debug!("Response body: {:?}", body);
debug!("Response status: {}", response.status);
let result =
serde_json::from_slice::<ListPlaybackConfigurationsResponse>(&body)
.unwrap();
result
}))
} else {
Box::new(response.buffer().from_err().and_then(|response| {
Err(ListPlaybackConfigurationsError::from_response(response))
}))
}
})
}
fn put_playback_configuration(
&self,
input: PutPlaybackConfigurationRequest,
) -> RusotoFuture<PutPlaybackConfigurationResponse, PutPlaybackConfigurationError> {
let request_uri = "/playbackConfiguration";
let mut request = SignedRequest::new("PUT", "mediatailor", &self.region, &request_uri);
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.set_endpoint_prefix("api.mediatailor".to_string());
let encoded = Some(serde_json::to_vec(&input).unwrap());
request.set_payload(encoded);
self.client.sign_and_dispatch(request, |response| {
if response.status.as_u16() == 200 {
Box::new(response.buffer().from_err().map(|response| {
let mut body = response.body;
if body == b"null" || body.is_empty() {
body = b"{}".to_vec();
}
debug!("Response body: {:?}", body);
debug!("Response status: {}", response.status);
let result =
serde_json::from_slice::<PutPlaybackConfigurationResponse>(&body).unwrap();
result
}))
} else {
Box::new(response.buffer().from_err().and_then(|response| {
Err(PutPlaybackConfigurationError::from_response(response))
}))
}
})
}
}
#[cfg(test)]
mod protocol_tests {}