Skip to content

Upgrade rust-hyper to use hyper 1.0 #19115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 14, 2024
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
8 changes: 8 additions & 0 deletions bin/configs/rust-hyper0x-petstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: rust
outputDir: samples/client/petstore/rust/hyper0x/petstore
library: hyper0x
inputSpec: modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/rust
additionalProperties:
supportAsync: "false"
packageName: petstore-hyper0x
2 changes: 1 addition & 1 deletion docs/generators/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|bestFitInt|Use best fitting integer type where minimum or maximum is set| |false|
|enumNameSuffix|Suffix that will be appended to all enum names.| ||
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|library|library template (sub-template) to use.|<dl><dt>**hyper**</dt><dd>HTTP client: Hyper.</dd><dt>**reqwest**</dt><dd>HTTP client: Reqwest.</dd></dl>|reqwest|
|library|library template (sub-template) to use.|<dl><dt>**hyper**</dt><dd>HTTP client: Hyper (v1.x).</dd><dt>**hyper0x**</dt><dd>HTTP client: Hyper (v0.x).</dd><dt>**reqwest**</dt><dd>HTTP client: Reqwest.</dd></dl>|reqwest|
|packageName|Rust package name (convention: lowercase).| |openapi|
|packageVersion|Rust package version.| |1.0.0|
|preferUnsignedInt|Prefer unsigned integers where minimum value is &gt;= 0| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
public static final String PACKAGE_NAME = "packageName";
public static final String PACKAGE_VERSION = "packageVersion";
public static final String HYPER_LIBRARY = "hyper";
public static final String HYPER0X_LIBRARY = "hyper0x";
public static final String REQWEST_LIBRARY = "reqwest";
public static final String SUPPORT_ASYNC = "supportAsync";
public static final String SUPPORT_MIDDLEWARE = "supportMiddleware";
Expand Down Expand Up @@ -202,7 +203,8 @@ public RustClientCodegen() {
cliOptions.add(new CliOption(AVOID_BOXED_MODELS, "If set, `Box<T>` will not be used for models", SchemaTypeUtil.BOOLEAN_TYPE)
.defaultValue(Boolean.FALSE.toString()));

supportedLibraries.put(HYPER_LIBRARY, "HTTP client: Hyper.");
supportedLibraries.put(HYPER_LIBRARY, "HTTP client: Hyper (v1.x).");
supportedLibraries.put(HYPER0X_LIBRARY, "HTTP client: Hyper (v0.x).");
supportedLibraries.put(REQWEST_LIBRARY, "HTTP client: Reqwest.");

CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use.");
Expand Down Expand Up @@ -371,6 +373,9 @@ public void processOpts() {

if (HYPER_LIBRARY.equals(getLibrary())) {
additionalProperties.put(HYPER_LIBRARY, "true");
} else if (HYPER0X_LIBRARY.equals(getLibrary())) {
additionalProperties.put(HYPER_LIBRARY, "true");
additionalProperties.put(HYPER0X_LIBRARY, "true");
} else if (REQWEST_LIBRARY.equals(getLibrary())) {
additionalProperties.put(REQWEST_LIBRARY, "true");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ serde_json = "^1.0"
url = "^2.5"
uuid = { version = "^1.8", features = ["serde", "v4"] }
{{#hyper}}
{{#hyper0x}}
hyper = { version = "~0.14", features = ["full"] }
hyper-tls = "~0.5"
{{/hyper0x}}
{{^hyper0x}}
hyper = { version = "^1.3.1", features = ["full"] }
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
http-body-util = { version = "0.1.2" }
{{/hyper0x}}
http = "~0.2"
base64 = "~0.7.0"
futures = "^0.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ use std::pin::Pin;
use std::option::Option;

use hyper;
use hyper_util::client::legacy::connect::Connect;
use futures::Future;

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

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

impl<C: hyper::client::connect::Connect> {{{classname}}}Client<C>
impl<C: Connect> {{{classname}}}Client<C>
where C: Clone + std::marker::Send + Sync {
pub fn new(configuration: Rc<configuration::Configuration<C>>) -> {{{classname}}}Client<C> {
{{{classname}}}Client {
Expand All @@ -34,7 +35,7 @@ pub trait {{{classname}}} {
{{/operations}}
}

impl<C: hyper::client::connect::Connect>{{{classname}}} for {{{classname}}}Client<C>
impl<C: Connect>{{{classname}}} for {{{classname}}}Client<C>
where C: Clone + std::marker::Send + Sync {
{{#operations}}
{{#operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
use http;
use std::fmt;
use std::fmt::Debug;

use hyper;
use hyper::http;
use hyper_util::client::legacy::connect::Connect;
use serde_json;

#[derive(Debug)]
pub enum Error {
Api(ApiError),
Header(hyper::http::header::InvalidHeaderValue),
Header(http::header::InvalidHeaderValue),
Http(http::Error),
Hyper(hyper::Error),
HyperClient(hyper_util::client::legacy::Error),
Serde(serde_json::Error),
UriError(http::uri::InvalidUri),
}

#[derive(Debug)]
pub struct ApiError {
pub code: hyper::StatusCode,
pub body: hyper::body::Body,
pub body: hyper::body::Incoming,
}

impl Debug for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ApiError")
.field("code", &self.code)
.field("body", &"hyper::body::Incoming")
.finish()
}
}

impl From<(hyper::StatusCode, hyper::body::Body)> for Error {
fn from(e: (hyper::StatusCode, hyper::body::Body)) -> Self {
impl From<(hyper::StatusCode, hyper::body::Incoming)> for Error {
fn from(e: (hyper::StatusCode, hyper::body::Incoming)) -> Self {
Error::Api(ApiError {
code: e.0,
body: e.1,
Expand All @@ -33,6 +46,12 @@ impl From<http::Error> for Error {
}
}

impl From<hyper_util::client::legacy::Error> for Error {
fn from(e: hyper_util::client::legacy::Error) -> Self {
Error::HyperClient(e)
}
}

impl From<hyper::Error> for Error {
fn from(e: hyper::Error) -> Self {
Error::Hyper(e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::rc::Rc;

use hyper;
use hyper_util::client::legacy::connect::Connect;
use super::configuration::Configuration;

pub struct APIClient {
Expand All @@ -18,7 +19,7 @@ pub struct APIClient {
}

impl APIClient {
pub fn new<C: hyper::client::connect::Connect>(configuration: Configuration<C>) -> APIClient
pub fn new<C: Connect>(configuration: Configuration<C>) -> APIClient
where C: Clone + std::marker::Send + Sync + 'static {
let rc = Rc::new(configuration);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{{>partial_header}}
use hyper;
use hyper_util::client::legacy::Client;
use hyper_util::client::legacy::connect::Connect;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::rt::TokioExecutor;

pub struct Configuration<C: hyper::client::connect::Connect>
pub struct Configuration<C: Connect = HttpConnector>
where C: Clone + std::marker::Send + Sync + 'static {
pub base_path: String,
pub user_agent: Option<String>,
pub client: hyper::client::Client<C>,
pub client: Client<C, String>,
pub basic_auth: Option<BasicAuth>,
pub oauth_access_token: Option<String>,
pub api_key: Option<ApiKey>,
Expand All @@ -19,9 +23,41 @@ pub struct ApiKey {
pub key: String,
}

impl<C: hyper::client::connect::Connect> Configuration<C>
impl Configuration<HttpConnector> {
/// Construct a default [`Configuration`](Self) with a hyper client using a default
/// [`HttpConnector`](hyper_util::client::legacy::connect::HttpConnector).
///
/// Use [`with_client`](Configuration<T>::with_client) to construct a Configuration with a
/// custom hyper client.
///
/// # Example
///
/// ```
/// let api_config = {
/// api_key: "my-api-key",
/// ...Configuration::new()
/// }
/// ```
pub fn new() -> Configuration<HttpConnector> {
Configuration::default()
}
}

impl<C: Connect> Configuration<C>
where C: Clone + std::marker::Send + Sync {
pub fn new(client: hyper::client::Client<C>) -> Configuration<C> {

/// Construct a new Configuration with a custom hyper client.
///
/// # Example
///
/// ```
/// let client = Client::builder(TokioExecutor::new())
/// .pool_idle_timeout(Duration::from_secs(30))
/// .build_http();
///
/// let api_config = Configuration::with_client(client);
/// ```
pub fn with_client(client: Client<C, String>) -> Configuration<C> {
Configuration {
base_path: "{{{basePath}}}".to_owned(),
user_agent: {{#httpUserAgent}}Some("{{{.}}}".to_owned()){{/httpUserAgent}}{{^httpUserAgent}}Some("OpenAPI-Generator/{{{version}}}/rust".to_owned()){{/httpUserAgent}},
Expand All @@ -32,3 +68,10 @@ impl<C: hyper::client::connect::Connect> Configuration<C>
}
}
}

impl Default for Configuration<HttpConnector> {
fn default() -> Self {
let client = Client::builder(TokioExecutor::new()).build_http();
Configuration::with_client(client)
}
}
Loading
Loading