Make the rust hyper client Send so it can be used in rust threads more easily (#19375)

* Add externCrateName property to rust hyper client

This is follows the lead of the rust hyper server generator and provides
an externCrateName. This is because the crate name used for importing
can be different from the package name, because dashes `-` get converted
to underscores `_`.

This allows us to write example code in rustdoc that compiles
successfully.

* Get the rustdoc examples to actually compile

* Make rust hyper client thread safe

* Fix compile time issue with reqwest client test

* Add a test for thread safety

* Generate rust hyper samples

* Use https for petstore api to fix client tests

http://petstore.swagger.io/v2 is 301 redirecting to
https://petstore.swagger.io/v2 and this is breaking posts to the API.
When the client recieves a redirect it does not resend the POST data,
instead it switches to GET. This is in line with how browsers behave
when encountering a 301 redirect on a POST request.

* Make rust hyper client structs `Sync` too

This trait is also helpful in making the api work well with threads.

* Use a getCrateName function instead of adding more state

* update samples

---------

Co-authored-by: Krishna Rajendran <krishna@emptybox.org>
This commit is contained in:
William Cheng
2024-08-17 16:01:11 +08:00
committed by GitHub
parent 172fafe674
commit bb831dad9a
39 changed files with 248 additions and 176 deletions

View File

@@ -1,4 +1,4 @@
use std::rc::Rc;
use std::sync::Arc;
use hyper;
use hyper_util::client::legacy::connect::Connect;
@@ -11,7 +11,7 @@ pub struct APIClient {
impl APIClient {
pub fn new<C: Connect>(configuration: Configuration<C>) -> APIClient
where C: Clone + std::marker::Send + Sync + 'static {
let rc = Rc::new(configuration);
let rc = Arc::new(configuration);
APIClient {
default_api: Box::new(crate::apis::DefaultApiClient::new(rc.clone())),

View File

@@ -42,10 +42,11 @@ impl Configuration<HttpConnector> {
/// # Example
///
/// ```
/// let api_config = {
/// api_key: "my-api-key",
/// ...Configuration::new()
/// }
/// # use empty_object_hyper::apis::configuration::Configuration;
/// let api_config = Configuration {
/// basic_auth: Some(("user".into(), None)),
/// ..Configuration::new()
/// };
/// ```
pub fn new() -> Configuration<HttpConnector> {
Configuration::default()
@@ -60,6 +61,11 @@ impl<C: Connect> Configuration<C>
/// # Example
///
/// ```
/// # use core::time::Duration;
/// # use empty_object_hyper::apis::configuration::Configuration;
/// use hyper_util::client::legacy::Client;
/// use hyper_util::rt::TokioExecutor;
///
/// let client = Client::builder(TokioExecutor::new())
/// .pool_idle_timeout(Duration::from_secs(30))
/// .build_http();

View File

@@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/
use std::rc::Rc;
use std::sync::Arc;
use std::borrow::Borrow;
use std::pin::Pin;
#[allow(unused_imports)]
@@ -24,26 +24,26 @@ use super::request as __internal_request;
pub struct DefaultApiClient<C: Connect>
where C: Clone + std::marker::Send + Sync + 'static {
configuration: Rc<configuration::Configuration<C>>,
configuration: Arc<configuration::Configuration<C>>,
}
impl<C: Connect> DefaultApiClient<C>
where C: Clone + std::marker::Send + Sync {
pub fn new(configuration: Rc<configuration::Configuration<C>>) -> DefaultApiClient<C> {
pub fn new(configuration: Arc<configuration::Configuration<C>>) -> DefaultApiClient<C> {
DefaultApiClient {
configuration,
}
}
}
pub trait DefaultApi {
fn endpoint_get(&self, ) -> Pin<Box<dyn Future<Output = Result<models::EmptyObject, Error>>>>;
pub trait DefaultApi: Send + Sync {
fn endpoint_get(&self, ) -> Pin<Box<dyn Future<Output = Result<models::EmptyObject, Error>> + Send>>;
}
impl<C: Connect>DefaultApi for DefaultApiClient<C>
where C: Clone + std::marker::Send + Sync {
#[allow(unused_mut)]
fn endpoint_get(&self, ) -> Pin<Box<dyn Future<Output = Result<models::EmptyObject, Error>>>> {
fn endpoint_get(&self, ) -> Pin<Box<dyn Future<Output = Result<models::EmptyObject, Error>> + Send>> {
let mut req = __internal_request::Request::new(hyper::Method::GET, "/endpoint".to_string())
;

View File

@@ -109,7 +109,7 @@ impl Request {
pub fn execute<'a, C, U>(
self,
conf: &configuration::Configuration<C>,
) -> Pin<Box<dyn Future<Output=Result<U, Error>> + 'a>>
) -> Pin<Box<dyn Future<Output=Result<U, Error>> + 'a + Send>>
where
C: Connect + Clone + std::marker::Send + Sync,
U: Sized + std::marker::Send + 'a,