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(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct AttributeValue {
#[serde(rename = "Value")]
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct DescribeServicesRequest {
#[serde(rename = "FormatVersion")]
#[serde(skip_serializing_if = "Option::is_none")]
pub format_version: Option<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 = "ServiceCode")]
#[serde(skip_serializing_if = "Option::is_none")]
pub service_code: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct DescribeServicesResponse {
#[serde(rename = "FormatVersion")]
#[serde(skip_serializing_if = "Option::is_none")]
pub format_version: Option<String>,
#[serde(rename = "NextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
#[serde(rename = "Services")]
#[serde(skip_serializing_if = "Option::is_none")]
pub services: Option<Vec<Service>>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct Filter {
#[serde(rename = "Field")]
pub field: String,
#[serde(rename = "Type")]
pub type_: String,
#[serde(rename = "Value")]
pub value: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct GetAttributeValuesRequest {
#[serde(rename = "AttributeName")]
pub attribute_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 = "ServiceCode")]
pub service_code: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct GetAttributeValuesResponse {
#[serde(rename = "AttributeValues")]
#[serde(skip_serializing_if = "Option::is_none")]
pub attribute_values: Option<Vec<AttributeValue>>,
#[serde(rename = "NextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
pub struct GetProductsRequest {
#[serde(rename = "Filters")]
#[serde(skip_serializing_if = "Option::is_none")]
pub filters: Option<Vec<Filter>>,
#[serde(rename = "FormatVersion")]
#[serde(skip_serializing_if = "Option::is_none")]
pub format_version: Option<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 = "ServiceCode")]
#[serde(skip_serializing_if = "Option::is_none")]
pub service_code: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct GetProductsResponse {
#[serde(rename = "FormatVersion")]
#[serde(skip_serializing_if = "Option::is_none")]
pub format_version: Option<String>,
#[serde(rename = "NextToken")]
#[serde(skip_serializing_if = "Option::is_none")]
pub next_token: Option<String>,
#[serde(rename = "PriceList")]
#[serde(skip_serializing_if = "Option::is_none")]
pub price_list: Option<Vec<String>>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
pub struct Service {
#[serde(rename = "AttributeNames")]
#[serde(skip_serializing_if = "Option::is_none")]
pub attribute_names: Option<Vec<String>>,
#[serde(rename = "ServiceCode")]
#[serde(skip_serializing_if = "Option::is_none")]
pub service_code: Option<String>,
}
#[derive(Debug, PartialEq)]
pub enum DescribeServicesError {
ExpiredNextToken(String),
InternalError(String),
InvalidNextToken(String),
InvalidParameter(String),
NotFound(String),
}
impl DescribeServicesError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<DescribeServicesError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"ExpiredNextTokenException" => {
return RusotoError::Service(DescribeServicesError::ExpiredNextToken(err.msg))
}
"InternalErrorException" => {
return RusotoError::Service(DescribeServicesError::InternalError(err.msg))
}
"InvalidNextTokenException" => {
return RusotoError::Service(DescribeServicesError::InvalidNextToken(err.msg))
}
"InvalidParameterException" => {
return RusotoError::Service(DescribeServicesError::InvalidParameter(err.msg))
}
"NotFoundException" => {
return RusotoError::Service(DescribeServicesError::NotFound(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for DescribeServicesError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DescribeServicesError::ExpiredNextToken(ref cause) => write!(f, "{}", cause),
DescribeServicesError::InternalError(ref cause) => write!(f, "{}", cause),
DescribeServicesError::InvalidNextToken(ref cause) => write!(f, "{}", cause),
DescribeServicesError::InvalidParameter(ref cause) => write!(f, "{}", cause),
DescribeServicesError::NotFound(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for DescribeServicesError {}
#[derive(Debug, PartialEq)]
pub enum GetAttributeValuesError {
ExpiredNextToken(String),
InternalError(String),
InvalidNextToken(String),
InvalidParameter(String),
NotFound(String),
}
impl GetAttributeValuesError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<GetAttributeValuesError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"ExpiredNextTokenException" => {
return RusotoError::Service(GetAttributeValuesError::ExpiredNextToken(err.msg))
}
"InternalErrorException" => {
return RusotoError::Service(GetAttributeValuesError::InternalError(err.msg))
}
"InvalidNextTokenException" => {
return RusotoError::Service(GetAttributeValuesError::InvalidNextToken(err.msg))
}
"InvalidParameterException" => {
return RusotoError::Service(GetAttributeValuesError::InvalidParameter(err.msg))
}
"NotFoundException" => {
return RusotoError::Service(GetAttributeValuesError::NotFound(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for GetAttributeValuesError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
GetAttributeValuesError::ExpiredNextToken(ref cause) => write!(f, "{}", cause),
GetAttributeValuesError::InternalError(ref cause) => write!(f, "{}", cause),
GetAttributeValuesError::InvalidNextToken(ref cause) => write!(f, "{}", cause),
GetAttributeValuesError::InvalidParameter(ref cause) => write!(f, "{}", cause),
GetAttributeValuesError::NotFound(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for GetAttributeValuesError {}
#[derive(Debug, PartialEq)]
pub enum GetProductsError {
ExpiredNextToken(String),
InternalError(String),
InvalidNextToken(String),
InvalidParameter(String),
NotFound(String),
}
impl GetProductsError {
pub fn from_response(res: BufferedHttpResponse) -> RusotoError<GetProductsError> {
if let Some(err) = proto::json::Error::parse(&res) {
match err.typ.as_str() {
"ExpiredNextTokenException" => {
return RusotoError::Service(GetProductsError::ExpiredNextToken(err.msg))
}
"InternalErrorException" => {
return RusotoError::Service(GetProductsError::InternalError(err.msg))
}
"InvalidNextTokenException" => {
return RusotoError::Service(GetProductsError::InvalidNextToken(err.msg))
}
"InvalidParameterException" => {
return RusotoError::Service(GetProductsError::InvalidParameter(err.msg))
}
"NotFoundException" => {
return RusotoError::Service(GetProductsError::NotFound(err.msg))
}
"ValidationException" => return RusotoError::Validation(err.msg),
_ => {}
}
}
RusotoError::Unknown(res)
}
}
impl fmt::Display for GetProductsError {
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
GetProductsError::ExpiredNextToken(ref cause) => write!(f, "{}", cause),
GetProductsError::InternalError(ref cause) => write!(f, "{}", cause),
GetProductsError::InvalidNextToken(ref cause) => write!(f, "{}", cause),
GetProductsError::InvalidParameter(ref cause) => write!(f, "{}", cause),
GetProductsError::NotFound(ref cause) => write!(f, "{}", cause),
}
}
}
impl Error for GetProductsError {}
#[async_trait]
pub trait Pricing {
async fn describe_services(
&self,
input: DescribeServicesRequest,
) -> Result<DescribeServicesResponse, RusotoError<DescribeServicesError>>;
async fn get_attribute_values(
&self,
input: GetAttributeValuesRequest,
) -> Result<GetAttributeValuesResponse, RusotoError<GetAttributeValuesError>>;
async fn get_products(
&self,
input: GetProductsRequest,
) -> Result<GetProductsResponse, RusotoError<GetProductsError>>;
}
#[derive(Clone)]
pub struct PricingClient {
client: Client,
region: region::Region,
}
impl PricingClient {
pub fn new(region: region::Region) -> PricingClient {
PricingClient {
client: Client::shared(),
region,
}
}
pub fn new_with<P, D>(
request_dispatcher: D,
credentials_provider: P,
region: region::Region,
) -> PricingClient
where
P: ProvideAwsCredentials + Send + Sync + 'static,
D: DispatchSignedRequest + Send + Sync + 'static,
{
PricingClient {
client: Client::new_with(credentials_provider, request_dispatcher),
region,
}
}
pub fn new_with_client(client: Client, region: region::Region) -> PricingClient {
PricingClient { client, region }
}
}
#[async_trait]
impl Pricing for PricingClient {
async fn describe_services(
&self,
input: DescribeServicesRequest,
) -> Result<DescribeServicesResponse, RusotoError<DescribeServicesError>> {
let mut request = SignedRequest::new("POST", "pricing", &self.region, "/");
request.set_endpoint_prefix("api.pricing".to_string());
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header("x-amz-target", "AWSPriceListService.DescribeServices");
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
let mut response = self
.client
.sign_and_dispatch(request)
.await
.map_err(RusotoError::from)?;
if response.status.is_success() {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
proto::json::ResponsePayload::new(&response)
.deserialize::<DescribeServicesResponse, _>()
} else {
let try_response = response.buffer().await;
let response = try_response.map_err(RusotoError::HttpDispatch)?;
Err(DescribeServicesError::from_response(response))
}
}
async fn get_attribute_values(
&self,
input: GetAttributeValuesRequest,
) -> Result<GetAttributeValuesResponse, RusotoError<GetAttributeValuesError>> {
let mut request = SignedRequest::new("POST", "pricing", &self.region, "/");
request.set_endpoint_prefix("api.pricing".to_string());
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header("x-amz-target", "AWSPriceListService.GetAttributeValues");
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
let mut response = self
.client
.sign_and_dispatch(request)
.await
.map_err(RusotoError::from)?;
if response.status.is_success() {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
proto::json::ResponsePayload::new(&response)
.deserialize::<GetAttributeValuesResponse, _>()
} else {
let try_response = response.buffer().await;
let response = try_response.map_err(RusotoError::HttpDispatch)?;
Err(GetAttributeValuesError::from_response(response))
}
}
async fn get_products(
&self,
input: GetProductsRequest,
) -> Result<GetProductsResponse, RusotoError<GetProductsError>> {
let mut request = SignedRequest::new("POST", "pricing", &self.region, "/");
request.set_endpoint_prefix("api.pricing".to_string());
request.set_content_type("application/x-amz-json-1.1".to_owned());
request.add_header("x-amz-target", "AWSPriceListService.GetProducts");
let encoded = serde_json::to_string(&input).unwrap();
request.set_payload(Some(encoded));
let mut response = self
.client
.sign_and_dispatch(request)
.await
.map_err(RusotoError::from)?;
if response.status.is_success() {
let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
proto::json::ResponsePayload::new(&response).deserialize::<GetProductsResponse, _>()
} else {
let try_response = response.buffer().await;
let response = try_response.map_err(RusotoError::HttpDispatch)?;
Err(GetProductsError::from_response(response))
}
}
}