rusoto_personalize_runtime/
generated.rs1use std::error::Error;
14use std::fmt;
15
16use async_trait::async_trait;
17use rusoto_core::credential::ProvideAwsCredentials;
18use rusoto_core::region;
19use rusoto_core::request::{BufferedHttpResponse, DispatchSignedRequest};
20use rusoto_core::{Client, RusotoError};
21
22use rusoto_core::proto;
23use rusoto_core::signature::SignedRequest;
24#[allow(unused_imports)]
25use serde::{Deserialize, Serialize};
26use serde_json;
27#[derive(Clone, Debug, Default, PartialEq, Serialize)]
28#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
29pub struct GetPersonalizedRankingRequest {
30 #[serde(rename = "campaignArn")]
32 pub campaign_arn: String,
33 #[serde(rename = "context")]
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub context: Option<::std::collections::HashMap<String, String>>,
37 #[serde(rename = "inputList")]
39 pub input_list: Vec<String>,
40 #[serde(rename = "userId")]
42 pub user_id: String,
43}
44
45#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
46#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
47pub struct GetPersonalizedRankingResponse {
48 #[serde(rename = "personalizedRanking")]
50 #[serde(skip_serializing_if = "Option::is_none")]
51 pub personalized_ranking: Option<Vec<PredictedItem>>,
52}
53
54#[derive(Clone, Debug, Default, PartialEq, Serialize)]
55#[cfg_attr(feature = "deserialize_structs", derive(Deserialize))]
56pub struct GetRecommendationsRequest {
57 #[serde(rename = "campaignArn")]
59 pub campaign_arn: String,
60 #[serde(rename = "context")]
62 #[serde(skip_serializing_if = "Option::is_none")]
63 pub context: Option<::std::collections::HashMap<String, String>>,
64 #[serde(rename = "filterArn")]
66 #[serde(skip_serializing_if = "Option::is_none")]
67 pub filter_arn: Option<String>,
68 #[serde(rename = "itemId")]
70 #[serde(skip_serializing_if = "Option::is_none")]
71 pub item_id: Option<String>,
72 #[serde(rename = "numResults")]
74 #[serde(skip_serializing_if = "Option::is_none")]
75 pub num_results: Option<i64>,
76 #[serde(rename = "userId")]
78 #[serde(skip_serializing_if = "Option::is_none")]
79 pub user_id: Option<String>,
80}
81
82#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
83#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
84pub struct GetRecommendationsResponse {
85 #[serde(rename = "itemList")]
87 #[serde(skip_serializing_if = "Option::is_none")]
88 pub item_list: Option<Vec<PredictedItem>>,
89}
90
91#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
93#[cfg_attr(any(test, feature = "serialize_structs"), derive(Serialize))]
94pub struct PredictedItem {
95 #[serde(rename = "itemId")]
97 #[serde(skip_serializing_if = "Option::is_none")]
98 pub item_id: Option<String>,
99 #[serde(rename = "score")]
101 #[serde(skip_serializing_if = "Option::is_none")]
102 pub score: Option<f64>,
103}
104
105#[derive(Debug, PartialEq)]
107pub enum GetPersonalizedRankingError {
108 InvalidInput(String),
110 ResourceNotFound(String),
112}
113
114impl GetPersonalizedRankingError {
115 pub fn from_response(res: BufferedHttpResponse) -> RusotoError<GetPersonalizedRankingError> {
116 if let Some(err) = proto::json::Error::parse_rest(&res) {
117 match err.typ.as_str() {
118 "InvalidInputException" => {
119 return RusotoError::Service(GetPersonalizedRankingError::InvalidInput(err.msg))
120 }
121 "ResourceNotFoundException" => {
122 return RusotoError::Service(GetPersonalizedRankingError::ResourceNotFound(
123 err.msg,
124 ))
125 }
126 "ValidationException" => return RusotoError::Validation(err.msg),
127 _ => {}
128 }
129 }
130 RusotoError::Unknown(res)
131 }
132}
133impl fmt::Display for GetPersonalizedRankingError {
134 #[allow(unused_variables)]
135 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136 match *self {
137 GetPersonalizedRankingError::InvalidInput(ref cause) => write!(f, "{}", cause),
138 GetPersonalizedRankingError::ResourceNotFound(ref cause) => write!(f, "{}", cause),
139 }
140 }
141}
142impl Error for GetPersonalizedRankingError {}
143#[derive(Debug, PartialEq)]
145pub enum GetRecommendationsError {
146 InvalidInput(String),
148 ResourceNotFound(String),
150}
151
152impl GetRecommendationsError {
153 pub fn from_response(res: BufferedHttpResponse) -> RusotoError<GetRecommendationsError> {
154 if let Some(err) = proto::json::Error::parse_rest(&res) {
155 match err.typ.as_str() {
156 "InvalidInputException" => {
157 return RusotoError::Service(GetRecommendationsError::InvalidInput(err.msg))
158 }
159 "ResourceNotFoundException" => {
160 return RusotoError::Service(GetRecommendationsError::ResourceNotFound(err.msg))
161 }
162 "ValidationException" => return RusotoError::Validation(err.msg),
163 _ => {}
164 }
165 }
166 RusotoError::Unknown(res)
167 }
168}
169impl fmt::Display for GetRecommendationsError {
170 #[allow(unused_variables)]
171 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
172 match *self {
173 GetRecommendationsError::InvalidInput(ref cause) => write!(f, "{}", cause),
174 GetRecommendationsError::ResourceNotFound(ref cause) => write!(f, "{}", cause),
175 }
176 }
177}
178impl Error for GetRecommendationsError {}
179#[async_trait]
181pub trait PersonalizeRuntime {
182 async fn get_personalized_ranking(
184 &self,
185 input: GetPersonalizedRankingRequest,
186 ) -> Result<GetPersonalizedRankingResponse, RusotoError<GetPersonalizedRankingError>>;
187
188 async fn get_recommendations(
190 &self,
191 input: GetRecommendationsRequest,
192 ) -> Result<GetRecommendationsResponse, RusotoError<GetRecommendationsError>>;
193}
194#[derive(Clone)]
196pub struct PersonalizeRuntimeClient {
197 client: Client,
198 region: region::Region,
199}
200
201impl PersonalizeRuntimeClient {
202 pub fn new(region: region::Region) -> PersonalizeRuntimeClient {
206 PersonalizeRuntimeClient {
207 client: Client::shared(),
208 region,
209 }
210 }
211
212 pub fn new_with<P, D>(
213 request_dispatcher: D,
214 credentials_provider: P,
215 region: region::Region,
216 ) -> PersonalizeRuntimeClient
217 where
218 P: ProvideAwsCredentials + Send + Sync + 'static,
219 D: DispatchSignedRequest + Send + Sync + 'static,
220 {
221 PersonalizeRuntimeClient {
222 client: Client::new_with(credentials_provider, request_dispatcher),
223 region,
224 }
225 }
226
227 pub fn new_with_client(client: Client, region: region::Region) -> PersonalizeRuntimeClient {
228 PersonalizeRuntimeClient { client, region }
229 }
230}
231
232#[async_trait]
233impl PersonalizeRuntime for PersonalizeRuntimeClient {
234 #[allow(unused_mut)]
236 async fn get_personalized_ranking(
237 &self,
238 input: GetPersonalizedRankingRequest,
239 ) -> Result<GetPersonalizedRankingResponse, RusotoError<GetPersonalizedRankingError>> {
240 let request_uri = "/personalize-ranking";
241
242 let mut request = SignedRequest::new("POST", "personalize", &self.region, &request_uri);
243 request.set_content_type("application/x-amz-json-1.1".to_owned());
244
245 request.set_endpoint_prefix("personalize-runtime".to_string());
246 let encoded = Some(serde_json::to_vec(&input).unwrap());
247 request.set_payload(encoded);
248
249 let mut response = self
250 .client
251 .sign_and_dispatch(request)
252 .await
253 .map_err(RusotoError::from)?;
254 if response.status.is_success() {
255 let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
256 let result = proto::json::ResponsePayload::new(&response)
257 .deserialize::<GetPersonalizedRankingResponse, _>()?;
258
259 Ok(result)
260 } else {
261 let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
262 Err(GetPersonalizedRankingError::from_response(response))
263 }
264 }
265
266 #[allow(unused_mut)]
268 async fn get_recommendations(
269 &self,
270 input: GetRecommendationsRequest,
271 ) -> Result<GetRecommendationsResponse, RusotoError<GetRecommendationsError>> {
272 let request_uri = "/recommendations";
273
274 let mut request = SignedRequest::new("POST", "personalize", &self.region, &request_uri);
275 request.set_content_type("application/x-amz-json-1.1".to_owned());
276
277 request.set_endpoint_prefix("personalize-runtime".to_string());
278 let encoded = Some(serde_json::to_vec(&input).unwrap());
279 request.set_payload(encoded);
280
281 let mut response = self
282 .client
283 .sign_and_dispatch(request)
284 .await
285 .map_err(RusotoError::from)?;
286 if response.status.is_success() {
287 let mut response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
288 let result = proto::json::ResponsePayload::new(&response)
289 .deserialize::<GetRecommendationsResponse, _>()?;
290
291 Ok(result)
292 } else {
293 let response = response.buffer().await.map_err(RusotoError::HttpDispatch)?;
294 Err(GetRecommendationsError::from_response(response))
295 }
296 }
297}