[Rust Server] Support multipart/form_data request bodies (#2846)

[Rust Server] Support multipart/form_data

- Support multipart/form_data in the Rust Server
- Add a new test API to test the change.
- Update the examples to match
This commit is contained in:
Richard Whitehouse
2019-08-04 12:54:24 +01:00
committed by GitHub
parent 86228e9964
commit 6963e5eeb0
48 changed files with 2421 additions and 892 deletions

View File

@@ -709,6 +709,9 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
consumesPlainText = true;
} else if (isMimetypeWwwFormUrlEncoded(mediaType)) {
additionalProperties.put("usesUrlEncodedForm", true);
} else if (isMimetypeMultipartFormData(mediaType)) {
op.vendorExtensions.put("consumesMultipart", true);
additionalProperties.put("apiUsesMultipart", true);
}
}
}
@@ -724,8 +727,8 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
} else {
op.bodyParam.vendorExtensions.put("consumesJson", true);
}
}
for (CodegenParameter param : op.bodyParams) {
processParam(param, op);
@@ -789,6 +792,8 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
codegenParameter.isPrimitiveType = false;
codegenParameter.isListContainer = false;
codegenParameter.isString = false;
codegenParameter.isByteArray = ModelUtils.isByteArraySchema(original_schema);
// This is a model, so should only have an example if explicitly
// defined.

View File

@@ -25,8 +25,8 @@ swagger = "2"
#
lazy_static = "0.2"
log = "0.3.0"
mime = "0.3.3"
multipart = {version = "0.13.3", optional = true}
mime = "0.2.6"
multipart = {version = "0.13.3"}
native-tls = {version = "0.1.4", optional = true}
openssl = {version = "0.9.14", optional = true}
percent-encoding = {version = "1.0.0", optional = true}

View File

@@ -6,9 +6,16 @@ extern crate openssl;
extern crate mime;
extern crate chrono;
extern crate url;
{{#usesUrlEncodedForm}}extern crate serde_urlencoded;{{/usesUrlEncodedForm}}
{{#usesUrlEncodedForm}}
extern crate serde_urlencoded;
{{/usesUrlEncodedForm}}
{{#apiUsesMultipart}}
extern crate multipart;
{{/apiUsesMultipart}}
{{#apiUsesUuid}}use uuid;{{/apiUsesUuid}}
{{#apiUsesUuid}}
use uuid;
{{/apiUsesUuid}}
use hyper;
use hyper::header::{Headers, ContentType};
use hyper::Uri;
@@ -26,11 +33,16 @@ use std::sync::Arc;
use std::str;
use std::str::FromStr;
use std::string::ToString;
{{#apiUsesMultipart}}
use hyper::mime::Mime;
use std::io::Cursor;
use client::multipart::client::lazy::Multipart;
{{/apiUsesMultipart}}
use mimetypes;
use serde_json;
{{#usesXml}}use serde_xml_rs;{{/usesXml}}
{{#usesXml}}
use serde_xml_rs;
{{/usesXml}}
#[allow(unused_imports)]
use std::collections::{HashMap, BTreeMap};
@@ -275,15 +287,93 @@ impl<F, C> Api<C> for Client<F> where
let mut request = hyper::Request::new(hyper::Method::{{#vendorExtensions}}{{{HttpMethod}}}{{/vendorExtensions}}, uri);
{{#vendorExtensions}}{{#formParams}}{{#-first}} let params = &[{{/-first}}
("{{{baseName}}}", {{#vendorExtensions}}{{#required}}Some({{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}format!("{:?}", param_{{{paramName}}}){{/isString}}){{/required}}{{^required}}{{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}param_{{{paramName}}}.map(|param| format!("{:?}", param)){{/isString}}{{/required}}),{{/vendorExtensions}}{{#-last}}
{{#vendorExtensions}}
{{#consumesMultipart}}
let mut multipart = Multipart::new();
{{#vendorExtensions}}
{{#formParams}}
{{#-first}}
// For each parameter, encode as appropriate and add to the multipart body as a stream.
{{/-first}}
{{^isByteArray}}
{{#jsonSchema}}
let {{{paramName}}}_str = match serde_json::to_string(&param_{{{paramName}}}) {
Ok(str) => str,
Err(e) => return Box::new(futures::done(Err(ApiError(format!("Unable to parse {{{paramName}}} to string: {}", e))))),
};
let {{{paramName}}}_vec = {{{paramName}}}_str.as_bytes().to_vec();
let {{{paramName}}}_mime = mime::Mime::from_str("application/json").expect("impossible to fail to parse");
let {{{paramName}}}_cursor = Cursor::new({{{paramName}}}_vec);
multipart.add_stream("{{{paramName}}}", {{{paramName}}}_cursor, None as Option<&str>, Some({{{paramName}}}_mime));
{{/jsonSchema}}
{{/isByteArray}}
{{#isByteArray}}
let {{{paramName}}}_vec = param_{{{paramName}}}.to_vec();
let {{{paramName}}}_mime = match mime::Mime::from_str("application/octet-stream") {
Ok(mime) => mime,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to get mime type: {:?}", err))))),
};
let {{{paramName}}}_cursor = Cursor::new({{{paramName}}}_vec);
let filename = None as Option<&str> ;
multipart.add_stream("{{{paramName}}}", {{{paramName}}}_cursor, filename, Some({{{paramName}}}_mime));
{{/isByteArray}}
{{#-last}}
{{/-last}}
{{/formParams}}
{{/vendorExtensions}}
let mut fields = match multipart.prepare() {
Ok(fields) => fields,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build request: {}", err))))),
};
let mut body_string = String::new();
fields.to_body().read_to_string(&mut body_string).unwrap();
let boundary = fields.boundary();
let multipart_header = match Mime::from_str(&format!("multipart/form-data;boundary={}", boundary)) {
Ok(multipart_header) => multipart_header,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build multipart header: {:?}", err))))),
};
request.set_body(body_string.into_bytes());
request.headers_mut().set(ContentType(multipart_header));
{{/consumesMultipart}}
{{/vendorExtensions}}
{{#vendorExtensions}}
{{^consumesMultipart}}
{{#vendorExtensions}}
{{#formParams}}
{{#-first}}
let params = &[
{{/-first}}
("{{{baseName}}}", {{#vendorExtensions}}{{#required}}Some({{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}format!("{:?}", param_{{{paramName}}}){{/isString}}){{/required}}{{^required}}{{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}param_{{{paramName}}}.map(|param| format!("{:?}", param)){{/isString}}{{/required}}),{{/vendorExtensions}}
{{#-last}}
];
let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize");
request.headers_mut().set(ContentType(mimetypes::requests::{{#vendorExtensions}}{{{uppercase_operation_id}}}{{/vendorExtensions}}.clone()));
request.set_body(body.into_bytes());{{/-last}}{{/formParams}}{{/vendorExtensions}}{{#bodyParam}}{{#-first}}
request.set_body(body.into_bytes());
{{/-last}}
{{/formParams}}
{{/vendorExtensions}}
{{#bodyParam}}
{{#-first}}
// Body parameter
{{/-first}}
{{/-first}}
{{#vendorExtensions}}
{{#consumesPlainText}}
{{#isByteArray}}
@@ -312,34 +402,51 @@ impl<F, C> Api<C> for Client<F> where
});
{{/required}}
{{/vendorExtensions}}
{{/bodyParam}}
{{^required}}
{{#bodyParam}}{{^required}}if let Some(body) = body {
{{/required}} request.set_body(body);
{{^required}} }{{/required}}
if let Some(body) = body {
{{/required}}
request.set_body(body);
{{^required}}
}
{{/required}}
request.headers_mut().set(ContentType(mimetypes::requests::{{#vendorExtensions}}{{{uppercase_operation_id}}}{{/vendorExtensions}}.clone()));
{{/bodyParam}}
{{/bodyParam}}
{{/consumesMultipart}}
{{/vendorExtensions}}
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
{{#authMethods}}{{#isBasic}} if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
{{#authMethods}}
{{#isBasic}}
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
if let AuthData::Basic(ref basic_header) = *auth_data {
request.headers_mut().set(hyper::header::Authorization(
basic_header.clone(),
))
}
}{{/isBasic}}{{#isApiKey}}{{#isKeyInHeader}} header! { ({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}, "{{keyParamName}}") => [String] }
}
{{/isBasic}}
{{#isApiKey}}
{{#isKeyInHeader}}
header! { ({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}, "{{keyParamName}}") => [String] }
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
if let AuthData::ApiKey(ref api_key) = *auth_data {
request.headers_mut().set({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}(api_key.to_string()));
}
}{{/isKeyInHeader}}{{/isApiKey}}{{/authMethods}}{{#headerParams}}{{#-first}}
}
{{/isKeyInHeader}}
{{/isApiKey}}
{{/authMethods}}
{{#headerParams}}
{{#-first}}
// Header parameters
{{/-first}}{{^isMapContainer}} header! { (Request{{vendorExtensions.typeName}}, "{{{baseName}}}") => {{#isListContainer}}({{{baseType}}})*{{/isListContainer}}{{^isListContainer}}[{{{dataType}}}]{{/isListContainer}} }
{{#required}} request.headers_mut().set(Request{{vendorExtensions.typeName}}(param_{{{paramName}}}{{#isListContainer}}.clone(){{/isListContainer}}));
{{/required}}{{^required}} param_{{{paramName}}}.map(|header| request.headers_mut().set(Request{{vendorExtensions.typeName}}(header{{#isListContainer}}.clone(){{/isListContainer}})));
{{/required}}{{/isMapContainer}}{{#isMapContainer}} let param_{{{paramName}}}: Option<{{{dataType}}}> = None;
{{/isMapContainer}}{{/headerParams}}
{{/isMapContainer}}
{{/headerParams}}
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {

View File

@@ -11,6 +11,7 @@ extern crate chrono;
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate mime;
// Logically this should be in the client and server modules, but rust doesn't allow `macro_use` from a module.
#[cfg(any(feature = "client", feature = "server"))]

View File

@@ -5,12 +5,16 @@ extern crate native_tls;
extern crate hyper_tls;
extern crate openssl;
extern crate mime;
{{^apiUsesUuid}}extern crate uuid;{{/apiUsesUuid}}
extern crate chrono;
extern crate percent_encoding;
extern crate url;
{{^apiUsesUuid}}
extern crate uuid;
{{/apiUsesUuid}}
{{#apiUsesMultipart}}
extern crate multipart;
{{/apiUsesMultipart}}
{{#apiUsesUuid}}use uuid;{{/apiUsesUuid}}
use std::sync::Arc;
use std::marker::PhantomData;
use futures::{Future, future, Stream, stream};
@@ -19,9 +23,18 @@ use hyper::{Request, Response, Error, StatusCode};
use hyper::header::{Headers, ContentType};
use self::url::form_urlencoded;
use mimetypes;
{{#apiUsesMultipart}}
use self::multipart::server::Multipart;
use self::multipart::server::save::SaveResult;
use std::fs;
{{/apiUsesMultipart}}
use serde_json;
{{#usesXml}}use serde_xml_rs;{{/usesXml}}
{{#usesXml}}
use serde_xml_rs;
{{/usesXml}}
{{#apiUsesUuid}}
use uuid;
{{/apiUsesUuid}}
#[allow(unused_imports)]
use std::collections::{HashMap, BTreeMap};
@@ -165,8 +178,14 @@ where
{{/authMethods}}
}
{{/hasAuthMethods}}
{{#vendorExtensions}}{{#hasPathParams}}
{{#vendorExtensions}}
{{#consumesMultipart}}
let boundary = match multipart_boundary(&headers) {
Some(boundary) => boundary.to_string(),
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body("Couldn't find valid multipart body"))),
};
{{/consumesMultipart}}
{{#hasPathParams}}
// Path parameters
let path = uri.path().to_string();
let path_params =
@@ -175,7 +194,8 @@ where
.unwrap_or_else(||
panic!("Path {} matched RE {{{PATH_ID}}} in set but failed match against \"{}\"", path, paths::REGEX_{{{PATH_ID}}}.as_str())
);
{{/hasPathParams}}{{/vendorExtensions}}
{{/hasPathParams}}
{{/vendorExtensions}}
{{#pathParams}}
let param_{{{paramName}}} = match percent_encoding::percent_decode(path_params["{{{baseName}}}"].as_bytes()).decode_utf8() {
Ok(param_{{{paramName}}}) => match param_{{{paramName}}}.parse::<{{{dataType}}}>() {
@@ -185,36 +205,39 @@ where
Err(_) => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't percent-decode path parameter as UTF-8: {}", &path_params["{{{baseName}}}"]))))
};
{{/pathParams}}
{{#headerParams}}{{#-first}}
{{#headerParams}}
{{#-first}}
// Header parameters
{{/-first}}
{{/-first}}
header! { (Request{{vendorExtensions.typeName}}, "{{{baseName}}}") => {{#isListContainer}}({{{baseType}}})*{{/isListContainer}}{{^isListContainer}}[{{{dataType}}}]{{/isListContainer}} }
{{#required}}
{{#required}}
let param_{{{paramName}}} = match headers.get::<Request{{vendorExtensions.typeName}}>() {
Some(param_{{{paramName}}}) => param_{{{paramName}}}.0.clone(),
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body("Missing or invalid required header {{{baseName}}}"))),
};
{{/required}}
{{^required}}
{{/required}}
{{^required}}
let param_{{{paramName}}} = headers.get::<Request{{vendorExtensions.typeName}}>().map(|header| header.0.clone());
{{/required}}{{/headerParams}}
{{#queryParams}}{{#-first}}
{{/required}}
{{/headerParams}}
{{#queryParams}}
{{#-first}}
// Query parameters (note that non-required or collection query parameters will ignore garbage values, rather than causing a 400 response)
let query_params = form_urlencoded::parse(uri.query().unwrap_or_default().as_bytes()).collect::<Vec<_>>();
{{/-first}}
{{/-first}}
let param_{{{paramName}}} = query_params.iter().filter(|e| e.0 == "{{{baseName}}}").map(|e| e.1.to_owned())
{{#isListContainer}}
{{#isListContainer}}
.filter_map(|param_{{{paramName}}}| param_{{{paramName}}}.parse::<{{{baseType}}}>().ok())
.collect::<Vec<_>>();
{{^required}}
{{^required}}
let param_{{{paramName}}} = if !param_{{{paramName}}}.is_empty() {
Some(param_{{{paramName}}})
} else {
None
};
{{/required}}
{{/isListContainer}}{{^isListContainer}}
{{/required}}
{{/isListContainer}}
{{^isListContainer}}
.nth(0);
{{#required}}
let param_{{{paramName}}} = match param_{{{paramName}}} {
@@ -229,8 +252,10 @@ where
{{/required}}
{{/isListContainer}}
{{/queryParams}}
{{#bodyParams}}{{#-first}}
{{#vendorExtensions}}
{{^consumesMultipart}}
{{#bodyParams}}
{{#-first}}
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -238,53 +263,147 @@ where
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
{{#vendorExtensions}}{{^consumesPlainText}}
{{#vendorExtensions}}
{{^consumesPlainText}}
let mut unused_elements = Vec::new();
{{/consumesPlainText}}
{{/consumesPlainText}}
let param_{{{paramName}}}: Option<{{{dataType}}}> = if !body.is_empty() {
{{#consumesXml}}
{{#consumesXml}}
let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body);
{{/consumesXml}}{{#consumesJson}}
{{/consumesXml}}
{{#consumesJson}}
let deserializer = &mut serde_json::Deserializer::from_slice(&*body);
{{/consumesJson}}{{^consumesPlainText}}
{{/consumesJson}}
{{^consumesPlainText}}
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
}) {
Ok(param_{{{paramName}}}) => param_{{{paramName}}},
{{#required}}
{{#required}}
Err(e) => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't parse body parameter {{{baseName}}} - doesn't match schema: {}", e)))),
{{/required}}{{^required}}
{{/required}}
{{^required}}
Err(_) => None,
{{/required}}
{{/required}}
}
{{/consumesPlainText}}{{#consumesPlainText}}
{{#isByteArray}}
{{/consumesPlainText}}
{{#consumesPlainText}}
{{#isByteArray}}
Some(swagger::ByteArray(body.to_vec()))
{{/isByteArray}}
{{#isString}}
{{/isByteArray}}
{{#isString}}
Some(String::from_utf8(body.to_vec()).unwrap())
{{/isString}}
{{/consumesPlainText}}{{/vendorExtensions}}
{{/isString}}
{{/consumesPlainText}}
{{/vendorExtensions}}
} else {
None
};
{{#required}}
{{#required}}
let param_{{{paramName}}} = match param_{{{paramName}}} {
Some(param_{{{paramName}}}) => param_{{{paramName}}},
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body("Missing required body parameter {{{baseName}}}"))),
};
{{/required}}
{{/-first}}{{/bodyParams}}
{{^bodyParams}}{{#vendorExtensions}}
{{/required}}
{{/-first}}
{{/bodyParams}}
{{/consumesMultipart}}
{{#consumesMultipart}}
{{^bodyParams}}
{{#vendorExtensions}}
// Form Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
Box::new(body.concat2()
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
// Read Form Parameters from body
let mut entries = match Multipart::with_body(&body.to_vec()[..], boundary).save().temp() {
SaveResult::Full(entries) => {
entries
},
_ => {
return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Unable to process all message parts"))))
},
};
{{#formParams}}{{#-first}}{{/-first}}
{{#isByteArray}}
let file_{{{paramName}}} = entries.files.remove("{{{paramName}}}");
{{#required}}
let param_{{{paramName}}} = match file_{{{paramName}}} {
Some(file) => {
let path = &file[0].path;
let {{{paramName}}}_str = fs::read_to_string(path).unwrap();
swagger::ByteArray({{{paramName}}}_str.as_bytes().to_vec())
}
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Missing required form parameter {{{paramName}}}")))),
};
{{/required}}
{{^required}}
let param_{{{paramName}}} = match file_{{{paramName}}} {
Some(file) => {
let path = &file[0].path;
let {{{paramName}}}_str = fs::read_to_string(path).unwrap();
Some(swagger::ByteArray({{{paramName}}}_str.as_bytes().to_vec()))
}
None => None,
};
{{/required}}
{{/isByteArray}}
{{^isByteArray}}{{#jsonSchema}}
let file_{{{paramName}}} = entries.files.remove("{{{paramName}}}");
{{#required}}
let param_{{{paramName}}} = match file_{{{paramName}}} {
Some(file) => {
let path = &file[0].path;
let {{{paramName}}}_str = fs::read_to_string(path).expect("Reading saved String should never fail");
let {{{paramName}}}_model: {{{dataType}}} = match serde_json::from_str(&{{{paramName}}}_str) {
Ok(model) => model,
Err(e) => {
return Box::new(future::ok(
Response::new()
.with_status(StatusCode::BadRequest)
.with_body(format!("{{{paramName}}} data does not match API definition: {}", e))))
}
};
{{{paramName}}}_model
}
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Missing required form parameter {{{paramName}}}")))),
};
{{/required}}
{{^required}}
let param_{{{paramName}}} = match file_{{{paramName}}} {
Some(file) => {
let path = &file[0].path;
let {{{paramName}}}_str = fs::read_to_string(path).unwrap();
let {{{paramName}}}_model: {{{dataType}}} = serde_json::from_str(&{{{paramName}}}_str).expect("Impossible to fail to serialise");
Some({{{paramName}}}_model)
}
None => None,
};
{{/required}}
{{/jsonSchema}}{{/isByteArray}}
{{/formParams}}
{{/vendorExtensions}}
{{/bodyParams}}
{{/consumesMultipart}}
{{^consumesMultipart}}
{{^bodyParams}}
{{#vendorExtensions}}
Box::new({
{{
{{#formParams}}{{#-first}}
{{#formParams}}
{{#-first}}
// Form parameters
{{/-first}}
{{/-first}}
let param_{{{paramName}}} = {{^isContainer}}{{#vendorExtensions}}{{{example}}};{{/vendorExtensions}}{{/isContainer}}{{#isListContainer}}{{#required}}Vec::new();{{/required}}{{^required}}None;{{/required}}{{/isListContainer}}{{#isMapContainer}}None;{{/isMapContainer}}
{{/formParams}}
{{/vendorExtensions}}{{/bodyParams}}
{{/formParams}}
{{/vendorExtensions}}
{{/bodyParams}}
{{/consumesMultipart}}
{{/vendorExtensions}}
Box::new(api_impl.{{#vendorExtensions}}{{{operation_id}}}{{/vendorExtensions}}({{#allParams}}param_{{{paramName}}}{{#isListContainer}}.as_ref(){{/isListContainer}}, {{/allParams}}&context)
.then(move |result| {
let mut response = Response::new();
@@ -356,19 +475,36 @@ where
future::ok(response)
}
))
{{^bodyParams}}{{#vendorExtensions}}
{{#vendorExtensions}}
{{^consumesMultipart}}
{{^bodyParams}}
}}
}) as Box<Future<Item=Response, Error=Error>>
{{/vendorExtensions}}{{/bodyParams}}
{{#bodyParams}}{{#-first}}
{{/bodyParams}}
{{/consumesMultipart}}
{{/vendorExtensions}}
{{#bodyParams}}
{{#-first}}
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter {{{baseName}}}: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
{{/-first}}{{/bodyParams}}
{{/-first}}
{{/bodyParams}}
{{#vendorExtensions}}
{{#consumesMultipart}}
{{^bodyParams}}
as Box<Future<Item=Response, Error=Error>>
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read multipart body")))),
}
})
)
{{/bodyParams}}
{{/consumesMultipart}}
{{/vendorExtensions}}
},
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
_ => Box::new(future::ok(Response::new().with_status(StatusCode::NotFound))) as Box<Future<Item=Response, Error=Error>>,
}
@@ -385,6 +521,20 @@ impl<T, C> Clone for Service<T, C>
}
}
{{#apiUsesMultipart}}
/// Utility function to get the multipart boundary marker (if any) from the Headers.
fn multipart_boundary<'a>(headers: &'a Headers) -> Option<&'a str> {
headers.get::<ContentType>().and_then(|content_type| {
let ContentType(ref mime) = *content_type;
if mime.type_() == hyper::mime::MULTIPART && mime.subtype() == hyper::mime::FORM_DATA {
mime.get_param(hyper::mime::BOUNDARY).map(|x| x.as_str())
} else {
None
}
})
}
{{/apiUsesMultipart}}
/// Request parser for `Api`.
pub struct ApiRequestParser;
impl RequestParser for ApiRequestParser {

View File

@@ -0,0 +1,51 @@
# Test the multipart function of the OpenAPI specification
#
# Specifically, these tests include
# - multipart/form data including
# - binary data
# - string data
# - objects
openapi: 3.0.1
info:
title: Multipart OpenAPI V3 Rust Server Test
description: API under test
version: 1.0.7
paths:
/multipart_request:
post:
requestBody:
required: true
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/multipart_request'
responses:
'201':
description: 'OK'
components:
schemas:
multipart_request:
type: object
required:
- string_field
- binary_field
properties:
string_field:
type: string
optional_string_field:
type: string
object_field:
type: object
required:
- field_a
properties:
field_a:
type: string
field_b:
type: array
items:
type: string
binary_field:
type: string
format: byte

View File

@@ -1,5 +1,7 @@
# Test the mainline function of the XML part of the OpenAPI specification,
# as found here : https://swagger.io/docs/specification/data-models/representing-xml/
# Test the mainline function of OpenAPI v3 specification.
#
# This includes the XML part of the OpenAPI specification, as found at
# https://swagger.io/docs/specification/data-models/representing-xml/
#
# Specifically, these tests are intended to include:
# - namespaces
@@ -9,6 +11,8 @@
# - wrapping and renaming to and from camelCase and snake_case
# - objects
# - renaming to and from camelCase and snake_case
# - UUIDs
# - Octet Streams
openapi: 3.0.1
info:
@@ -76,6 +80,15 @@ paths:
description: 'OK'
'400':
description: Bad Request
/uuid:
get:
responses:
200:
description: Duplicate Response long text. One.
content:
application/json:
schema:
$ref: "#/components/schemas/UuidObject"
/required_octet_stream:
put:
requestBody:

View File

@@ -0,0 +1,18 @@
[build]
rustflags = [
"-W", "missing_docs", # detects missing documentation for public members
"-W", "trivial_casts", # detects trivial casts which could be removed
"-W", "trivial_numeric_casts", # detects trivial casts of numeric types which could be removed
"-W", "unsafe_code", # usage of `unsafe` code
"-W", "unused_qualifications", # detects unnecessarily qualified names
"-W", "unused_extern_crates", # extern crates that are never used
"-W", "unused_import_braces", # unnecessary braces around an imported item
"-D", "warnings", # all warnings should be denied
]

View File

@@ -0,0 +1,2 @@
target
Cargo.lock

View File

@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.
# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

View File

@@ -0,0 +1 @@
4.1.0-SNAPSHOT

View File

@@ -0,0 +1,48 @@
[package]
name = "multipart-v3"
version = "1.0.7"
authors = []
description = "API under test"
license = "Unlicense"
[features]
default = ["client", "server"]
client = ["serde_json", "serde_ignored", "hyper", "hyper-tls", "native-tls", "openssl", "tokio-core", "url", "uuid"]
server = ["serde_json", "serde_ignored", "hyper", "hyper-tls", "native-tls", "openssl", "tokio-core", "tokio-proto", "tokio-tls", "regex", "percent-encoding", "url", "uuid"]
[dependencies]
# Required by example server.
#
chrono = { version = "0.4", features = ["serde"] }
futures = "0.1"
hyper = {version = "0.11", optional = true}
hyper-tls = {version = "0.1.2", optional = true}
swagger = "2"
# Not required by example server.
#
lazy_static = "0.2"
log = "0.3.0"
mime = "0.2.6"
multipart = {version = "0.13.3"}
native-tls = {version = "0.1.4", optional = true}
openssl = {version = "0.9.14", optional = true}
percent-encoding = {version = "1.0.0", optional = true}
regex = {version = "0.2", optional = true}
serde = "1.0"
serde_derive = "1.0"
serde_ignored = {version = "0.0.4", optional = true}
serde_json = {version = "1.0", optional = true}
serde_urlencoded = {version = "0.5.1", optional = true}
tokio-core = {version = "0.1.6", optional = true}
tokio-proto = {version = "0.1.1", optional = true}
tokio-tls = {version = "0.1.3", optional = true, features = ["tokio-proto"]}
url = {version = "1.5", optional = true}
uuid = {version = "0.5", optional = true, features = ["serde", "v4"]}
# ToDo: this should be updated to point at the official crate once
# https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream
[dev-dependencies]
clap = "2.25"
error-chain = "0.12"

View File

@@ -0,0 +1,128 @@
# Rust API for multipart-v3
API under test
## Overview
This client/server was generated by the [openapi-generator]
(https://openapi-generator.tech) project.
By using the [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) from a remote server, you can easily generate a server stub.
-
To see how to make this your own, look here:
[README]((https://openapi-generator.tech))
- API version: 1.0.7
This autogenerated project defines an API crate `multipart-v3` which contains:
* An `Api` trait defining the API in Rust.
* Data types representing the underlying data model.
* A `Client` type which implements `Api` and issues HTTP requests for each operation.
* A router which accepts HTTP requests and invokes the appropriate `Api` method for each operation.
It also contains an example server and client which make use of `multipart-v3`:
* The example server starts up a web server using the `multipart-v3` router,
and supplies a trivial implementation of `Api` which returns failure for every operation.
* The example client provides a CLI which lets you invoke any single operation on the
`multipart-v3` client by passing appropriate arguments on the command line.
You can use the example server and client as a basis for your own code.
See below for [more detail on implementing a server](#writing-a-server).
## Examples
Run examples with:
```
cargo run --example <example-name>
```
To pass in arguments to the examples, put them after `--`, for example:
```
cargo run --example client -- --help
```
### Running the server
To run the server, follow these simple steps:
```
cargo run --example server
```
### Running a client
To run a client, follow one of the following simple steps:
```
cargo run --example client MultipartRequestPost
```
### HTTPS
The examples can be run in HTTPS mode by passing in the flag `--https`, for example:
```
cargo run --example server -- --https
```
This will use the keys/certificates from the examples directory. Note that the server chain is signed with
`CN=localhost`.
## Writing a server
The server example is designed to form the basis for implementing your own server. Simply follow these steps.
* Set up a new Rust project, e.g., with `cargo init --bin`.
* Insert `multipart-v3` into the `members` array under [workspace] in the root `Cargo.toml`, e.g., `members = [ "multipart-v3" ]`.
* Add `multipart-v3 = {version = "1.0.7", path = "multipart-v3"}` under `[dependencies]` in the root `Cargo.toml`.
* Copy the `[dependencies]` and `[dev-dependencies]` from `multipart-v3/Cargo.toml` into the root `Cargo.toml`'s `[dependencies]` section.
* Copy all of the `[dev-dependencies]`, but only the `[dependencies]` that are required by the example server. These should be clearly indicated by comments.
* Remove `"optional = true"` from each of these lines if present.
Each autogenerated API will contain an implementation stub and main entry point, which should be copied into your project the first time:
```
cp multipart-v3/examples/server.rs src/main.rs
cp multipart-v3/examples/server_lib/mod.rs src/lib.rs
cp multipart-v3/examples/server_lib/server.rs src/server.rs
```
Now
* From `src/main.rs`, remove the `mod server_lib;` line, and uncomment and fill in the `extern crate` line with the name of this server crate.
* Move the block of imports "required by the service library" from `src/main.rs` to `src/lib.rs` and uncomment.
* Change the `let server = server::Server {};` line to `let server = SERVICE_NAME::server().unwrap();` where `SERVICE_NAME` is the name of the server crate.
* Run `cargo build` to check it builds.
* Run `cargo fmt` to reformat the code.
* Commit the result before making any further changes (lest format changes get confused with your own updates).
Now replace the implementations in `src/server.rs` with your own code as required.
## Updating your server to track API changes
Later, if the API changes, you can copy new sections from the autogenerated API stub into your implementation.
Alternatively, implement the now-missing methods based on the compiler's error messages.
## Documentation for API Endpoints
All URIs are relative to *http://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
[****](docs/default_api.md#) | **POST** /multipart_request |
## Documentation For Models
- [MultipartRequest](docs/MultipartRequest.md)
- [MultipartRequestObjectField](docs/MultipartRequestObjectField.md)
## Documentation For Authorization
Endpoints do not require authorization.
## Author

View File

@@ -0,0 +1,47 @@
openapi: 3.0.1
info:
description: API under test
title: Multipart OpenAPI V3 Rust Server Test
version: 1.0.7
servers:
- url: /
paths:
/multipart_request:
post:
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/multipart_request'
required: true
responses:
201:
description: OK
components:
schemas:
multipart_request:
properties:
string_field:
type: string
optional_string_field:
type: string
object_field:
$ref: '#/components/schemas/multipart_request_object_field'
binary_field:
format: byte
type: string
required:
- binary_field
- string_field
type: object
multipart_request_object_field:
properties:
field_a:
type: string
field_b:
items:
type: string
type: array
required:
- field_a

View File

@@ -0,0 +1,13 @@
# MultipartRequest
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**string_field** | **String** | |
**optional_string_field** | **String** | | [optional] [default to None]
**object_field** | [***models::MultipartRequestObjectField**](multipart_request_object_field.md) | | [optional] [default to None]
**binary_field** | [***swagger::ByteArray**](ByteArray.md) | |
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@@ -0,0 +1,11 @@
# MultipartRequestObjectField
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**field_a** | **String** | |
**field_b** | **Vec<String>** | | [optional] [default to None]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@@ -0,0 +1,46 @@
# default_api
All URIs are relative to *http://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
****](default_api.md#) | **POST** /multipart_request |
# ****
> (string_field, binary_field, optional)
### Required Parameters
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**string_field** | **String**| |
**binary_field** | **swagger::ByteArray**| |
**optional** | **map[string]interface{}** | optional parameters | nil if no parameters
### Optional Parameters
Optional parameters are passed through a map[string]interface{}.
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**string_field** | **String**| |
**binary_field** | **swagger::ByteArray**| |
**optional_string_field** | **String**| |
**object_field** | [**multipart_request_object_field**](multipart_request_object_field.md)| |
### Return type
(empty response body)
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: multipart/form-data
- **Accept**: Not defined
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

View File

@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICtjCCAZ4CCQDpKecRERZ0xDANBgkqhkiG9w0BAQsFADAdMQswCQYDVQQGEwJV
UzEOMAwGA1UEAxMFTXkgQ0EwHhcNMTcwNTIzMTYwMDIzWhcNMTcwNjIyMTYwMDIz
WjAdMQswCQYDVQQGEwJVUzEOMAwGA1UEAxMFTXkgQ0EwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQCt66py3x7sCSASRF2D05L5wkNDxAUjQKYx23W8Gbwv
GMGykk89BIdU5LX1JB1cKiUOkoIxfwAYuWc2V/wzTvVV7+11besnk3uX1c9KiqUF
LIX7kn/z5hzS4aelhKvH+MJlSZCSlp1ytpZbwo5GB5Pi2SGH56jDBiBoDRNBVdWL
z4wH7TdrQjqWwNxIZumD5OGMtcfJyuX08iPiEOaslOeoMqzObhvjc9aUgjVjhqyA
FkJGTXsi0oaD7oml+NE+mTNfEeZvEJQpLSjBY0OvQHzuHkyGBShBnfu/9x7/NRwd
WaqsLiF7/re9KDGYdJwP7Cu6uxYfKAyWarp6h2mG/GIdAgMBAAEwDQYJKoZIhvcN
AQELBQADggEBAGIl/VVIafeq/AJOQ9r7TzzB2ABJYr7NZa6bTu5O1jSp1Fonac15
SZ8gvRxODgH22ZYSqghPG4xzq4J3hkytlQqm57ZEt2I2M3OqIp17Ndcc1xDYzpLl
tA0FrVn6crQTM8vQkTDtGesaCWX+7Fir5dK7HnYWzfpSmsOpST07PfbNisEXKOxG
Dj4lBL1OnhTjsJeymVS1pFvkKkrcEJO+IxFiHL3CDsWjcXB0Z+E1zBtPoYyYsNsO
rBrjUxcZewF4xqWZhpW90Mt61fY2nRgU0uUwHcvDQUqvmzKcsqYa4mPKzfBI5mxo
01Ta96cDD6pS5Y1hOflZ0g84f2g/7xBLLDA=
-----END CERTIFICATE-----

View File

@@ -0,0 +1,82 @@
#![allow(missing_docs, unused_variables, trivial_casts)]
extern crate multipart_v3;
#[allow(unused_extern_crates)]
extern crate futures;
#[allow(unused_extern_crates)]
#[macro_use]
extern crate swagger;
#[allow(unused_extern_crates)]
extern crate uuid;
extern crate clap;
extern crate tokio_core;
use swagger::{ContextBuilder, EmptyContext, XSpanIdString, Has, Push, AuthData};
#[allow(unused_imports)]
use futures::{Future, future, Stream, stream};
use tokio_core::reactor;
#[allow(unused_imports)]
use multipart_v3::{ApiNoContext, ContextWrapperExt,
ApiError,
MultipartRequestPostResponse
};
use clap::{App, Arg};
fn main() {
let matches = App::new("client")
.arg(Arg::with_name("operation")
.help("Sets the operation to run")
.possible_values(&[
"MultipartRequestPost",
])
.required(true)
.index(1))
.arg(Arg::with_name("https")
.long("https")
.help("Whether to use HTTPS or not"))
.arg(Arg::with_name("host")
.long("host")
.takes_value(true)
.default_value("localhost")
.help("Hostname to contact"))
.arg(Arg::with_name("port")
.long("port")
.takes_value(true)
.default_value("80")
.help("Port to contact"))
.get_matches();
let mut core = reactor::Core::new().unwrap();
let is_https = matches.is_present("https");
let base_url = format!("{}://{}:{}",
if is_https { "https" } else { "http" },
matches.value_of("host").unwrap(),
matches.value_of("port").unwrap());
let client = if matches.is_present("https") {
// Using Simple HTTPS
multipart_v3::Client::try_new_https(core.handle(), &base_url, "examples/ca.pem")
.expect("Failed to create HTTPS client")
} else {
// Using HTTP
multipart_v3::Client::try_new_http(core.handle(), &base_url)
.expect("Failed to create HTTP client")
};
let context: make_context_ty!(ContextBuilder, EmptyContext, Option<AuthData>, XSpanIdString) =
make_context!(ContextBuilder, EmptyContext, None as Option<AuthData>, XSpanIdString(self::uuid::Uuid::new_v4().to_string()));
let client = client.with_context(context);
match matches.value_of("operation") {
Some("MultipartRequestPost") => {
let result = core.run(client.multipart_request_post("string_field_example".to_string(), swagger::ByteArray(Vec::from("BYTE_ARRAY_DATA_HERE")), Some("optional_string_field_example".to_string()), None));
println!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
},
_ => {
panic!("Invalid operation provided")
}
}
}

View File

@@ -0,0 +1,66 @@
Certificate:
Data:
Version: 1 (0x0)
Serial Number: 4096 (0x1000)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=US, CN=My CA
Validity
Not Before: May 23 16:00:23 2017 GMT
Not After : Apr 29 16:00:23 2117 GMT
Subject: CN=localhost, C=US
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:c9:d4:43:60:50:fc:d6:0f:38:4d:5d:5e:aa:7c:
c0:5e:a9:ec:d9:93:78:d3:93:72:28:41:f5:08:a5:
ea:ac:67:07:d7:1f:f7:7d:74:69:7e:46:89:20:4b:
7a:2d:9b:02:08:e7:6f:0f:1d:0c:0f:c7:60:69:19:
4b:df:7e:ca:75:94:0b:49:71:e3:6d:f2:e8:79:fd:
ed:0a:94:67:55:f3:ca:6b:61:ba:58:b7:2e:dd:7b:
ca:b9:02:9f:24:36:ac:26:8f:04:8f:81:c8:35:10:
f4:aa:33:b2:24:16:f8:f7:1e:ea:f7:16:fe:fa:34:
c3:dd:bb:2c:ba:7a:df:4d:e2:da:1e:e5:d2:28:44:
6e:c8:96:e0:fd:09:0c:14:0c:31:dc:e0:ca:c1:a7:
9b:bf:16:8c:f7:36:3f:1b:2e:dd:90:eb:45:78:51:
bf:59:22:1e:c6:8c:0a:69:88:e5:03:5e:73:b7:fc:
93:7f:1b:46:1b:97:68:c5:c0:8b:35:1f:bb:1e:67:
7f:55:b7:3b:55:3f:ea:f2:ca:db:cc:52:cd:16:89:
db:15:47:bd:f2:cd:6c:7a:d7:b4:1a:ac:c8:15:6c:
6a:fb:77:c4:e9:f2:30:e0:14:24:66:65:6f:2a:e5:
2d:cc:f6:81:ae:57:c8:d1:9b:38:90:dc:60:93:02:
5e:cb
Exponent: 65537 (0x10001)
Signature Algorithm: sha256WithRSAEncryption
1c:7c:39:e8:3d:49:b2:09:1e:68:5a:2f:74:18:f4:63:b5:8c:
f6:e6:a1:e3:4d:95:90:99:ef:32:5c:34:40:e8:55:13:0e:e0:
1c:be:cd:ab:3f:64:38:99:5e:2b:c1:81:53:a0:18:a8:f6:ee:
6a:33:73:6c:9a:73:9d:86:08:5d:c7:11:38:46:4c:cd:a0:47:
37:8f:fe:a6:50:a9:02:21:99:42:86:5e:47:fe:65:56:60:1d:
16:53:86:bd:e4:63:c5:69:cf:fa:30:51:ab:a1:c3:50:53:cc:
66:1c:4c:ff:3f:2a:39:4d:a2:8f:9d:d1:a7:8b:22:e4:78:69:
24:06:83:4d:cc:0a:c0:87:69:9b:bc:80:a9:d2:b7:a5:23:84:
7e:a2:32:26:7c:78:0e:bd:db:cd:3b:69:18:33:b8:44:ef:96:
b4:99:86:ee:06:bd:51:1c:c7:a1:a4:0c:c4:4c:51:a0:df:ac:
14:07:88:8e:d7:39:45:fe:52:e0:a3:4c:db:5d:7a:ab:4d:e4:
ca:06:e8:bd:74:6f:46:e7:93:4a:4f:1b:67:e7:a5:9f:ef:9c:
02:49:d1:f2:d5:e9:53:ee:09:21:ac:08:c8:15:f7:af:35:b9:
4f:11:0f:43:ae:46:8e:fd:5b:8d:a3:4e:a7:2c:b7:25:ed:e4:
e5:94:1d:e3
-----BEGIN CERTIFICATE-----
MIICtTCCAZ0CAhAAMA0GCSqGSIb3DQEBCwUAMB0xCzAJBgNVBAYTAlVTMQ4wDAYD
VQQDEwVNeSBDQTAgFw0xNzA1MjMxNjAwMjNaGA8yMTE3MDQyOTE2MDAyM1owITES
MBAGA1UEAxMJbG9jYWxob3N0MQswCQYDVQQGEwJVUzCCASIwDQYJKoZIhvcNAQEB
BQADggEPADCCAQoCggEBAMnUQ2BQ/NYPOE1dXqp8wF6p7NmTeNOTcihB9Qil6qxn
B9cf9310aX5GiSBLei2bAgjnbw8dDA/HYGkZS99+ynWUC0lx423y6Hn97QqUZ1Xz
ymthuli3Lt17yrkCnyQ2rCaPBI+ByDUQ9KozsiQW+Pce6vcW/vo0w927LLp6303i
2h7l0ihEbsiW4P0JDBQMMdzgysGnm78WjPc2Pxsu3ZDrRXhRv1kiHsaMCmmI5QNe
c7f8k38bRhuXaMXAizUfux5nf1W3O1U/6vLK28xSzRaJ2xVHvfLNbHrXtBqsyBVs
avt3xOnyMOAUJGZlbyrlLcz2ga5XyNGbOJDcYJMCXssCAwEAATANBgkqhkiG9w0B
AQsFAAOCAQEAHHw56D1JsgkeaFovdBj0Y7WM9uah402VkJnvMlw0QOhVEw7gHL7N
qz9kOJleK8GBU6AYqPbuajNzbJpznYYIXccROEZMzaBHN4/+plCpAiGZQoZeR/5l
VmAdFlOGveRjxWnP+jBRq6HDUFPMZhxM/z8qOU2ij53Rp4si5HhpJAaDTcwKwIdp
m7yAqdK3pSOEfqIyJnx4Dr3bzTtpGDO4RO+WtJmG7ga9URzHoaQMxExRoN+sFAeI
jtc5Rf5S4KNM2116q03kygbovXRvRueTSk8bZ+eln++cAknR8tXpU+4JIawIyBX3
rzW5TxEPQ65Gjv1bjaNOpyy3Je3k5ZQd4w==
-----END CERTIFICATE-----

View File

@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDJ1ENgUPzWDzhN
XV6qfMBeqezZk3jTk3IoQfUIpeqsZwfXH/d9dGl+RokgS3otmwII528PHQwPx2Bp
GUvffsp1lAtJceNt8uh5/e0KlGdV88prYbpYty7de8q5Ap8kNqwmjwSPgcg1EPSq
M7IkFvj3Hur3Fv76NMPduyy6et9N4toe5dIoRG7IluD9CQwUDDHc4MrBp5u/Foz3
Nj8bLt2Q60V4Ub9ZIh7GjAppiOUDXnO3/JN/G0Ybl2jFwIs1H7seZ39VtztVP+ry
ytvMUs0WidsVR73yzWx617QarMgVbGr7d8Tp8jDgFCRmZW8q5S3M9oGuV8jRmziQ
3GCTAl7LAgMBAAECggEBAKEd1q9j14KWYc64s6KLthGbutyxsinMMbxbct11fdIk
6YhdF3fJ35ETg9IJDr6rWEN9ZRX+jStncNpVfFEs6ThVd3Eo/nI+EEGaaIkikR93
X2a7fEPn7/yVHu70XdBN6L1bPDvHUeiy4W2hmRrgT90OjGm1rNRWHOm7yugOwIZu
HclzbR9Ca7EInFnotUiDQm9sw9VKHbJHqWx6OORdZrxR2ytYs0Qkq0XpGMvti2HW
7WAmKTg5QM8myXW7+/4iqb/u68wVBR2BBalShKmIf7lim9O3W2a1RjDdsvm/wNe9
I+D+Iq825vpqkKXcrxYlpVg7hYiaQaW/MNsEb7lQRjECgYEA/RJYby0POW+/k0Jn
jO8UmJVEMiuGa8WIUu/JJWMOmzRCukjSRNQOkt7niQrZPJYE8W6clM6RJTolWf9L
IL6mIb+mRaoudUk8SHGDq7ho1iMg9GK8lhYxvKh1Q6uv8EyVSkgLknAEY0NANKC1
zNdU5Dhven9aRX2gq9vP4XwMz2MCgYEAzCogQ7IFk+gkp3k491dOZnrGRoRCfuzo
4CJtyKFgOSd7BjmpcKkj0IPfVBjw6GjMIxfQRMTQmxAjjWevH45vG8l0Iiwz/gSp
81b5nsDEX5uv2Olcmcz5zxRFy36jOZ9ihMWinxcIlT2oDbyCdbruDKZq9ieJ9S8g
4qGx0OkwE3kCgYEA7CmAiU89U9YqqttfEq/RQoqY91CSwmO10d+ej9seuEtOsdRf
FIfnibulycdr7hP5TOxyBpO1802NqayJiWcgVYIpQf2MGTtcnCYCP+95NcvWZvj1
EAJqK6nwtFO1fcOZ1ZXh5qfOEGujsPkAbsXLnKXlsiTCMvMHSxl3pu5Cbg0CgYBf
JjbZNctRrjv+7Qj2hPLd4dQsIxGWc7ToWENP4J2mpVa5hQAJqFovoHXhjKohtk2F
AWEn243Y5oGbMjo0e74edhmwn2cvuF64MM2vBem/ISCn98IXT6cQskMA3qkVfsl8
VVs/x41ReGWs2TD3y0GMFbb9t1mdMfSiincDhNnKCQKBgGfeT4jKyYeCoCw4OLI1
G75Gd0METt/IkppwODPpNwj3Rp9I5jctWZFA/3wCX/zk0HgBeou5AFNS4nQZ/X/L
L9axbSdR7UJTGkT1r4gu3rLkPV4Tk+8XM03/JT2cofMlzQBuhvl1Pn4SgKowz7hl
lS76ECw4Av3T0S34VW9Z5oye
-----END PRIVATE KEY-----

View File

@@ -0,0 +1,75 @@
//! Main binary entry point for multipart_v3 implementation.
#![allow(missing_docs)]
// Imports required by this file.
// extern crate <name of this crate>;
extern crate multipart_v3;
extern crate swagger;
extern crate hyper;
extern crate openssl;
extern crate native_tls;
extern crate tokio_proto;
extern crate tokio_tls;
extern crate clap;
// Imports required by server library.
// extern crate multipart_v3;
// extern crate swagger;
extern crate futures;
extern crate chrono;
#[macro_use]
extern crate error_chain;
use openssl::x509::X509_FILETYPE_PEM;
use openssl::ssl::{SslAcceptorBuilder, SslMethod};
use openssl::error::ErrorStack;
use hyper::server::Http;
use tokio_proto::TcpServer;
use clap::{App, Arg};
use swagger::auth::AllowAllAuthenticator;
use swagger::EmptyContext;
mod server_lib;
// Builds an SSL implementation for Simple HTTPS from some hard-coded file names
fn ssl() -> Result<SslAcceptorBuilder, ErrorStack> {
let mut ssl = SslAcceptorBuilder::mozilla_intermediate_raw(SslMethod::tls())?;
// Server authentication
ssl.set_private_key_file("examples/server-key.pem", X509_FILETYPE_PEM)?;
ssl.set_certificate_chain_file("examples/server-chain.pem")?;
ssl.check_private_key()?;
Ok(ssl)
}
/// Create custom server, wire it to the autogenerated router,
/// and pass it to the web server.
fn main() {
let matches = App::new("server")
.arg(Arg::with_name("https")
.long("https")
.help("Whether to use HTTPS or not"))
.get_matches();
let service_fn =
multipart_v3::server::context::NewAddContext::<_, EmptyContext>::new(
AllowAllAuthenticator::new(
server_lib::NewService::new(),
"cosmo"
)
);
let addr = "127.0.0.1:80".parse().expect("Failed to parse bind address");
if matches.is_present("https") {
let ssl = ssl().expect("Failed to load SSL keys");
let builder: native_tls::TlsAcceptorBuilder = native_tls::backend::openssl::TlsAcceptorBuilderExt::from_openssl(ssl);
let tls_acceptor = builder.build().expect("Failed to build TLS acceptor");
TcpServer::new(tokio_tls::proto::Server::new(Http::new(), tls_acceptor), addr).serve(service_fn);
} else {
// Using HTTP
TcpServer::new(Http::new(), addr).serve(service_fn);
}
}

View File

@@ -0,0 +1,37 @@
//! Main library entry point for multipart_v3 implementation.
mod server;
mod errors {
error_chain!{}
}
pub use self::errors::*;
use std::io;
use std::clone::Clone;
use std::marker::PhantomData;
use hyper;
use multipart_v3;
use swagger::{Has, XSpanIdString};
pub struct NewService<C>{
marker: PhantomData<C>
}
impl<C> NewService<C>{
pub fn new() -> Self {
NewService{marker:PhantomData}
}
}
impl<C> hyper::server::NewService for NewService<C> where C: Has<XSpanIdString> + Clone + 'static {
type Request = (hyper::Request, C);
type Response = hyper::Response;
type Error = hyper::Error;
type Instance = multipart_v3::server::Service<server::Server<C>, C>;
/// Instantiate a new server.
fn new_service(&self) -> io::Result<Self::Instance> {
Ok(multipart_v3::server::Service::new(server::Server::new()))
}
}

View File

@@ -0,0 +1,38 @@
//! Server implementation of multipart_v3.
#![allow(unused_imports)]
use futures::{self, Future};
use chrono;
use std::collections::HashMap;
use std::marker::PhantomData;
use swagger;
use swagger::{Has, XSpanIdString};
use multipart_v3::{Api, ApiError,
MultipartRequestPostResponse
};
use multipart_v3::models;
#[derive(Copy, Clone)]
pub struct Server<C> {
marker: PhantomData<C>,
}
impl<C> Server<C> {
pub fn new() -> Self {
Server{marker: PhantomData}
}
}
impl<C> Api<C> for Server<C> where C: Has<XSpanIdString>{
fn multipart_request_post(&self, string_field: String, binary_field: swagger::ByteArray, optional_string_field: Option<String>, object_field: Option<models::MultipartRequestObjectField>, context: &C) -> Box<Future<Item=MultipartRequestPostResponse, Error=ApiError>> {
let context = context.clone();
println!("multipart_request_post(\"{}\", {:?}, {:?}, {:?}) - X-Span-ID: {:?}", string_field, binary_field, optional_string_field, object_field, context.get().0.clone());
Box::new(futures::failed("Generic failure".into()))
}
}

View File

@@ -0,0 +1,417 @@
#![allow(unused_extern_crates)]
extern crate tokio_core;
extern crate native_tls;
extern crate hyper_tls;
extern crate openssl;
extern crate mime;
extern crate chrono;
extern crate url;
extern crate multipart;
use hyper;
use hyper::header::{Headers, ContentType};
use hyper::Uri;
use self::url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, QUERY_ENCODE_SET};
use futures;
use futures::{Future, Stream};
use futures::{future, stream};
use self::tokio_core::reactor::Handle;
use std::borrow::Cow;
use std::io::{Read, Error, ErrorKind};
use std::error;
use std::fmt;
use std::path::Path;
use std::sync::Arc;
use std::str;
use std::str::FromStr;
use std::string::ToString;
use hyper::mime::Mime;
use std::io::Cursor;
use client::multipart::client::lazy::Multipart;
use mimetypes;
use serde_json;
#[allow(unused_imports)]
use std::collections::{HashMap, BTreeMap};
#[allow(unused_imports)]
use swagger;
use swagger::{ApiError, XSpanId, XSpanIdString, Has, AuthData};
use {Api,
MultipartRequestPostResponse
};
use models;
define_encode_set! {
/// This encode set is used for object IDs
///
/// Aside from the special characters defined in the `PATH_SEGMENT_ENCODE_SET`,
/// the vertical bar (|) is encoded.
pub ID_ENCODE_SET = [PATH_SEGMENT_ENCODE_SET] | {'|'}
}
/// Convert input into a base path, e.g. "http://example:123". Also checks the scheme as it goes.
fn into_base_path(input: &str, correct_scheme: Option<&'static str>) -> Result<String, ClientInitError> {
// First convert to Uri, since a base path is a subset of Uri.
let uri = Uri::from_str(input)?;
let scheme = uri.scheme().ok_or(ClientInitError::InvalidScheme)?;
// Check the scheme if necessary
if let Some(correct_scheme) = correct_scheme {
if scheme != correct_scheme {
return Err(ClientInitError::InvalidScheme);
}
}
let host = uri.host().ok_or_else(|| ClientInitError::MissingHost)?;
let port = uri.port().map(|x| format!(":{}", x)).unwrap_or_default();
Ok(format!("{}://{}{}", scheme, host, port))
}
/// A client that implements the API by making HTTP calls out to a server.
pub struct Client<F> where
F: Future<Item=hyper::Response, Error=hyper::Error> + 'static {
client_service: Arc<Box<hyper::client::Service<Request=hyper::Request<hyper::Body>, Response=hyper::Response, Error=hyper::Error, Future=F>>>,
base_path: String,
}
impl<F> fmt::Debug for Client<F> where
F: Future<Item=hyper::Response, Error=hyper::Error> + 'static {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Client {{ base_path: {} }}", self.base_path)
}
}
impl<F> Clone for Client<F> where
F: Future<Item=hyper::Response, Error=hyper::Error> + 'static {
fn clone(&self) -> Self {
Client {
client_service: self.client_service.clone(),
base_path: self.base_path.clone()
}
}
}
impl Client<hyper::client::FutureResponse> {
/// Create an HTTP client.
///
/// # Arguments
/// * `handle` - tokio reactor handle to use for execution
/// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com"
pub fn try_new_http(handle: Handle, base_path: &str) -> Result<Client<hyper::client::FutureResponse>, ClientInitError> {
let http_connector = swagger::http_connector();
Self::try_new_with_connector::<hyper::client::HttpConnector>(
handle,
base_path,
Some("http"),
http_connector,
)
}
/// Create a client with a TLS connection to the server.
///
/// # Arguments
/// * `handle` - tokio reactor handle to use for execution
/// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com"
/// * `ca_certificate` - Path to CA certificate used to authenticate the server
pub fn try_new_https<CA>(
handle: Handle,
base_path: &str,
ca_certificate: CA,
) -> Result<Client<hyper::client::FutureResponse>, ClientInitError>
where
CA: AsRef<Path>,
{
let https_connector = swagger::https_connector(ca_certificate);
Self::try_new_with_connector::<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>(
handle,
base_path,
Some("https"),
https_connector,
)
}
/// Create a client with a mutually authenticated TLS connection to the server.
///
/// # Arguments
/// * `handle` - tokio reactor handle to use for execution
/// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com"
/// * `ca_certificate` - Path to CA certificate used to authenticate the server
/// * `client_key` - Path to the client private key
/// * `client_certificate` - Path to the client's public certificate associated with the private key
pub fn try_new_https_mutual<CA, K, C>(
handle: Handle,
base_path: &str,
ca_certificate: CA,
client_key: K,
client_certificate: C,
) -> Result<Client<hyper::client::FutureResponse>, ClientInitError>
where
CA: AsRef<Path>,
K: AsRef<Path>,
C: AsRef<Path>,
{
let https_connector =
swagger::https_mutual_connector(ca_certificate, client_key, client_certificate);
Self::try_new_with_connector::<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>(
handle,
base_path,
Some("https"),
https_connector,
)
}
/// Create a client with a custom implementation of hyper::client::Connect.
///
/// Intended for use with custom implementations of connect for e.g. protocol logging
/// or similar functionality which requires wrapping the transport layer. When wrapping a TCP connection,
/// this function should be used in conjunction with
/// `swagger::{http_connector, https_connector, https_mutual_connector}`.
///
/// For ordinary tcp connections, prefer the use of `try_new_http`, `try_new_https`
/// and `try_new_https_mutual`, to avoid introducing a dependency on the underlying transport layer.
///
/// # Arguments
///
/// * `handle` - tokio reactor handle to use for execution
/// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com"
/// * `protocol` - Which protocol to use when constructing the request url, e.g. `Some("http")`
/// * `connector_fn` - Function which returns an implementation of `hyper::client::Connect`
pub fn try_new_with_connector<C>(
handle: Handle,
base_path: &str,
protocol: Option<&'static str>,
connector_fn: Box<Fn(&Handle) -> C + Send + Sync>,
) -> Result<Client<hyper::client::FutureResponse>, ClientInitError>
where
C: hyper::client::Connect + hyper::client::Service,
{
let connector = connector_fn(&handle);
let client_service = Box::new(hyper::Client::configure().connector(connector).build(
&handle,
));
Ok(Client {
client_service: Arc::new(client_service),
base_path: into_base_path(base_path, protocol)?,
})
}
/// Constructor for creating a `Client` by passing in a pre-made `hyper` client.
///
/// One should avoid relying on this function if possible, since it adds a dependency on the underlying transport
/// implementation, which it would be better to abstract away. Therefore, using this function may lead to a loss of
/// code generality, which may make it harder to move the application to a serverless environment, for example.
///
/// The reason for this function's existence is to support legacy test code, which did mocking at the hyper layer.
/// This is not a recommended way to write new tests. If other reasons are found for using this function, they
/// should be mentioned here.
#[deprecated(note="Use try_new_with_client_service instead")]
pub fn try_new_with_hyper_client(
hyper_client: Arc<Box<hyper::client::Service<Request=hyper::Request<hyper::Body>, Response=hyper::Response, Error=hyper::Error, Future=hyper::client::FutureResponse>>>,
handle: Handle,
base_path: &str
) -> Result<Client<hyper::client::FutureResponse>, ClientInitError>
{
Ok(Client {
client_service: hyper_client,
base_path: into_base_path(base_path, None)?,
})
}
}
impl<F> Client<F> where
F: Future<Item=hyper::Response, Error=hyper::Error> + 'static
{
/// Constructor for creating a `Client` by passing in a pre-made `hyper` client Service.
///
/// This allows adding custom wrappers around the underlying transport, for example for logging.
pub fn try_new_with_client_service(client_service: Arc<Box<hyper::client::Service<Request=hyper::Request<hyper::Body>, Response=hyper::Response, Error=hyper::Error, Future=F>>>,
handle: Handle,
base_path: &str)
-> Result<Client<F>, ClientInitError>
{
Ok(Client {
client_service: client_service,
base_path: into_base_path(base_path, None)?,
})
}
}
impl<F, C> Api<C> for Client<F> where
F: Future<Item=hyper::Response, Error=hyper::Error> + 'static,
C: Has<XSpanIdString> {
fn multipart_request_post(&self, param_string_field: String, param_binary_field: swagger::ByteArray, param_optional_string_field: Option<String>, param_object_field: Option<models::MultipartRequestObjectField>, context: &C) -> Box<Future<Item=MultipartRequestPostResponse, Error=ApiError>> {
let mut uri = format!(
"{}/multipart_request",
self.base_path
);
let mut query_string = self::url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build URI: {}", err))))),
};
let mut request = hyper::Request::new(hyper::Method::Post, uri);
let mut multipart = Multipart::new();
// For each parameter, encode as appropriate and add to the multipart body as a stream.
let string_field_str = match serde_json::to_string(&param_string_field) {
Ok(str) => str,
Err(e) => return Box::new(futures::done(Err(ApiError(format!("Unable to parse string_field to string: {}", e))))),
};
let string_field_vec = string_field_str.as_bytes().to_vec();
let string_field_mime = mime::Mime::from_str("application/json").expect("impossible to fail to parse");
let string_field_cursor = Cursor::new(string_field_vec);
multipart.add_stream("string_field", string_field_cursor, None as Option<&str>, Some(string_field_mime));
let optional_string_field_str = match serde_json::to_string(&param_optional_string_field) {
Ok(str) => str,
Err(e) => return Box::new(futures::done(Err(ApiError(format!("Unable to parse optional_string_field to string: {}", e))))),
};
let optional_string_field_vec = optional_string_field_str.as_bytes().to_vec();
let optional_string_field_mime = mime::Mime::from_str("application/json").expect("impossible to fail to parse");
let optional_string_field_cursor = Cursor::new(optional_string_field_vec);
multipart.add_stream("optional_string_field", optional_string_field_cursor, None as Option<&str>, Some(optional_string_field_mime));
let object_field_str = match serde_json::to_string(&param_object_field) {
Ok(str) => str,
Err(e) => return Box::new(futures::done(Err(ApiError(format!("Unable to parse object_field to string: {}", e))))),
};
let object_field_vec = object_field_str.as_bytes().to_vec();
let object_field_mime = mime::Mime::from_str("application/json").expect("impossible to fail to parse");
let object_field_cursor = Cursor::new(object_field_vec);
multipart.add_stream("object_field", object_field_cursor, None as Option<&str>, Some(object_field_mime));
let binary_field_vec = param_binary_field.to_vec();
let binary_field_mime = match mime::Mime::from_str("application/octet-stream") {
Ok(mime) => mime,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to get mime type: {:?}", err))))),
};
let binary_field_cursor = Cursor::new(binary_field_vec);
let filename = None as Option<&str> ;
multipart.add_stream("binary_field", binary_field_cursor, filename, Some(binary_field_mime));
let mut fields = match multipart.prepare() {
Ok(fields) => fields,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build request: {}", err))))),
};
let mut body_string = String::new();
fields.to_body().read_to_string(&mut body_string).unwrap();
let boundary = fields.boundary();
let multipart_header = match Mime::from_str(&format!("multipart/form-data;boundary={}", boundary)) {
Ok(multipart_header) => multipart_header,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build multipart header: {:?}", err))))),
};
request.set_body(body_string.into_bytes());
request.headers_mut().set(ContentType(multipart_header));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
201 => {
let body = response.body();
Box::new(
future::ok(
MultipartRequestPostResponse::OK
)
) as Box<Future<Item=_, Error=_>>
},
code => {
let headers = response.headers().clone();
Box::new(response.body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<Future<Item=_, Error=_>>
}
}
}))
}
}
#[derive(Debug)]
pub enum ClientInitError {
InvalidScheme,
InvalidUri(hyper::error::UriError),
MissingHost,
SslError(openssl::error::ErrorStack)
}
impl From<hyper::error::UriError> for ClientInitError {
fn from(err: hyper::error::UriError) -> ClientInitError {
ClientInitError::InvalidUri(err)
}
}
impl From<openssl::error::ErrorStack> for ClientInitError {
fn from(err: openssl::error::ErrorStack) -> ClientInitError {
ClientInitError::SslError(err)
}
}
impl fmt::Display for ClientInitError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self as &fmt::Debug).fmt(f)
}
}
impl error::Error for ClientInitError {
fn description(&self) -> &str {
"Failed to produce a hyper client."
}
}

View File

@@ -0,0 +1,101 @@
#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, non_camel_case_types)]
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate futures;
extern crate chrono;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate mime;
// Logically this should be in the client and server modules, but rust doesn't allow `macro_use` from a module.
#[cfg(any(feature = "client", feature = "server"))]
#[macro_use]
extern crate hyper;
extern crate swagger;
#[macro_use]
extern crate url;
use futures::Stream;
use std::io::Error;
#[allow(unused_imports)]
use std::collections::HashMap;
pub use futures::Future;
#[cfg(any(feature = "client", feature = "server"))]
mod mimetypes;
pub use swagger::{ApiError, ContextWrapper};
pub const BASE_PATH: &'static str = "";
pub const API_VERSION: &'static str = "1.0.7";
#[derive(Debug, PartialEq)]
pub enum MultipartRequestPostResponse {
/// OK
OK ,
}
/// API
pub trait Api<C> {
fn multipart_request_post(&self, string_field: String, binary_field: swagger::ByteArray, optional_string_field: Option<String>, object_field: Option<models::MultipartRequestObjectField>, context: &C) -> Box<Future<Item=MultipartRequestPostResponse, Error=ApiError>>;
}
/// API without a `Context`
pub trait ApiNoContext {
fn multipart_request_post(&self, string_field: String, binary_field: swagger::ByteArray, optional_string_field: Option<String>, object_field: Option<models::MultipartRequestObjectField>) -> Box<Future<Item=MultipartRequestPostResponse, Error=ApiError>>;
}
/// Trait to extend an API to make it easy to bind it to a context.
pub trait ContextWrapperExt<'a, C> where Self: Sized {
/// Binds this API to a context.
fn with_context(self: &'a Self, context: C) -> ContextWrapper<'a, Self, C>;
}
impl<'a, T: Api<C> + Sized, C> ContextWrapperExt<'a, C> for T {
fn with_context(self: &'a T, context: C) -> ContextWrapper<'a, T, C> {
ContextWrapper::<T, C>::new(self, context)
}
}
impl<'a, T: Api<C>, C> ApiNoContext for ContextWrapper<'a, T, C> {
fn multipart_request_post(&self, string_field: String, binary_field: swagger::ByteArray, optional_string_field: Option<String>, object_field: Option<models::MultipartRequestObjectField>) -> Box<Future<Item=MultipartRequestPostResponse, Error=ApiError>> {
self.api().multipart_request_post(string_field, binary_field, optional_string_field, object_field, &self.context())
}
}
#[cfg(feature = "client")]
pub mod client;
// Re-export Client as a top-level name
#[cfg(feature = "client")]
pub use self::client::Client;
#[cfg(feature = "server")]
pub mod server;
// Re-export router() as a top-level name
#[cfg(feature = "server")]
pub use self::server::Service;
pub mod models;

View File

@@ -0,0 +1,17 @@
/// mime types for requests and responses
pub mod responses {
use hyper::mime::*;
// The macro is called per-operation to beat the recursion limit
}
pub mod requests {
use hyper::mime::*;
/// Create Mime objects for the request content types for MultipartRequestPost
lazy_static! {
pub static ref MULTIPART_REQUEST_POST: Mime = "multipart/form-data".parse().unwrap();
}
}

View File

@@ -0,0 +1,63 @@
#![allow(unused_imports, unused_qualifications, unused_extern_crates)]
extern crate chrono;
extern crate uuid;
use serde::ser::Serializer;
use std::collections::HashMap;
use models;
use swagger;
use std::string::ParseError;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MultipartRequest {
#[serde(rename = "string_field")]
pub string_field: String,
#[serde(rename = "optional_string_field")]
#[serde(skip_serializing_if="Option::is_none")]
pub optional_string_field: Option<String>,
#[serde(rename = "object_field")]
#[serde(skip_serializing_if="Option::is_none")]
pub object_field: Option<models::MultipartRequestObjectField>,
#[serde(rename = "binary_field")]
pub binary_field: swagger::ByteArray,
}
impl MultipartRequest {
pub fn new(string_field: String, binary_field: swagger::ByteArray, ) -> MultipartRequest {
MultipartRequest {
string_field: string_field,
optional_string_field: None,
object_field: None,
binary_field: binary_field,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MultipartRequestObjectField {
#[serde(rename = "field_a")]
pub field_a: String,
#[serde(rename = "field_b")]
#[serde(skip_serializing_if="Option::is_none")]
pub field_b: Option<Vec<String>>,
}
impl MultipartRequestObjectField {
pub fn new(field_a: String, ) -> MultipartRequestObjectField {
MultipartRequestObjectField {
field_a: field_a,
field_b: None,
}
}
}

View File

@@ -0,0 +1,91 @@
use std::io;
use std::marker::PhantomData;
use std::default::Default;
use hyper;
use hyper::{Request, Response, Error, StatusCode};
use server::url::form_urlencoded;
use swagger::auth::{Authorization, AuthData, Scopes};
use swagger::{Has, Pop, Push, XSpanIdString};
use Api;
pub struct NewAddContext<T, A>
{
inner: T,
marker: PhantomData<A>,
}
impl<T, A, B, C, D> NewAddContext<T, A>
where
A: Default + Push<XSpanIdString, Result=B>,
B: Push<Option<AuthData>, Result=C>,
C: Push<Option<Authorization>, Result=D>,
T: hyper::server::NewService<Request = (Request, D), Response = Response, Error = Error> + 'static,
{
pub fn new(inner: T) -> NewAddContext<T, A> {
NewAddContext {
inner,
marker: PhantomData,
}
}
}
impl<T, A, B, C, D> hyper::server::NewService for NewAddContext<T, A>
where
A: Default + Push<XSpanIdString, Result=B>,
B: Push<Option<AuthData>, Result=C>,
C: Push<Option<Authorization>, Result=D>,
T: hyper::server::NewService<Request = (Request, D), Response = Response, Error = Error> + 'static,
{
type Request = Request;
type Response = Response;
type Error = Error;
type Instance = AddContext<T::Instance, A>;
fn new_service(&self) -> Result<Self::Instance, io::Error> {
self.inner.new_service().map(|s| AddContext::new(s))
}
}
/// Middleware to extract authentication data from request
pub struct AddContext<T, A>
{
inner: T,
marker: PhantomData<A>,
}
impl<T, A, B, C, D> AddContext<T, A>
where
A: Default + Push<XSpanIdString, Result=B>,
B: Push<Option<AuthData>, Result=C>,
C: Push<Option<Authorization>, Result=D>,
T: hyper::server::Service<Request = (Request, D), Response = Response, Error = Error>,
{
pub fn new(inner: T) -> AddContext<T, A> {
AddContext {
inner,
marker: PhantomData,
}
}
}
impl<T, A, B, C, D> hyper::server::Service for AddContext<T, A>
where
A: Default + Push<XSpanIdString, Result=B>,
B: Push<Option<AuthData>, Result=C>,
C: Push<Option<Authorization>, Result=D>,
T: hyper::server::Service<Request = (Request, D), Response = Response, Error = Error>,
{
type Request = Request;
type Response = Response;
type Error = Error;
type Future = T::Future;
fn call(&self, req: Self::Request) -> Self::Future {
let context = A::default().push(XSpanIdString::get_or_generate(&req));
let context = context.push(None::<AuthData>);
let context = context.push(None::<Authorization>);
return self.inner.call((req, context));
}
}

View File

@@ -0,0 +1,277 @@
#![allow(unused_extern_crates)]
extern crate serde_ignored;
extern crate tokio_core;
extern crate native_tls;
extern crate hyper_tls;
extern crate openssl;
extern crate mime;
extern crate chrono;
extern crate percent_encoding;
extern crate url;
extern crate uuid;
extern crate multipart;
use std::sync::Arc;
use std::marker::PhantomData;
use futures::{Future, future, Stream, stream};
use hyper;
use hyper::{Request, Response, Error, StatusCode};
use hyper::header::{Headers, ContentType};
use self::url::form_urlencoded;
use mimetypes;
use self::multipart::server::Multipart;
use self::multipart::server::save::SaveResult;
use std::fs;
use serde_json;
#[allow(unused_imports)]
use std::collections::{HashMap, BTreeMap};
#[allow(unused_imports)]
use swagger;
use std::io;
#[allow(unused_imports)]
use std::collections::BTreeSet;
pub use swagger::auth::Authorization;
use swagger::{ApiError, XSpanId, XSpanIdString, Has, RequestParser};
use swagger::auth::Scopes;
use {Api,
MultipartRequestPostResponse
};
#[allow(unused_imports)]
use models;
pub mod context;
header! { (Warning, "Warning") => [String] }
mod paths {
extern crate regex;
lazy_static! {
pub static ref GLOBAL_REGEX_SET: regex::RegexSet = regex::RegexSet::new(&[
r"^/multipart_request$"
]).unwrap();
}
pub static ID_MULTIPART_REQUEST: usize = 0;
}
pub struct NewService<T, C> {
api_impl: Arc<T>,
marker: PhantomData<C>,
}
impl<T, C> NewService<T, C>
where
T: Api<C> + Clone + 'static,
C: Has<XSpanIdString> + 'static
{
pub fn new<U: Into<Arc<T>>>(api_impl: U) -> NewService<T, C> {
NewService{api_impl: api_impl.into(), marker: PhantomData}
}
}
impl<T, C> hyper::server::NewService for NewService<T, C>
where
T: Api<C> + Clone + 'static,
C: Has<XSpanIdString> + 'static
{
type Request = (Request, C);
type Response = Response;
type Error = Error;
type Instance = Service<T, C>;
fn new_service(&self) -> Result<Self::Instance, io::Error> {
Ok(Service::new(self.api_impl.clone()))
}
}
pub struct Service<T, C> {
api_impl: Arc<T>,
marker: PhantomData<C>,
}
impl<T, C> Service<T, C>
where
T: Api<C> + Clone + 'static,
C: Has<XSpanIdString> + 'static {
pub fn new<U: Into<Arc<T>>>(api_impl: U) -> Service<T, C> {
Service{api_impl: api_impl.into(), marker: PhantomData}
}
}
impl<T, C> hyper::server::Service for Service<T, C>
where
T: Api<C> + Clone + 'static,
C: Has<XSpanIdString> + 'static
{
type Request = (Request, C);
type Response = Response;
type Error = Error;
type Future = Box<Future<Item=Response, Error=Error>>;
fn call(&self, (req, mut context): Self::Request) -> Self::Future {
let api_impl = self.api_impl.clone();
let (method, uri, _, headers, body) = req.deconstruct();
let path = paths::GLOBAL_REGEX_SET.matches(uri.path());
// This match statement is duplicated below in `parse_operation_id()`.
// Please update both places if changing how this code is autogenerated.
match &method {
// MultipartRequestPost - POST /multipart_request
&hyper::Method::Post if path.matched(paths::ID_MULTIPART_REQUEST) => {
let boundary = match multipart_boundary(&headers) {
Some(boundary) => boundary.to_string(),
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body("Couldn't find valid multipart body"))),
};
// Form Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
Box::new(body.concat2()
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
// Read Form Parameters from body
let mut entries = match Multipart::with_body(&body.to_vec()[..], boundary).save().temp() {
SaveResult::Full(entries) => {
entries
},
_ => {
return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Unable to process all message parts"))))
},
};
let file_string_field = entries.files.remove("string_field");
let param_string_field = match file_string_field {
Some(file) => {
let path = &file[0].path;
let string_field_str = fs::read_to_string(path).expect("Reading saved String should never fail");
let string_field_model: String = match serde_json::from_str(&string_field_str) {
Ok(model) => model,
Err(e) => {
return Box::new(future::ok(
Response::new()
.with_status(StatusCode::BadRequest)
.with_body(format!("string_field data does not match API definition: {}", e))))
}
};
string_field_model
}
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Missing required form parameter string_field")))),
};
let file_optional_string_field = entries.files.remove("optional_string_field");
let param_optional_string_field = match file_optional_string_field {
Some(file) => {
let path = &file[0].path;
let optional_string_field_str = fs::read_to_string(path).unwrap();
let optional_string_field_model: String = serde_json::from_str(&optional_string_field_str).expect("Impossible to fail to serialise");
Some(optional_string_field_model)
}
None => None,
};
let file_object_field = entries.files.remove("object_field");
let param_object_field = match file_object_field {
Some(file) => {
let path = &file[0].path;
let object_field_str = fs::read_to_string(path).unwrap();
let object_field_model: models::MultipartRequestObjectField = serde_json::from_str(&object_field_str).expect("Impossible to fail to serialise");
Some(object_field_model)
}
None => None,
};
let file_binary_field = entries.files.remove("binary_field");
let param_binary_field = match file_binary_field {
Some(file) => {
let path = &file[0].path;
let binary_field_str = fs::read_to_string(path).unwrap();
swagger::ByteArray(binary_field_str.as_bytes().to_vec())
}
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Missing required form parameter binary_field")))),
};
Box::new(api_impl.multipart_request_post(param_string_field, param_binary_field, param_optional_string_field, param_object_field, &context)
.then(move |result| {
let mut response = Response::new();
response.headers_mut().set(XSpanId((&context as &Has<XSpanIdString>).get().0.to_string()));
match result {
Ok(rsp) => match rsp {
MultipartRequestPostResponse::OK
=> {
response.set_status(StatusCode::try_from(201).unwrap());
},
},
Err(_) => {
// Application code returned an error. This should not happen, as the implementation should
// return a valid response.
response.set_status(StatusCode::InternalServerError);
response.set_body("An internal error occurred");
},
}
future::ok(response)
}
))
as Box<Future<Item=Response, Error=Error>>
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read multipart body")))),
}
})
)
},
_ => Box::new(future::ok(Response::new().with_status(StatusCode::NotFound))) as Box<Future<Item=Response, Error=Error>>,
}
}
}
impl<T, C> Clone for Service<T, C>
{
fn clone(&self) -> Self {
Service {
api_impl: self.api_impl.clone(),
marker: self.marker.clone(),
}
}
}
/// Utility function to get the multipart boundary marker (if any) from the Headers.
fn multipart_boundary<'a>(headers: &'a Headers) -> Option<&'a str> {
headers.get::<ContentType>().and_then(|content_type| {
let ContentType(ref mime) = *content_type;
if mime.type_() == hyper::mime::MULTIPART && mime.subtype() == hyper::mime::FORM_DATA {
mime.get_param(hyper::mime::BOUNDARY).map(|x| x.as_str())
} else {
None
}
})
}
/// Request parser for `Api`.
pub struct ApiRequestParser;
impl RequestParser for ApiRequestParser {
fn parse_operation_id(request: &Request) -> Result<&'static str, ()> {
let path = paths::GLOBAL_REGEX_SET.matches(request.uri().path());
match request.method() {
// MultipartRequestPost - POST /multipart_request
&hyper::Method::Post if path.matched(paths::ID_MULTIPART_REQUEST) => Ok("MultipartRequestPost"),
_ => Err(()),
}
}
}

View File

@@ -23,8 +23,8 @@ swagger = "2"
#
lazy_static = "0.2"
log = "0.3.0"
mime = "0.3.3"
multipart = {version = "0.13.3", optional = true}
mime = "0.2.6"
multipart = {version = "0.13.3"}
native-tls = {version = "0.1.4", optional = true}
openssl = {version = "0.9.14", optional = true}
percent-encoding = {version = "1.0.0", optional = true}

View File

@@ -56,6 +56,7 @@ To run a client, follow one of the following simple steps:
```
cargo run --example client RequiredOctetStreamPut
cargo run --example client UuidGet
cargo run --example client XmlExtraPost
cargo run --example client XmlOtherPost
cargo run --example client XmlOtherPut
@@ -115,6 +116,7 @@ All URIs are relative to *http://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
[****](docs/default_api.md#) | **PUT** /required_octet_stream |
[****](docs/default_api.md#) | **GET** /uuid |
[****](docs/default_api.md#) | **POST** /xml_extra |
[****](docs/default_api.md#) | **POST** /xml_other |
[****](docs/default_api.md#) | **PUT** /xml_other |

View File

@@ -65,6 +65,15 @@ paths:
description: OK
400:
description: Bad Request
/uuid:
get:
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/UuidObject'
description: Duplicate Response long text. One.
/required_octet_stream:
put:
requestBody:

View File

@@ -5,6 +5,7 @@ All URIs are relative to *http://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
****](default_api.md#) | **PUT** /required_octet_stream |
****](default_api.md#) | **GET** /uuid |
****](default_api.md#) | **POST** /xml_extra |
****](default_api.md#) | **POST** /xml_other |
****](default_api.md#) | **PUT** /xml_other |
@@ -37,6 +38,28 @@ No authorization required
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
# ****
> uuid::Uuid ()
### Required Parameters
This endpoint does not need any parameter.
### Return type
[**uuid::Uuid**](UUID.md)
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: Not defined
- **Accept**: application/json,
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
# ****
> (optional)

View File

@@ -20,6 +20,7 @@ use tokio_core::reactor;
use openapi_v3::{ApiNoContext, ContextWrapperExt,
ApiError,
RequiredOctetStreamPutResponse,
UuidGetResponse,
XmlExtraPostResponse,
XmlOtherPostResponse,
XmlOtherPutResponse,
@@ -34,6 +35,7 @@ fn main() {
.help("Sets the operation to run")
.possible_values(&[
"RequiredOctetStreamPut",
"UuidGet",
"XmlExtraPost",
"XmlOtherPost",
"XmlOtherPut",
@@ -84,6 +86,11 @@ fn main() {
println!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
},
Some("UuidGet") => {
let result = core.run(client.uuid_get());
println!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
},
Some("XmlExtraPost") => {
let result = core.run(client.xml_extra_post(None));
println!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());

View File

@@ -12,6 +12,7 @@ use swagger::{Has, XSpanIdString};
use openapi_v3::{Api, ApiError,
RequiredOctetStreamPutResponse,
UuidGetResponse,
XmlExtraPostResponse,
XmlOtherPostResponse,
XmlOtherPutResponse,
@@ -41,6 +42,13 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString>{
}
fn uuid_get(&self, context: &C) -> Box<Future<Item=UuidGetResponse, Error=ApiError>> {
let context = context.clone();
println!("uuid_get() - X-Span-ID: {:?}", context.get().0.clone());
Box::new(futures::failed("Generic failure".into()))
}
fn xml_extra_post(&self, duplicate_xml_object: Option<models::DuplicateXmlObject>, context: &C) -> Box<Future<Item=XmlExtraPostResponse, Error=ApiError>> {
let context = context.clone();
println!("xml_extra_post({:?}) - X-Span-ID: {:?}", duplicate_xml_object, context.get().0.clone());

View File

@@ -7,8 +7,6 @@ extern crate mime;
extern crate chrono;
extern crate url;
use hyper;
use hyper::header::{Headers, ContentType};
use hyper::Uri;
@@ -26,9 +24,7 @@ use std::sync::Arc;
use std::str;
use std::str::FromStr;
use std::string::ToString;
use mimetypes;
use serde_json;
use serde_xml_rs;
@@ -41,6 +37,7 @@ use swagger::{ApiError, XSpanId, XSpanIdString, Has, AuthData};
use {Api,
RequiredOctetStreamPutResponse,
UuidGetResponse,
XmlExtraPostResponse,
XmlOtherPostResponse,
XmlOtherPutResponse,
@@ -273,17 +270,13 @@ impl<F, C> Api<C> for Client<F> where
let mut request = hyper::Request::new(hyper::Method::Put, uri);
// Body parameter
let body = param_body.0;
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::REQUIRED_OCTET_STREAM_PUT.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -321,6 +314,80 @@ impl<F, C> Api<C> for Client<F> where
}
fn uuid_get(&self, context: &C) -> Box<Future<Item=UuidGetResponse, Error=ApiError>> {
let mut uri = format!(
"{}/uuid",
self.base_path
);
let mut query_string = self::url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build URI: {}", err))))),
};
let mut request = hyper::Request::new(hyper::Method::Get, uri);
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<uuid::Uuid>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
UuidGetResponse::DuplicateResponseLongText(body)
})
) as Box<Future<Item=_, Error=_>>
},
code => {
let headers = response.headers().clone();
Box::new(response.body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<Future<Item=_, Error=_>>
}
}
}))
}
fn xml_extra_post(&self, param_duplicate_xml_object: Option<models::DuplicateXmlObject>, context: &C) -> Box<Future<Item=XmlExtraPostResponse, Error=ApiError>> {
let mut uri = format!(
"{}/xml_extra",
@@ -347,14 +414,13 @@ impl<F, C> Api<C> for Client<F> where
body.to_xml()
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::XML_EXTRA_POST.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -427,14 +493,13 @@ if let Some(body) = body {
body.to_xml()
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::XML_OTHER_POST.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -507,14 +572,13 @@ if let Some(body) = body {
body.to_xml()
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::XML_OTHER_PUT.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -587,14 +651,13 @@ if let Some(body) = body {
body.to_xml()
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::XML_POST.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -667,14 +730,13 @@ if let Some(body) = body {
body.to_xml()
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::XML_PUT.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {

View File

@@ -11,6 +11,7 @@ extern crate chrono;
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate mime;
// Logically this should be in the client and server modules, but rust doesn't allow `macro_use` from a module.
#[cfg(any(feature = "client", feature = "server"))]
@@ -45,6 +46,12 @@ pub enum RequiredOctetStreamPutResponse {
OK ,
}
#[derive(Debug, PartialEq)]
pub enum UuidGetResponse {
/// Duplicate Response long text. One.
DuplicateResponseLongText ( uuid::Uuid ) ,
}
#[derive(Debug, PartialEq)]
pub enum XmlExtraPostResponse {
/// OK
@@ -93,6 +100,9 @@ pub trait Api<C> {
fn required_octet_stream_put(&self, body: swagger::ByteArray, context: &C) -> Box<Future<Item=RequiredOctetStreamPutResponse, Error=ApiError>>;
fn uuid_get(&self, context: &C) -> Box<Future<Item=UuidGetResponse, Error=ApiError>>;
fn xml_extra_post(&self, duplicate_xml_object: Option<models::DuplicateXmlObject>, context: &C) -> Box<Future<Item=XmlExtraPostResponse, Error=ApiError>>;
@@ -116,6 +126,9 @@ pub trait ApiNoContext {
fn required_octet_stream_put(&self, body: swagger::ByteArray) -> Box<Future<Item=RequiredOctetStreamPutResponse, Error=ApiError>>;
fn uuid_get(&self) -> Box<Future<Item=UuidGetResponse, Error=ApiError>>;
fn xml_extra_post(&self, duplicate_xml_object: Option<models::DuplicateXmlObject>) -> Box<Future<Item=XmlExtraPostResponse, Error=ApiError>>;
@@ -152,6 +165,11 @@ impl<'a, T: Api<C>, C> ApiNoContext for ContextWrapper<'a, T, C> {
}
fn uuid_get(&self) -> Box<Future<Item=UuidGetResponse, Error=ApiError>> {
self.api().uuid_get(&self.context())
}
fn xml_extra_post(&self, duplicate_xml_object: Option<models::DuplicateXmlObject>) -> Box<Future<Item=XmlExtraPostResponse, Error=ApiError>> {
self.api().xml_extra_post(duplicate_xml_object, &self.context())
}

View File

@@ -4,6 +4,10 @@ pub mod responses {
use hyper::mime::*;
// The macro is called per-operation to beat the recursion limit
/// Create Mime objects for the response content types for UuidGet
lazy_static! {
pub static ref UUID_GET_DUPLICATE_RESPONSE_LONG_TEXT: Mime = "application/json".parse().unwrap();
}
}

View File

@@ -5,11 +5,10 @@ extern crate native_tls;
extern crate hyper_tls;
extern crate openssl;
extern crate mime;
extern crate uuid;
extern crate chrono;
extern crate percent_encoding;
extern crate url;
extern crate uuid;
use std::sync::Arc;
use std::marker::PhantomData;
@@ -19,7 +18,6 @@ use hyper::{Request, Response, Error, StatusCode};
use hyper::header::{Headers, ContentType};
use self::url::form_urlencoded;
use mimetypes;
use serde_json;
use serde_xml_rs;
@@ -38,6 +36,7 @@ use swagger::auth::Scopes;
use {Api,
RequiredOctetStreamPutResponse,
UuidGetResponse,
XmlExtraPostResponse,
XmlOtherPostResponse,
XmlOtherPutResponse,
@@ -57,15 +56,17 @@ mod paths {
lazy_static! {
pub static ref GLOBAL_REGEX_SET: regex::RegexSet = regex::RegexSet::new(&[
r"^/required_octet_stream$",
r"^/uuid$",
r"^/xml$",
r"^/xml_extra$",
r"^/xml_other$"
]).unwrap();
}
pub static ID_REQUIRED_OCTET_STREAM: usize = 0;
pub static ID_XML: usize = 1;
pub static ID_XML_EXTRA: usize = 2;
pub static ID_XML_OTHER: usize = 3;
pub static ID_UUID: usize = 1;
pub static ID_XML: usize = 2;
pub static ID_XML_EXTRA: usize = 3;
pub static ID_XML_OTHER: usize = 4;
}
pub struct NewService<T, C> {
@@ -133,12 +134,6 @@ where
// RequiredOctetStreamPut - PUT /required_octet_stream
&hyper::Method::Put if path.matched(paths::ID_REQUIRED_OCTET_STREAM) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -147,9 +142,7 @@ where
match result {
Ok(body) => {
let param_body: Option<swagger::ByteArray> = if !body.is_empty() {
Some(swagger::ByteArray(body.to_vec()))
} else {
None
};
@@ -157,8 +150,6 @@ where
Some(param_body) => param_body,
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body("Missing required body parameter body"))),
};
Box::new(api_impl.required_octet_stream_put(param_body, &context)
.then(move |result| {
let mut response = Response::new();
@@ -185,25 +176,57 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter body: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
// UuidGet - GET /uuid
&hyper::Method::Get if path.matched(paths::ID_UUID) => {
Box::new({
{{
Box::new(api_impl.uuid_get(&context)
.then(move |result| {
let mut response = Response::new();
response.headers_mut().set(XSpanId((&context as &Has<XSpanIdString>).get().0.to_string()));
match result {
Ok(rsp) => match rsp {
UuidGetResponse::DuplicateResponseLongText
(body)
=> {
response.set_status(StatusCode::try_from(200).unwrap());
response.headers_mut().set(ContentType(mimetypes::responses::UUID_GET_DUPLICATE_RESPONSE_LONG_TEXT.clone()));
let body = serde_json::to_string(&body).expect("impossible to fail to serialize");
response.set_body(body);
},
},
Err(_) => {
// Application code returned an error. This should not happen, as the implementation should
// return a valid response.
response.set_status(StatusCode::InternalServerError);
response.set_body("An internal error occurred");
},
}
future::ok(response)
}
))
}}
}) as Box<Future<Item=Response, Error=Error>>
},
// XmlExtraPost - POST /xml_extra
&hyper::Method::Post if path.matched(paths::ID_XML_EXTRA) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -211,25 +234,19 @@ where
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
let mut unused_elements = Vec::new();
let param_duplicate_xml_object: Option<models::DuplicateXmlObject> = if !body.is_empty() {
let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body);
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
}) {
Ok(param_duplicate_xml_object) => param_duplicate_xml_object,
Err(_) => None,
}
} else {
None
};
Box::new(api_impl.xml_extra_post(param_duplicate_xml_object, &context)
.then(move |result| {
let mut response = Response::new();
@@ -267,25 +284,15 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter DuplicateXmlObject: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
// XmlOtherPost - POST /xml_other
&hyper::Method::Post if path.matched(paths::ID_XML_OTHER) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -293,25 +300,19 @@ where
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
let mut unused_elements = Vec::new();
let param_another_xml_object: Option<models::AnotherXmlObject> = if !body.is_empty() {
let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body);
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
}) {
Ok(param_another_xml_object) => param_another_xml_object,
Err(_) => None,
}
} else {
None
};
Box::new(api_impl.xml_other_post(param_another_xml_object, &context)
.then(move |result| {
let mut response = Response::new();
@@ -349,25 +350,15 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter AnotherXmlObject: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
// XmlOtherPut - PUT /xml_other
&hyper::Method::Put if path.matched(paths::ID_XML_OTHER) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -375,25 +366,19 @@ where
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
let mut unused_elements = Vec::new();
let param_string: Option<models::AnotherXmlArray> = if !body.is_empty() {
let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body);
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
}) {
Ok(param_string) => param_string,
Err(_) => None,
}
} else {
None
};
Box::new(api_impl.xml_other_put(param_string, &context)
.then(move |result| {
let mut response = Response::new();
@@ -431,25 +416,15 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter string: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
// XmlPost - POST /xml
&hyper::Method::Post if path.matched(paths::ID_XML) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -457,25 +432,19 @@ where
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
let mut unused_elements = Vec::new();
let param_string: Option<models::XmlArray> = if !body.is_empty() {
let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body);
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
}) {
Ok(param_string) => param_string,
Err(_) => None,
}
} else {
None
};
Box::new(api_impl.xml_post(param_string, &context)
.then(move |result| {
let mut response = Response::new();
@@ -513,25 +482,15 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter string: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
// XmlPut - PUT /xml
&hyper::Method::Put if path.matched(paths::ID_XML) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -539,25 +498,19 @@ where
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
let mut unused_elements = Vec::new();
let param_xml_object: Option<models::XmlObject> = if !body.is_empty() {
let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body);
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
}) {
Ok(param_xml_object) => param_xml_object,
Err(_) => None,
}
} else {
None
};
Box::new(api_impl.xml_put(param_xml_object, &context)
.then(move |result| {
let mut response = Response::new();
@@ -595,17 +548,13 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter XmlObject: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
_ => Box::new(future::ok(Response::new().with_status(StatusCode::NotFound))) as Box<Future<Item=Response, Error=Error>>,
}
}
@@ -621,6 +570,7 @@ impl<T, C> Clone for Service<T, C>
}
}
/// Request parser for `Api`.
pub struct ApiRequestParser;
impl RequestParser for ApiRequestParser {
@@ -631,6 +581,9 @@ impl RequestParser for ApiRequestParser {
// RequiredOctetStreamPut - PUT /required_octet_stream
&hyper::Method::Put if path.matched(paths::ID_REQUIRED_OCTET_STREAM) => Ok("RequiredOctetStreamPut"),
// UuidGet - GET /uuid
&hyper::Method::Get if path.matched(paths::ID_UUID) => Ok("UuidGet"),
// XmlExtraPost - POST /xml_extra
&hyper::Method::Post if path.matched(paths::ID_XML_EXTRA) => Ok("XmlExtraPost"),

View File

@@ -23,8 +23,8 @@ swagger = "2"
#
lazy_static = "0.2"
log = "0.3.0"
mime = "0.3.3"
multipart = {version = "0.13.3", optional = true}
mime = "0.2.6"
multipart = {version = "0.13.3"}
native-tls = {version = "0.1.4", optional = true}
openssl = {version = "0.9.14", optional = true}
percent-encoding = {version = "1.0.0", optional = true}

View File

@@ -7,7 +7,7 @@ extern crate mime;
extern crate chrono;
extern crate url;
extern crate serde_urlencoded;
extern crate multipart;
use hyper;
use hyper::header::{Headers, ContentType};
@@ -26,9 +26,10 @@ use std::sync::Arc;
use std::str;
use std::str::FromStr;
use std::string::ToString;
use hyper::mime::Mime;
use std::io::Cursor;
use client::multipart::client::lazy::Multipart;
use mimetypes;
use serde_json;
use serde_xml_rs;
@@ -299,17 +300,13 @@ impl<F, C> Api<C> for Client<F> where
let mut request = hyper::Request::new(hyper::Method::Patch, uri);
// Body parameter
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::TEST_SPECIAL_TAGS.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -382,20 +379,18 @@ impl<F, C> Api<C> for Client<F> where
let mut request = hyper::Request::new(hyper::Method::Post, uri);
// Body parameter
let body = param_body.map(|ref body| {
serde_json::to_string(body).expect("impossible to fail to serialize")
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::FAKE_OUTER_BOOLEAN_SERIALIZE.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -472,14 +467,13 @@ if let Some(body) = body {
serde_json::to_string(body).expect("impossible to fail to serialize")
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::FAKE_OUTER_COMPOSITE_SERIALIZE.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -556,14 +550,13 @@ if let Some(body) = body {
serde_json::to_string(body).expect("impossible to fail to serialize")
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::FAKE_OUTER_NUMBER_SERIALIZE.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -640,14 +633,13 @@ if let Some(body) = body {
serde_json::to_string(body).expect("impossible to fail to serialize")
});
if let Some(body) = body {
request.set_body(body);
if let Some(body) = body {
request.set_body(body);
}
request.headers_mut().set(ContentType(mimetypes::requests::FAKE_OUTER_STRING_SERIALIZE.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -722,14 +714,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Put, uri);
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::TEST_BODY_WITH_QUERY_PARAMS.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -790,14 +779,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Patch, uri);
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::TEST_CLIENT_MODEL.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -890,6 +876,7 @@ if let Some(body) = body {
request.headers_mut().set(ContentType(mimetypes::requests::TEST_ENDPOINT_PARAMETERS.clone()));
request.set_body(body.into_bytes());
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
if let AuthData::Basic(ref basic_header) = *auth_data {
@@ -898,7 +885,6 @@ if let Some(body) = body {
))
}
}
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -986,15 +972,13 @@ if let Some(body) = body {
request.headers_mut().set(ContentType(mimetypes::requests::TEST_ENUM_PARAMETERS.clone()));
request.set_body(body.into_bytes());
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
// Header parameters
header! { (RequestEnumHeaderStringArray, "enum_header_string_array") => (String)* }
param_enum_header_string_array.map(|header| request.headers_mut().set(RequestEnumHeaderStringArray(header.clone())));
header! { (RequestEnumHeaderString, "enum_header_string") => [String] }
param_enum_header_string.map(|header| request.headers_mut().set(RequestEnumHeaderString(header)));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1064,14 +1048,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Post, uri);
let body = serde_json::to_string(&param_param).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::TEST_INLINE_ADDITIONAL_PROPERTIES.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1139,9 +1120,8 @@ if let Some(body) = body {
request.headers_mut().set(ContentType(mimetypes::requests::TEST_JSON_FORM_DATA.clone()));
request.set_body(body.into_bytes());
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1205,17 +1185,13 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Patch, uri);
// Body parameter
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::TEST_CLASSNAME.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1288,17 +1264,13 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Post, uri);
// Body parameter
let body = param_body.to_xml();
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::ADD_PET.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1360,12 +1332,9 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
// Header parameters
header! { (RequestApiKey, "api_key") => [String] }
param_api_key.map(|header| request.headers_mut().set(RequestApiKey(header)));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1428,8 +1397,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1515,8 +1482,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1607,7 +1572,6 @@ if let Some(body) = body {
request.headers_mut().set(ApiKey(api_key.to_string()));
}
}
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1700,14 +1664,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Put, uri);
let body = param_body.to_xml();
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::UPDATE_PET.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1793,9 +1754,8 @@ if let Some(body) = body {
request.headers_mut().set(ContentType(mimetypes::requests::UPDATE_PET_WITH_FORM.clone()));
request.set_body(body.into_bytes());
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1855,17 +1815,57 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Post, uri);
let params = &[
("additionalMetadata", param_additional_metadata),
("file", param_file.map(|param| format!("{:?}", param))),
];
let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize");
let mut multipart = Multipart::new();
// For each parameter, encode as appropriate and add to the multipart body as a stream.
let additional_metadata_str = match serde_json::to_string(&param_additional_metadata) {
Ok(str) => str,
Err(e) => return Box::new(futures::done(Err(ApiError(format!("Unable to parse additional_metadata to string: {}", e))))),
};
let additional_metadata_vec = additional_metadata_str.as_bytes().to_vec();
let additional_metadata_mime = mime::Mime::from_str("application/json").expect("impossible to fail to parse");
let additional_metadata_cursor = Cursor::new(additional_metadata_vec);
multipart.add_stream("additional_metadata", additional_metadata_cursor, None as Option<&str>, Some(additional_metadata_mime));
let file_str = match serde_json::to_string(&param_file) {
Ok(str) => str,
Err(e) => return Box::new(futures::done(Err(ApiError(format!("Unable to parse file to string: {}", e))))),
};
let file_vec = file_str.as_bytes().to_vec();
let file_mime = mime::Mime::from_str("application/json").expect("impossible to fail to parse");
let file_cursor = Cursor::new(file_vec);
multipart.add_stream("file", file_cursor, None as Option<&str>, Some(file_mime));
let mut fields = match multipart.prepare() {
Ok(fields) => fields,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build request: {}", err))))),
};
let mut body_string = String::new();
fields.to_body().read_to_string(&mut body_string).unwrap();
let boundary = fields.boundary();
let multipart_header = match Mime::from_str(&format!("multipart/form-data;boundary={}", boundary)) {
Ok(multipart_header) => multipart_header,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build multipart header: {:?}", err))))),
};
request.set_body(body_string.into_bytes());
request.headers_mut().set(ContentType(multipart_header));
request.headers_mut().set(ContentType(mimetypes::requests::UPLOAD_FILE.clone()));
request.set_body(body.into_bytes());
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -1940,8 +1940,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2018,7 +2016,6 @@ if let Some(body) = body {
request.headers_mut().set(ApiKey(api_key.to_string()));
}
}
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2093,8 +2090,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2187,14 +2182,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Post, uri);
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::PLACE_ORDER.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2277,17 +2269,13 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Post, uri);
// Body parameter
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::CREATE_USER.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2348,14 +2336,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Post, uri);
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::CREATE_USERS_WITH_ARRAY_INPUT.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2416,14 +2401,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Post, uri);
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::CREATE_USERS_WITH_LIST_INPUT.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2485,8 +2467,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2557,8 +2537,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2654,8 +2632,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2750,8 +2726,6 @@ if let Some(body) = body {
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -2812,14 +2786,11 @@ if let Some(body) = body {
let mut request = hyper::Request::new(hyper::Method::Put, uri);
let body = serde_json::to_string(&param_body).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::UPDATE_USER.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {

View File

@@ -11,6 +11,7 @@ extern crate chrono;
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate mime;
// Logically this should be in the client and server modules, but rust doesn't allow `macro_use` from a module.
#[cfg(any(feature = "client", feature = "server"))]

View File

@@ -23,8 +23,8 @@ swagger = "2"
#
lazy_static = "0.2"
log = "0.3.0"
mime = "0.3.3"
multipart = {version = "0.13.3", optional = true}
mime = "0.2.6"
multipart = {version = "0.13.3"}
native-tls = {version = "0.1.4", optional = true}
openssl = {version = "0.9.14", optional = true}
percent-encoding = {version = "1.0.0", optional = true}

View File

@@ -7,8 +7,6 @@ extern crate mime;
extern crate chrono;
extern crate url;
use hyper;
use hyper::header::{Headers, ContentType};
use hyper::Uri;
@@ -26,12 +24,9 @@ use std::sync::Arc;
use std::str;
use std::str::FromStr;
use std::string::ToString;
use mimetypes;
use serde_json;
#[allow(unused_imports)]
use std::collections::{HashMap, BTreeMap};
#[allow(unused_imports)]
@@ -274,8 +269,6 @@ impl<F, C> Api<C> for Client<F> where
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -336,14 +329,11 @@ impl<F, C> Api<C> for Client<F> where
let mut request = hyper::Request::new(hyper::Method::Put, uri);
let body = serde_json::to_string(&param_nested_response).expect("impossible to fail to serialize");
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::DUMMY_PUT.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -405,8 +395,6 @@ impl<F, C> Api<C> for Client<F> where
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -480,14 +468,11 @@ impl<F, C> Api<C> for Client<F> where
let mut request = hyper::Request::new(hyper::Method::Post, uri);
let body = param_body;
request.set_body(body);
request.headers_mut().set(ContentType(mimetypes::requests::HTML_POST.clone()));
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@@ -562,8 +547,6 @@ impl<F, C> Api<C> for Client<F> where
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {

View File

@@ -11,6 +11,7 @@ extern crate chrono;
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate mime;
// Logically this should be in the client and server modules, but rust doesn't allow `macro_use` from a module.
#[cfg(any(feature = "client", feature = "server"))]

View File

@@ -5,11 +5,10 @@ extern crate native_tls;
extern crate hyper_tls;
extern crate openssl;
extern crate mime;
extern crate uuid;
extern crate chrono;
extern crate percent_encoding;
extern crate url;
extern crate uuid;
use std::sync::Arc;
use std::marker::PhantomData;
@@ -19,10 +18,8 @@ use hyper::{Request, Response, Error, StatusCode};
use hyper::header::{Headers, ContentType};
use self::url::form_urlencoded;
use mimetypes;
use serde_json;
#[allow(unused_imports)]
use std::collections::{HashMap, BTreeMap};
#[allow(unused_imports)]
@@ -132,16 +129,8 @@ where
// DummyGet - GET /dummy
&hyper::Method::Get if path.matched(paths::ID_DUMMY) => {
Box::new({
{{
Box::new(api_impl.dummy_get(&context)
.then(move |result| {
let mut response = Response::new();
@@ -168,22 +157,12 @@ where
future::ok(response)
}
))
}}
}) as Box<Future<Item=Response, Error=Error>>
},
// DummyPut - PUT /dummy
&hyper::Method::Put if path.matched(paths::ID_DUMMY) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -191,12 +170,9 @@ where
.then(move |result| -> Box<Future<Item=Response, Error=Error>> {
match result {
Ok(body) => {
let mut unused_elements = Vec::new();
let param_nested_response: Option<models::InlineObject> = if !body.is_empty() {
let deserializer = &mut serde_json::Deserializer::from_slice(&*body);
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
@@ -204,7 +180,6 @@ where
Ok(param_nested_response) => param_nested_response,
Err(e) => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't parse body parameter nested_response - doesn't match schema: {}", e)))),
}
} else {
None
};
@@ -212,8 +187,6 @@ where
Some(param_nested_response) => param_nested_response,
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body("Missing required body parameter nested_response"))),
};
Box::new(api_impl.dummy_put(param_nested_response, &context)
.then(move |result| {
let mut response = Response::new();
@@ -244,29 +217,17 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter nested_response: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
// FileResponseGet - GET /file_response
&hyper::Method::Get if path.matched(paths::ID_FILE_RESPONSE) => {
Box::new({
{{
Box::new(api_impl.file_response_get(&context)
.then(move |result| {
let mut response = Response::new();
@@ -301,22 +262,12 @@ where
future::ok(response)
}
))
}}
}) as Box<Future<Item=Response, Error=Error>>
},
// HtmlPost - POST /html
&hyper::Method::Post if path.matched(paths::ID_HTML) => {
// Body parameters (note that non-required body parameters will ignore garbage
// values, rather than causing a 400 response). Produce warning header and logs for
// any unused fields.
@@ -325,9 +276,7 @@ where
match result {
Ok(body) => {
let param_body: Option<String> = if !body.is_empty() {
Some(String::from_utf8(body.to_vec()).unwrap())
} else {
None
};
@@ -335,8 +284,6 @@ where
Some(param_body) => param_body,
None => return Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body("Missing required body parameter body"))),
};
Box::new(api_impl.html_post(param_body, &context)
.then(move |result| {
let mut response = Response::new();
@@ -371,29 +318,17 @@ where
future::ok(response)
}
))
},
Err(e) => Box::new(future::ok(Response::new().with_status(StatusCode::BadRequest).with_body(format!("Couldn't read body parameter body: {}", e)))),
}
})
) as Box<Future<Item=Response, Error=Error>>
},
// RawJsonGet - GET /raw_json
&hyper::Method::Get if path.matched(paths::ID_RAW_JSON) => {
Box::new({
{{
Box::new(api_impl.raw_json_get(&context)
.then(move |result| {
let mut response = Response::new();
@@ -428,14 +363,10 @@ where
future::ok(response)
}
))
}}
}) as Box<Future<Item=Response, Error=Error>>
},
_ => Box::new(future::ok(Response::new().with_status(StatusCode::NotFound))) as Box<Future<Item=Response, Error=Error>>,
}
}
@@ -451,6 +382,7 @@ impl<T, C> Clone for Service<T, C>
}
}
/// Request parser for `Api`.
pub struct ApiRequestParser;
impl RequestParser for ApiRequestParser {