Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ public RustClientCodegen() {
typeMapping.put("date", "string");
typeMapping.put("DateTime", "String");
typeMapping.put("password", "String");
// TODO(farcaller): map file
typeMapping.put("file", "::models::File");
// TODO(bcourtine): review file mapping.
// I tried to map as "std::io::File", but Reqwest multipart file requires a "AsRef<Path>" param.
// Getting a file from a Path is simple, but the opposite is difficult. So I map as "std::path::Path".
// Reference is required here because Path does not implement the "Sized" trait.
typeMapping.put("file", "&std::path::Path");
typeMapping.put("binary", "::models::File");
typeMapping.put("ByteArray", "String");
typeMapping.put("object", "Value");
Expand Down Expand Up @@ -379,7 +382,6 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");
@SuppressWarnings("unchecked")
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
Set<String> headerKeys = new HashSet<>();
for (CodegenOperation operation : operations) {
// http method verb conversion, depending on client library (e.g. Hyper: PUT => Put, Reqwest: PUT => put)
if (HYPER_LIBRARY.equals(getLibrary())) {
Expand Down Expand Up @@ -439,9 +441,6 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}*/
}

additionalProperties.put("headerKeys", headerKeys);
additionalProperties.putIfAbsent("authHeaderKey", "api-key");

return objs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::Borrow;

use reqwest;

use super::{Error, configuration};
use super::{Error, configuration, urlencode};

pub struct {{{classname}}}Client {
configuration: Rc<configuration::Configuration>,
Expand All @@ -26,47 +26,36 @@ pub trait {{{classname}}} {
{{/operations}}
}


impl {{{classname}}} for {{{classname}}}Client {
{{#operations}}
{{#operation}}
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());
{{#queryParams}}
query.append_pair("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(","){{/isListContainer}}.to_string());
{{/queryParams}}
{{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let val = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
query.append_pair("{{{keyParamName}}}".to_owned(), val);
}
{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}}
query.finish()
};
let uri_str = format!("{}{{{path}}}?{}", configuration.base_path, query_string{{#pathParams}}, {{{baseName}}}={{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{/pathParams}});

let uri_str = format!("{}{{{path}}}", configuration.base_path{{#pathParams}}, {{{baseName}}}={{#isString}}urlencode({{/isString}}{{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{#isString}}){{/isString}}{{/pathParams}});
let mut req_builder = client.{{{httpMethod}}}(uri_str.as_str());

{{#queryParams}}
req_builder = req_builder.query(&[("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(","){{/isListContainer}}.to_string())]);
{{/queryParams}}
{{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let val = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.query(&[("{{{keyParamName}}}", val)]);
}
{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}

{{#hasHeaderParams}}
{{#headerParams}}
{{#hasHeaderParams}}{{#headerParams}}
req_builder = req_builder.header("{{{baseName}}}", {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string());
{{/headerParams}}
{{/hasHeaderParams}}

{{#hasAuthMethods}}
{{#authMethods}}
{{/headerParams}}{{/hasHeaderParams}}
{{#hasAuthMethods}}{{#authMethods}}
{{#isApiKey}}{{#isKeyInHeader}}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
Expand All @@ -87,14 +76,34 @@ impl {{{classname}}} for {{{classname}}}Client {
req_builder = req_builder.bearer_auth(token.to_owned());
};
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}

{{#hasBodyParam}}
{{#bodyParams}}
{{/authMethods}}{{/hasAuthMethods}}
{{#isMultipart}}{{#hasFormParams}}
let form = reqwest::multipart::Form::new()
{{#formParams}}
{{#isFile}}
.file("{{{baseName}}}", {{{paramName}}})?{{#-last}};{{/-last}}
{{/isFile}}
{{^isFile}}
.text("{{{baseName}}}", {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()){{#-last}};{{/-last}}
{{/isFile}}
{{/formParams}}
req_builder = req_builder.multipart(form);
{{/hasFormParams}}{{/isMultipart}}
{{^isMultipart}}{{#hasFormParams}}
let mut form_params = std::collections::HashMap::new();
{{#formParams}}
{{#isFile}}
form_params.insert("{{{baseName}}}", unimplemented!("File form param not supported with x-www-form-urlencoded content"));
{{/isFile}}
{{^isFile}}
form_params.insert("{{{baseName}}}", {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string());
{{/isFile}}
{{/formParams}}
req_builder = req_builder.form(&form_params);
{{/hasFormParams}}{{/isMultipart}}
{{#hasBodyParam}}{{#bodyParams}}
req_builder = req_builder.json(&{{{paramName}}});
{{/bodyParams}}
{{/hasBodyParam}}
{{/bodyParams}}{{/hasBodyParam}}

// send request
let req = req_builder.build()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_json;
pub enum Error {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Io(std::io::Error),
}

impl From<reqwest::Error> for Error {
Expand All @@ -19,6 +20,16 @@ impl From<serde_json::Error> for Error {
}
}

impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
return Error::Io(e)
}
}

pub fn urlencode<T: AsRef<str>>(s: T) -> String {
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}

use super::models::*;

{{#apiInfo}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.4-SNAPSHOT
4.0.0-SNAPSHOT
10 changes: 5 additions & 5 deletions samples/client/petstore/rust-reqwest/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Method | HTTP request | Description


# **add_pet**
> add_pet(ctx, pet)
> add_pet(ctx, body)
Add a new pet to the store

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**ctx** | **context.Context** | context containing the authentication | nil if no authentication
**pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store |
**body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store |

### Return type

Expand Down Expand Up @@ -160,15 +160,15 @@ Name | Type | Description | Notes
[[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)

# **update_pet**
> update_pet(ctx, pet)
> update_pet(ctx, body)
Update an existing pet

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**ctx** | **context.Context** | context containing the authentication | nil if no authentication
**pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store |
**body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store |

### Return type

Expand Down Expand Up @@ -240,7 +240,7 @@ Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**pet_id** | **i64**| ID of pet to update |
**additional_metadata** | **String**| Additional data to pass to server |
**file** | **::models::File**| file to upload |
**file** | **&std::path::Path**| file to upload |

### Return type

Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/rust-reqwest/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ 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)

# **place_order**
> ::models::Order place_order(order)
> ::models::Order place_order(body)
Place an order for a pet

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**order** | [**Order**](Order.md)| order placed for purchasing the pet |
**body** | [**Order**](Order.md)| order placed for purchasing the pet |

### Return type

Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/rust-reqwest/docs/UserApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Method | HTTP request | Description


# **create_user**
> create_user(user)
> create_user(body)
Create user

This can only be done by the logged in user.
Expand All @@ -24,7 +24,7 @@ This can only be done by the logged in user.

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**user** | [**User**](User.md)| Created user object |
**body** | [**User**](User.md)| Created user object |

### Return type

Expand All @@ -42,14 +42,14 @@ 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)

# **create_users_with_array_input**
> create_users_with_array_input(user)
> create_users_with_array_input(body)
Creates list of users with given input array

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**user** | [**Vec<::models::User>**](array.md)| List of user object |
**body** | [**Vec<::models::User>**](array.md)| List of user object |

### Return type

Expand All @@ -67,14 +67,14 @@ 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)

# **create_users_with_list_input**
> create_users_with_list_input(user)
> create_users_with_list_input(body)
Creates list of users with given input array

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**user** | [**Vec<::models::User>**](array.md)| List of user object |
**body** | [**Vec<::models::User>**](array.md)| List of user object |

### Return type

Expand Down Expand Up @@ -192,7 +192,7 @@ 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)

# **update_user**
> update_user(username, user)
> update_user(username, body)
Updated user

This can only be done by the logged in user.
Expand All @@ -202,7 +202,7 @@ This can only be done by the logged in user.
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**username** | **String**| name that need to be deleted |
**user** | [**User**](User.md)| Updated user object |
**body** | [**User**](User.md)| Updated user object |

### Return type

Expand Down
11 changes: 11 additions & 0 deletions samples/client/petstore/rust-reqwest/src/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_json;
pub enum Error {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Io(std::io::Error),
}

impl From<reqwest::Error> for Error {
Expand All @@ -19,6 +20,16 @@ impl From<serde_json::Error> for Error {
}
}

impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
return Error::Io(e)
}
}

pub fn urlencode<T: AsRef<str>>(s: T) -> String {
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}

use super::models::*;

mod pet_api;
Expand Down
Loading