Skip to content

Commit

Permalink
[rust] Update to modern hyper and futures crates
Browse files Browse the repository at this point in the history
  • Loading branch information
softdevca authored and jsoverson committed Aug 19, 2021
1 parent 9312ed8 commit 07e81c7
Show file tree
Hide file tree
Showing 14 changed files with 424 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ serde_derive = "^1.0"
serde_json = "^1.0"
url = "^2.2"
{{#hyper}}
hyper = "~0.11"
hyper = { version = "~0.14", features = ["full"] }
hyper-tls = "~0.5"
http = "~0.2"
serde_yaml = "0.7"
base64 = "~0.7.0"
futures = "0.1.23"
futures = "^0.3"
{{/hyper}}
{{#reqwest}}
{{^supportAsync}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{{>partial_header}}
use std::rc::Rc;
use std::borrow::Borrow;
use std::pin::Pin;
#[allow(unused_imports)]
use std::option::Option;

use hyper;
use serde_json;
use futures::Future;

use super::{Error, configuration};
use super::request as __internal_request;

pub struct {{{classname}}}Client<C: hyper::client::Connect> {
pub struct {{{classname}}}Client<C: hyper::client::connect::Connect>
where C: Clone + std::marker::Send + Sync + 'static {
configuration: Rc<configuration::Configuration<C>>,
}

impl<C: hyper::client::Connect> {{{classname}}}Client<C> {
impl<C: hyper::client::connect::Connect> {{{classname}}}Client<C>
where C: Clone + std::marker::Send + Sync {
pub fn new(configuration: Rc<configuration::Configuration<C>>) -> {{{classname}}}Client<C> {
{{{classname}}}Client {
configuration,
Expand All @@ -26,16 +28,18 @@ impl<C: hyper::client::Connect> {{{classname}}}Client<C> {
pub trait {{{classname}}} {
{{#operations}}
{{#operation}}
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Box<dyn Future<Item = {{^returnType}}(){{/returnType}}{{{returnType}}}, Error = Error<serde_json::Value>>>;
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Pin<Box<dyn Future<Output = Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error>>>>;
{{/operation}}
{{/operations}}
}

impl<C: hyper::client::Connect>{{{classname}}} for {{{classname}}}Client<C> {
{{#operations}}
{{#operation}}
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Box<dyn Future<Item = {{^returnType}}(){{/returnType}}{{{returnType}}}, Error = Error<serde_json::Value>>> {
let mut req = __internal_request::Request::new(hyper::Method::{{{httpMethod}}}, "{{{path}}}".to_string())
impl<C: hyper::client::connect::Connect>{{{classname}}} for {{{classname}}}Client<C>
where C: Clone + std::marker::Send + Sync {
{{#operations}}
{{#operation}}
#[allow(unused_mut)]
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Pin<Box<dyn Future<Output = Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{.}}}{{/returnType}}, Error>>>> {
let mut req = __internal_request::Request::new(hyper::Method::{{{httpMethod.toUpperCase}}}, "{{{path}}}".to_string())
{{#hasAuthMethods}}
{{#authMethods}}
{{#isApiKey}}
Expand Down Expand Up @@ -68,7 +72,8 @@ impl<C: hyper::client::Connect>{{{classname}}} for {{{classname}}}Client<C> {
{{/required}}
{{^required}}
if let Some(ref s) = {{{paramName}}} {
req = req.with_query_param("{{{baseName}}}".to_string(), s{{#isArray}}.join(","){{/isArray}}.to_string());
let query_value = {{#isArray}}s.iter().map(|s| s.to_string()).collect::<Vec<String>>().join(","){{/isArray}}{{^isArray}}s.to_string(){{/isArray}};
req = req.with_query_param("{{{baseName}}}".to_string(), query_value);
}
{{/required}}
{{/queryParams}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
use http;
use hyper;
use serde;
use serde_json;

#[derive(Debug)]
pub enum Error<T> {
UriError(hyper::error::UriError),
pub enum Error {
Api(ApiError),
Header(hyper::http::header::InvalidHeaderValue),
Http(http::Error),
Hyper(hyper::Error),
Serde(serde_json::Error),
ApiError(ApiError<T>),
UriError(http::uri::InvalidUri),
}

#[derive(Debug)]
pub struct ApiError<T> {
pub struct ApiError {
pub code: hyper::StatusCode,
pub content: Option<T>,
pub body: hyper::body::Body,
}

impl<'de, T> From<(hyper::StatusCode, &'de [u8])> for Error<T>
where T: serde::Deserialize<'de> {
fn from(e: (hyper::StatusCode, &'de [u8])) -> Self {
if e.1.len() == 0 {
return Error::ApiError(ApiError{
code: e.0,
content: None,
});
}
match serde_json::from_slice::<T>(e.1) {
Ok(t) => Error::ApiError(ApiError{
code: e.0,
content: Some(t),
}),
Err(e) => {
Error::from(e)
}
}
impl From<(hyper::StatusCode, hyper::body::Body)> for Error {
fn from(e: (hyper::StatusCode, hyper::body::Body)) -> Self {
Error::Api(ApiError {
code: e.0,
body: e.1,
})
}
}

impl<T> From<hyper::Error> for Error<T> {
impl From<http::Error> for Error {
fn from(e: http::Error) -> Self {
return Error::Http(e)
}
}

impl From<hyper::Error> for Error {
fn from(e: hyper::Error) -> Self {
return Error::Hyper(e)
}
}

impl<T> From<serde_json::Error> for Error<T> {
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
return Error::Serde(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub struct APIClient {
}

impl APIClient {
pub fn new<C: hyper::client::Connect>(configuration: Configuration<C>) -> APIClient {
pub fn new<C: hyper::client::connect::Connect>(configuration: Configuration<C>) -> APIClient
where C: Clone + std::marker::Send + Sync + 'static {
let rc = Rc::new(configuration);
APIClient {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{>partial_header}}
use hyper;

pub struct Configuration<C: hyper::client::Connect> {
pub struct Configuration<C: hyper::client::connect::Connect>
where C: Clone + std::marker::Send + Sync + 'static {
pub base_path: String,
pub user_agent: Option<String>,
pub client: hyper::client::Client<C>,
Expand All @@ -18,12 +19,13 @@ pub struct ApiKey {
pub key: String,
}

impl<C: hyper::client::Connect> Configuration<C> {
impl<C: hyper::client::connect::Connect> Configuration<C>
where C: Clone + std::marker::Send + Sync {
pub fn new(client: hyper::client::Client<C>) -> Configuration<C> {
Configuration {
base_path: "{{{basePath}}}".to_owned(),
user_agent: {{#httpUserAgent}}Some("{{{.}}}".to_owned()){{/httpUserAgent}}{{^httpUserAgent}}Some("OpenAPI-Generator/{{{version}}}/rust".to_owned()){{/httpUserAgent}},
client: client,
client,
basic_auth: None,
oauth_access_token: None,
api_key: None,
Expand Down
Loading

0 comments on commit 07e81c7

Please sign in to comment.