feat: add async file stream support for reqwest client (#21771)

This commit is contained in:
andershausding 2025-08-22 09:02:55 +02:00 committed by GitHub
parent dbe0419034
commit 2569321b82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 629 additions and 563 deletions

View File

@ -672,6 +672,7 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
OperationMap objectMap = objs.getOperations();
boolean useAsyncFileStream = false;
List<CodegenOperation> operations = objectMap.getOperation();
for (CodegenOperation operation : operations) {
if (operation.pathParams != null && operation.pathParams.size() > 0) {
@ -707,6 +708,17 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
}
}
// If we use a file parameter, we need to include the imports and crates for it
// But they should be added only once per file
for (var param: operation.allParams) {
if (param.isFile && supportAsync && !useAsyncFileStream) {
useAsyncFileStream = true;
additionalProperties.put("useAsyncFileStream", Boolean.TRUE);
operation.vendorExtensions.put("useAsyncFileStream", Boolean.TRUE);
break;
}
}
// http method verb conversion, depending on client library (e.g. Hyper: PUT => Put, Reqwest: PUT => put)
if (HYPER_LIBRARY.equals(getLibrary())) {
operation.httpMethod = StringUtils.camelize(operation.httpMethod.toLowerCase(Locale.ROOT));

View File

@ -69,7 +69,14 @@ reqwest-middleware = { version = "^0.4", features = ["json", "blocking", "multip
{{/supportMiddleware}}
{{/supportAsync}}
{{#supportAsync}}
{{#useAsyncFileStream}}
tokio = { version = "^1.46.0", features = ["fs"] }
tokio-util = { version = "^0.7", features = ["codec"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart", "stream"] }
{{/useAsyncFileStream}}
{{^useAsyncFileStream}}
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart"] }
{{/useAsyncFileStream}}
{{#supportMiddleware}}
reqwest-middleware = { version = "^0.4", features = ["json", "multipart"] }
{{/supportMiddleware}}

View File

@ -4,6 +4,14 @@ use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
{{#operations}}
{{#operation}}
{{#vendorExtensions.useAsyncFileStream}}
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
{{/vendorExtensions.useAsyncFileStream}}
{{/operation}}
{{/operations}}
{{#operations}}
{{#operation}}
@ -445,7 +453,23 @@ pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}(configuration:
{{#hasBodyParam}}
{{#bodyParams}}
{{#isFile}}
{{#supportAsync}}
{{#required}}
let file = TokioFile::open({{{vendorExtensions.x-rust-param-identifier}}}).await?;
let stream = FramedRead::new(file, BytesCodec::new());
req_builder = req_builder.body(reqwest::Body::wrap_stream(stream));
{{/required}}
{{^required}}
if let Some(param_value) = {{{vendorExtensions.x-rust-param-identifier}}} {
let file = TokioFile::open(param_value).await?;
let stream = FramedRead::new(file, BytesCodec::new());
req_builder = req_builder.body(reqwest::Body::wrap_stream(stream));
}
{{/required}}
{{/supportAsync}}
{{^supportAsync}}
req_builder = req_builder.body({{{vendorExtensions.x-rust-param-identifier}}});
{{/supportAsync}}
{{/isFile}}
{{^isFile}}
req_builder = req_builder.json(&{{{vendorExtensions.x-rust-param-identifier}}});

View File

@ -0,0 +1,23 @@
# file_streaming_test.yaml
swagger: "2.0"
info:
title: File Streaming Test API
version: 1.0.0
paths:
/upload:
post:
consumes:
- application/octet-stream
description: Upload a file from the local filesystem
operationId: uploadFile
parameters:
- description: A test file
in: body
name: inputStream
schema:
format: binary
type: string
responses:
'200':
description: Success

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
/target/
**/*.rs.bk
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,11 @@
.gitignore
.travis.yml
Cargo.toml
README.md
docs/DefaultApi.md
git_push.sh
src/apis/configuration.rs
src/apis/default_api.rs
src/apis/mod.rs
src/lib.rs
src/models/mod.rs

View File

@ -0,0 +1 @@
7.15.0-SNAPSHOT

View File

@ -0,0 +1 @@
language: rust

View File

@ -0,0 +1,17 @@
[package]
name = "output"
version = "1.0.0"
authors = ["OpenAPI Generator team and contributors"]
description = "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)"
# Override this license by providing a License Object in the OpenAPI.
license = "Unlicense"
edition = "2021"
[dependencies]
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
serde_repr = "^0.1"
url = "^2.5"
tokio = { version = "^1.46.0", features = ["fs"] }
tokio-util = { version = "^0.7", features = ["codec"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart", "stream"] }

View File

@ -0,0 +1,45 @@
# Rust API client for output
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
## Overview
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [openapi-spec](https://openapis.org) from a remote server, you can easily generate an API client.
- API version: 1.0.0
- Package version: 1.0.0
- Generator version: 7.15.0-SNAPSHOT
- Build package: `org.openapitools.codegen.languages.RustClientCodegen`
## Installation
Put the package under your project folder in a directory named `output` and add the following to `Cargo.toml` under `[dependencies]`:
```
output = { path = "./output" }
```
## Documentation for API Endpoints
All URIs are relative to *http://localhost*
Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*DefaultApi* | [**upload_file**](docs/DefaultApi.md#upload_file) | **POST** /upload |
## Documentation For Models
To get access to the crate's generated documentation, use:
```
cargo doc --open
```
## Author

View File

@ -0,0 +1,39 @@
# \DefaultApi
All URIs are relative to *http://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
[**upload_file**](DefaultApi.md#upload_file) | **POST** /upload |
## upload_file
> upload_file(input_stream)
Upload a file from the local filesystem
### Parameters
Name | Type | Description | Required | Notes
------------- | ------------- | ------------- | ------------- | -------------
**input_stream** | Option<**std::path::PathBuf**> | A test file | |
### Return type
(empty response body)
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: application/octet-stream
- **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,57 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 openapi-petstore-perl "minor update" "gitlab.com"
git_user_id=$1
git_repo_id=$2
release_note=$3
git_host=$4
if [ "$git_host" = "" ]; then
git_host="github.com"
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
fi
if [ "$git_user_id" = "" ]; then
git_user_id="GIT_USER_ID"
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
fi
if [ "$git_repo_id" = "" ]; then
git_repo_id="GIT_REPO_ID"
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
fi
if [ "$release_note" = "" ]; then
release_note="Minor update"
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
fi
# Initialize the local directory as a Git repository
git init
# Adds the files in the local repository and stages them for commit.
git add .
# Commits the tracked changes and prepares them to be pushed to a remote repository.
git commit -m "$release_note"
# Sets the new remote
git_remote=$(git remote)
if [ "$git_remote" = "" ]; then # git remote not defined
if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:"${GIT_TOKEN}"@${git_host}/${git_user_id}/${git_repo_id}.git
fi
fi
git pull origin master
# Pushes (Forces) the changes in the local repository up to the remote repository
echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
git push origin master 2>&1 | grep -v 'To https'

View File

@ -0,0 +1,51 @@
/*
* File Streaming Test API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/
#[derive(Debug, Clone)]
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest::Client,
pub basic_auth: Option<BasicAuth>,
pub oauth_access_token: Option<String>,
pub bearer_access_token: Option<String>,
pub api_key: Option<ApiKey>,
}
pub type BasicAuth = (String, Option<String>);
#[derive(Debug, Clone)]
pub struct ApiKey {
pub prefix: Option<String>,
pub key: String,
}
impl Configuration {
pub fn new() -> Configuration {
Configuration::default()
}
}
impl Default for Configuration {
fn default() -> Self {
Configuration {
base_path: "http://localhost".to_owned(),
user_agent: Some("OpenAPI-Generator/1.0.0/rust".to_owned()),
client: reqwest::Client::new(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
api_key: None,
}
}
}

View File

@ -0,0 +1,58 @@
/*
* File Streaming Test API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/
use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
/// struct for typed errors of method [`upload_file`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UploadFileError {
UnknownValue(serde_json::Value),
}
/// Upload a file from the local filesystem
pub async fn upload_file(configuration: &configuration::Configuration, input_stream: Option<std::path::PathBuf>) -> Result<(), Error<UploadFileError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_body_input_stream = input_stream;
let uri_str = format!("{}/upload", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(param_value) = p_body_input_stream {
let file = TokioFile::open(param_value).await?;
let stream = FramedRead::new(file, BytesCodec::new());
req_builder = req_builder.body(reqwest::Body::wrap_stream(stream));
}
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<UploadFileError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}

View File

@ -0,0 +1,116 @@
use std::error;
use std::fmt;
#[derive(Debug, Clone)]
pub struct ResponseContent<T> {
pub status: reqwest::StatusCode,
pub content: String,
pub entity: Option<T>,
}
#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
}
impl <T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::Reqwest(e) => ("reqwest", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
};
write!(f, "error in {}: {}", module, e)
}
}
impl <T: fmt::Debug> error::Error for Error<T> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::Reqwest(e) => e,
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
})
}
}
impl <T> From<reqwest::Error> for Error<T> {
fn from(e: reqwest::Error) -> Self {
Error::Reqwest(e)
}
}
impl <T> From<serde_json::Error> for Error<T> {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
}
}
impl <T> From<std::io::Error> for Error<T> {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
pub fn urlencode<T: AsRef<str>>(s: T) -> String {
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}
pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
if let serde_json::Value::Object(object) = value {
let mut params = vec![];
for (key, value) in object {
match value {
serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
&format!("{}[{}]", prefix, key),
value,
)),
serde_json::Value::Array(array) => {
for (i, value) in array.iter().enumerate() {
params.append(&mut parse_deep_object(
&format!("{}[{}][{}]", prefix, key, i),
value,
));
}
},
serde_json::Value::String(s) => params.push((format!("{}[{}]", prefix, key), s.clone())),
_ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
}
}
return params;
}
unimplemented!("Only objects are supported with style=deepObject")
}
/// Internal use only
/// A content type supported by this client.
#[allow(dead_code)]
enum ContentType {
Json,
Text,
Unsupported(String)
}
impl From<&str> for ContentType {
fn from(content_type: &str) -> Self {
if content_type.starts_with("application") && content_type.contains("json") {
return Self::Json;
} else if content_type.starts_with("text/plain") {
return Self::Text;
} else {
return Self::Unsupported(content_type.to_string());
}
}
}
pub mod default_api;
pub mod configuration;

View File

@ -0,0 +1,11 @@
#![allow(unused_imports)]
#![allow(clippy::too_many_arguments)]
extern crate serde_repr;
extern crate serde;
extern crate serde_json;
extern crate url;
extern crate reqwest;
pub mod apis;
pub mod models;

View File

@ -13,5 +13,7 @@ serde_json = "^1.0"
serde_repr = "^0.1"
url = "^2.5"
uuid = { version = "^1.8", features = ["serde", "v4"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart"] }
tokio = { version = "^1.46.0", features = ["fs"] }
tokio-util = { version = "^0.7", features = ["codec"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart", "stream"] }
reqwest-middleware = { version = "^0.4", features = ["json", "multipart"] }

View File

@ -13,6 +13,8 @@ use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
/// struct for passing parameters to the method [`add_pet`]
#[derive(Clone, Debug)]

View File

@ -13,7 +13,9 @@ serde_json = "^1.0"
serde_repr = "^0.1"
url = "^2.5"
uuid = { version = "^1.8", features = ["serde", "v4"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart"] }
tokio = { version = "^1.46.0", features = ["fs"] }
tokio-util = { version = "^0.7", features = ["codec"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart", "stream"] }
async-trait = "^0.1"
# TODO: propose to Yoshidan to externalize this as non google related crate, so that it can easily be extended for other cloud providers.
google-cloud-token = "^0.1"

View File

@ -13,6 +13,8 @@ use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
/// struct for passing parameters to the method [`add_pet`]
#[derive(Clone, Debug)]

View File

@ -13,4 +13,6 @@ serde_json = "^1.0"
serde_repr = "^0.1"
url = "^2.5"
uuid = { version = "^1.8", features = ["serde", "v4"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart"] }
tokio = { version = "^1.46.0", features = ["fs"] }
tokio-util = { version = "^0.7", features = ["codec"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart", "stream"] }

View File

@ -13,6 +13,8 @@ use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
/// struct for passing parameters to the method [`add_pet`]
#[derive(Clone, Debug)]

View File

@ -13,4 +13,6 @@ serde_json = "^1.0"
serde_repr = "^0.1"
url = "^2.5"
uuid = { version = "^1.8", features = ["serde", "v4"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart"] }
tokio = { version = "^1.46.0", features = ["fs"] }
tokio-util = { version = "^0.7", features = ["codec"] }
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart", "stream"] }

View File

@ -13,6 +13,8 @@ use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
/// struct for passing parameters to the method [`add_pet`]
#[derive(Clone, Debug)]