diff --git a/modules/swagger-codegen/src/main/resources/rust/api.mustache b/modules/swagger-codegen/src/main/resources/rust/api.mustache index 518908b2aa6..ad9b3f26533 100644 --- a/modules/swagger-codegen/src/main/resources/rust/api.mustache +++ b/modules/swagger-codegen/src/main/resources/rust/api.mustache @@ -27,7 +27,7 @@ impl {{{classname}}}Client { pub trait {{classname}} { {{#operations}} {{#operation}} - fn {{{operationId}}}(&self, {{#allParams}}{{paramName}}: {{#isString}}&str{{/isString}}{{^isString}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Box>; + fn {{{operationId}}}(&self, {{#allParams}}{{paramName}}: {{#isString}}&str{{/isString}}{{^isString}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Box>>; {{/operation}} {{/operations}} } @@ -36,7 +36,7 @@ pub trait {{classname}} { impl{{classname}} for {{classname}}Client { {{#operations}} {{#operation}} - fn {{{operationId}}}(&self, {{#allParams}}{{paramName}}: {{#isString}}&str{{/isString}}{{^isString}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Box> { + fn {{{operationId}}}(&self, {{#allParams}}{{paramName}}: {{#isString}}&str{{/isString}}{{^isString}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::{{httpMethod}}; @@ -84,8 +84,21 @@ impl{{classname}} for {{classname}}Client { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) {{^returnType}} .and_then(|_| futures::future::ok(())) {{/returnType}} @@ -93,7 +106,7 @@ impl{{classname}} for {{classname}}Client { .and_then(|body| { let parsed: Result<{{{returnType}}}, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) {{/returnType}} ) } diff --git a/modules/swagger-codegen/src/main/resources/rust/api_mod.mustache b/modules/swagger-codegen/src/main/resources/rust/api_mod.mustache index 5b62b897cdd..5c8f6b86a4d 100644 --- a/modules/swagger-codegen/src/main/resources/rust/api_mod.mustache +++ b/modules/swagger-codegen/src/main/resources/rust/api_mod.mustache @@ -1,19 +1,48 @@ use hyper; +use serde; use serde_json; #[derive(Debug)] -pub enum Error { +pub enum Error { Hyper(hyper::Error), Serde(serde_json::Error), + ApiError(ApiError), } -impl From for Error { +#[derive(Debug)] +pub struct ApiError { + pub code: hyper::StatusCode, + pub content: Option, +} + +impl<'de, T> From<(hyper::StatusCode, &'de [u8])> for Error + where T: serde::Deserialize<'de> { + fn from(e: (hyper::StatusCode, &'de [u8])) -> Self { + if e.1.len() == 0 { + return Error::ApiError(ApiError{ + code: e.0, + content: None, + }); + } + match serde_json::from_slice::(e.1) { + Ok(t) => Error::ApiError(ApiError{ + code: e.0, + content: Some(t), + }), + Err(e) => { + Error::from(e) + } + } + } +} + +impl From for Error { fn from(e: hyper::Error) -> Self { return Error::Hyper(e) } } -impl From for Error { +impl From for Error { fn from(e: serde_json::Error) -> Self { return Error::Serde(e) } diff --git a/modules/swagger-codegen/src/main/resources/rust/lib.rs b/modules/swagger-codegen/src/main/resources/rust/lib.rs index 2ad195c6fb1..b51fea8150e 100644 --- a/modules/swagger-codegen/src/main/resources/rust/lib.rs +++ b/modules/swagger-codegen/src/main/resources/rust/lib.rs @@ -2,6 +2,7 @@ extern crate serde_derive; extern crate hyper; +extern crate serde; extern crate serde_json; extern crate futures; extern crate url; diff --git a/samples/client/petstore/rust/examples/error_handling.rs b/samples/client/petstore/rust/examples/error_handling.rs new file mode 100644 index 00000000000..cb3a91d2833 --- /dev/null +++ b/samples/client/petstore/rust/examples/error_handling.rs @@ -0,0 +1,49 @@ +extern crate futures; +extern crate hyper; +extern crate petstore_client; +extern crate tokio_core; + +use hyper::Client; +use hyper::client::HttpConnector; +use tokio_core::reactor::Core; +use futures::Future; +use petstore_client::apis::client::APIClient; +use petstore_client::apis::Error; + +fn main() { + let mut core = Core::new().expect("failed to init core"); + let handle = core.handle(); + + let mut configuration = petstore_client::apis::configuration::Configuration::new( + Client::configure() + .connector(HttpConnector::new(4, &handle)) + .build(&handle), + ); + if let Ok(env_override) = std::env::var("PETSTORE_BASEPATH") { + configuration.base_path = env_override; + } + + let apicli = APIClient::new(configuration); + let work = apicli + .user_api() + .delete_user("404") + .then(|resp| match resp { + Ok(resp) => { + panic!(format!( + "update for nonexistent pet should fail, but got: {:?}", + resp + )); + } + Err(Error::ApiError(s)) => { + println!("got expected error: {:?}", s); + futures::future::ok::<(), ()>(()) + } + Err(Error::Hyper(e)) => { + println!("network error: {}", e); + futures::future::ok::<(), ()>(()) + } + Err(e) => panic!("unexpected error: {:?}", e), + }); + + core.run(work).expect("failed to run core"); +} diff --git a/samples/client/petstore/rust/src/apis/mod.rs b/samples/client/petstore/rust/src/apis/mod.rs index ebd01fe651a..7199222c3f7 100644 --- a/samples/client/petstore/rust/src/apis/mod.rs +++ b/samples/client/petstore/rust/src/apis/mod.rs @@ -1,19 +1,48 @@ use hyper; +use serde; use serde_json; #[derive(Debug)] -pub enum Error { +pub enum Error { Hyper(hyper::Error), Serde(serde_json::Error), + ApiError(ApiError), } -impl From for Error { +#[derive(Debug)] +pub struct ApiError { + pub code: hyper::StatusCode, + pub content: Option, +} + +impl<'de, T> From<(hyper::StatusCode, &'de [u8])> for Error + where T: serde::Deserialize<'de> { + fn from(e: (hyper::StatusCode, &'de [u8])) -> Self { + if e.1.len() == 0 { + return Error::ApiError(ApiError{ + code: e.0, + content: None, + }); + } + match serde_json::from_slice::(e.1) { + Ok(t) => Error::ApiError(ApiError{ + code: e.0, + content: Some(t), + }), + Err(e) => { + Error::from(e) + } + } + } +} + +impl From for Error { fn from(e: hyper::Error) -> Self { return Error::Hyper(e) } } -impl From for Error { +impl From for Error { fn from(e: serde_json::Error) -> Self { return Error::Serde(e) } diff --git a/samples/client/petstore/rust/src/apis/pet_api.rs b/samples/client/petstore/rust/src/apis/pet_api.rs index 081ff02a618..315ffeec112 100644 --- a/samples/client/petstore/rust/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/src/apis/pet_api.rs @@ -34,19 +34,19 @@ impl PetApiClient { } pub trait PetApi { - fn add_pet(&self, body: ::models::Pet) -> Box>; - fn delete_pet(&self, pet_id: i64, api_key: &str) -> Box>; - fn find_pets_by_status(&self, status: Vec) -> Box, Error = Error>>; - fn find_pets_by_tags(&self, tags: Vec) -> Box, Error = Error>>; - fn get_pet_by_id(&self, pet_id: i64) -> Box>; - fn update_pet(&self, body: ::models::Pet) -> Box>; - fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Box>; - fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Box>; + fn add_pet(&self, body: ::models::Pet) -> Box>>; + fn delete_pet(&self, pet_id: i64, api_key: &str) -> Box>>; + fn find_pets_by_status(&self, status: Vec) -> Box, Error = Error>>; + fn find_pets_by_tags(&self, tags: Vec) -> Box, Error = Error>>; + fn get_pet_by_id(&self, pet_id: i64) -> Box>>; + fn update_pet(&self, body: ::models::Pet) -> Box>>; + fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Box>>; + fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Box>>; } implPetApi for PetApiClient { - fn add_pet(&self, body: ::models::Pet) -> Box> { + fn add_pet(&self, body: ::models::Pet) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Post; @@ -72,13 +72,26 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn delete_pet(&self, pet_id: i64, api_key: &str) -> Box> { + fn delete_pet(&self, pet_id: i64, api_key: &str) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Delete; @@ -104,13 +117,26 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn find_pets_by_status(&self, status: Vec) -> Box, Error = Error>> { + fn find_pets_by_status(&self, status: Vec) -> Box, Error = Error>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -135,16 +161,29 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } - fn find_pets_by_tags(&self, tags: Vec) -> Box, Error = Error>> { + fn find_pets_by_tags(&self, tags: Vec) -> Box, Error = Error>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -169,16 +208,29 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } - fn get_pet_by_id(&self, pet_id: i64) -> Box> { + fn get_pet_by_id(&self, pet_id: i64) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -200,16 +252,29 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result<::models::Pet, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } - fn update_pet(&self, body: ::models::Pet) -> Box> { + fn update_pet(&self, body: ::models::Pet) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Put; @@ -235,13 +300,26 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Box> { + fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Post; @@ -263,13 +341,26 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Box> { + fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Post; @@ -291,12 +382,25 @@ implPetApi for PetApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result<::models::ApiResponse, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } diff --git a/samples/client/petstore/rust/src/apis/store_api.rs b/samples/client/petstore/rust/src/apis/store_api.rs index 5f493f2961a..fe3c1d325c7 100644 --- a/samples/client/petstore/rust/src/apis/store_api.rs +++ b/samples/client/petstore/rust/src/apis/store_api.rs @@ -34,15 +34,15 @@ impl StoreApiClient { } pub trait StoreApi { - fn delete_order(&self, order_id: &str) -> Box>; - fn get_inventory(&self, ) -> Box, Error = Error>>; - fn get_order_by_id(&self, order_id: i64) -> Box>; - fn place_order(&self, body: ::models::Order) -> Box>; + fn delete_order(&self, order_id: &str) -> Box>>; + fn get_inventory(&self, ) -> Box, Error = Error>>; + fn get_order_by_id(&self, order_id: i64) -> Box>>; + fn place_order(&self, body: ::models::Order) -> Box>>; } implStoreApi for StoreApiClient { - fn delete_order(&self, order_id: &str) -> Box> { + fn delete_order(&self, order_id: &str) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Delete; @@ -64,13 +64,26 @@ implStoreApi for StoreApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn get_inventory(&self, ) -> Box, Error = Error>> { + fn get_inventory(&self, ) -> Box, Error = Error>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -92,16 +105,29 @@ implStoreApi for StoreApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result<::std::collections::HashMap, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } - fn get_order_by_id(&self, order_id: i64) -> Box> { + fn get_order_by_id(&self, order_id: i64) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -123,16 +149,29 @@ implStoreApi for StoreApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result<::models::Order, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } - fn place_order(&self, body: ::models::Order) -> Box> { + fn place_order(&self, body: ::models::Order) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Post; @@ -158,12 +197,25 @@ implStoreApi for StoreApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result<::models::Order, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } diff --git a/samples/client/petstore/rust/src/apis/user_api.rs b/samples/client/petstore/rust/src/apis/user_api.rs index 71a35e49b26..c3c9f7d457e 100644 --- a/samples/client/petstore/rust/src/apis/user_api.rs +++ b/samples/client/petstore/rust/src/apis/user_api.rs @@ -34,19 +34,19 @@ impl UserApiClient { } pub trait UserApi { - fn create_user(&self, body: ::models::User) -> Box>; - fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Box>; - fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Box>; - fn delete_user(&self, username: &str) -> Box>; - fn get_user_by_name(&self, username: &str) -> Box>; - fn login_user(&self, username: &str, password: &str) -> Box>; - fn logout_user(&self, ) -> Box>; - fn update_user(&self, username: &str, body: ::models::User) -> Box>; + fn create_user(&self, body: ::models::User) -> Box>>; + fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Box>>; + fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Box>>; + fn delete_user(&self, username: &str) -> Box>>; + fn get_user_by_name(&self, username: &str) -> Box>>; + fn login_user(&self, username: &str, password: &str) -> Box>>; + fn logout_user(&self, ) -> Box>>; + fn update_user(&self, username: &str, body: ::models::User) -> Box>>; } implUserApi for UserApiClient { - fn create_user(&self, body: ::models::User) -> Box> { + fn create_user(&self, body: ::models::User) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Post; @@ -72,13 +72,26 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Box> { + fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Post; @@ -104,13 +117,26 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Box> { + fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Post; @@ -136,13 +162,26 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn delete_user(&self, username: &str) -> Box> { + fn delete_user(&self, username: &str) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Delete; @@ -164,13 +203,26 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn get_user_by_name(&self, username: &str) -> Box> { + fn get_user_by_name(&self, username: &str) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -192,16 +244,29 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result<::models::User, _> = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } - fn login_user(&self, username: &str, password: &str) -> Box> { + fn login_user(&self, username: &str, password: &str) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -227,16 +292,29 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|body| { let parsed: Result = serde_json::from_slice(&body); parsed.map_err(|e| Error::from(e)) - }).map_err(|e| Error::from(e)) + }) ) } - fn logout_user(&self, ) -> Box> { + fn logout_user(&self, ) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Get; @@ -258,13 +336,26 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } - fn update_user(&self, username: &str, body: ::models::User) -> Box> { + fn update_user(&self, username: &str, body: ::models::User) -> Box>> { let configuration: &configuration::Configuration = self.configuration.borrow(); let method = hyper::Method::Put; @@ -290,8 +381,21 @@ implUserApi for UserApiClient { // send request Box::new( - configuration.client.request(req).and_then(|res| { res.body().concat2() }) + configuration.client.request(req) .map_err(|e| Error::from(e)) + .and_then(|resp| { + let status = resp.status(); + resp.body().concat2() + .and_then(move |body| Ok((status, body))) + .map_err(|e| Error::from(e)) + }) + .and_then(|(status, body)| { + if status.is_success() { + Ok(body) + } else { + Err(Error::from((status, &*body))) + } + }) .and_then(|_| futures::future::ok(())) ) } diff --git a/samples/client/petstore/rust/src/lib.rs b/samples/client/petstore/rust/src/lib.rs index 2ad195c6fb1..b51fea8150e 100644 --- a/samples/client/petstore/rust/src/lib.rs +++ b/samples/client/petstore/rust/src/lib.rs @@ -2,6 +2,7 @@ extern crate serde_derive; extern crate hyper; +extern crate serde; extern crate serde_json; extern crate futures; extern crate url; diff --git a/samples/server/petstore/rust-server/.swagger-codegen/VERSION b/samples/server/petstore/rust-server/.swagger-codegen/VERSION index f9f7450d135..cc6612c36e0 100644 --- a/samples/server/petstore/rust-server/.swagger-codegen/VERSION +++ b/samples/server/petstore/rust-server/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.0-SNAPSHOT \ No newline at end of file +2.3.0 \ No newline at end of file diff --git a/samples/server/petstore/rust-server/README.md b/samples/server/petstore/rust-server/README.md index 09fa6742f17..f8bdb0ab4d1 100644 --- a/samples/server/petstore/rust-server/README.md +++ b/samples/server/petstore/rust-server/README.md @@ -13,7 +13,7 @@ To see how to make this your own, look here: [README](https://github.com/swagger-api/swagger-codegen/blob/master/README.md) - API version: 1.0.0 -- Build date: 2017-12-20T15:51:59.221+09:00 +- Build date: 2018-01-07T09:31:52.377-08:00 This autogenerated project defines an API crate `petstore_api` which contains: * An `Api` trait defining the API in Rust. diff --git a/samples/server/petstore/rust-server/api/swagger.yaml b/samples/server/petstore/rust-server/api/swagger.yaml index a9b80a7cef3..128267b767d 100644 --- a/samples/server/petstore/rust-server/api/swagger.yaml +++ b/samples/server/petstore/rust-server/api/swagger.yaml @@ -143,11 +143,11 @@ paths: type: "array" items: type: "string" + default: "available" enum: - "available" - "pending" - "sold" - default: "available" collectionFormat: "csv" formatString: "{:?}" example: "&Vec::new()" @@ -938,10 +938,10 @@ paths: type: "array" items: type: "string" + default: "$" enum: - ">" - "$" - default: "$" formatString: "{:?}" example: "Some(&Vec::new())" - name: "enum_form_string" @@ -963,10 +963,10 @@ paths: type: "array" items: type: "string" + default: "$" enum: - ">" - "$" - default: "$" formatString: "{:?}" example: "Some(&Vec::new())" - name: "enum_header_string" @@ -988,10 +988,10 @@ paths: type: "array" items: type: "string" + default: "$" enum: - ">" - "$" - default: "$" formatString: "{:?}" example: "Some(&Vec::new())" - name: "enum_query_string"