Skip to content

Commit 64acffc

Browse files
authored
Add support for reqwest-middleware (#41)
1 parent 4106ac6 commit 64acffc

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ serde_with = { version = "^3.8", default-features = false, features = ["base64",
1313
serde_json = "^1.0"
1414
serde_repr = "^0.1"
1515
url = "^2.5"
16-
reqwest = { version = "^0.12", default-features = false, features = ["json", "cookies", "multipart"] }
16+
reqwest = { version = "^0.12", default-features = false, features = ["json", "multipart"] }
17+
reqwest-middleware = { version = "^0.4", features = ["json", "multipart"] }
1718

1819
[features]
19-
default = ["native-tls"]
20+
default = ["reqwest/cookies"]
2021
native-tls = ["reqwest/native-tls"]
2122
rustls-tls = ["reqwest/rustls-tls"]
2223

examples/cookies_load.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ async fn main() {
2323
config.client = reqwest::Client::builder()
2424
.cookie_provider(jar)
2525
.build()
26-
.unwrap();
26+
.unwrap()
27+
.into();
2728

2829
let user = ::vrchatapi::apis::authentication_api::get_current_user(&config)
2930
.await

examples/cookies_store.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ async fn main() {
1111
config.client = reqwest::Client::builder()
1212
.cookie_provider(cookie_store.clone())
1313
.build()
14-
.unwrap();
14+
.unwrap()
15+
.into();
1516

1617
match ::vrchatapi::apis::authentication_api::get_current_user(&config)
1718
.await

generate.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rm src/apis src/models docs -rf
1212

1313
./node_modules/\@openapitools/openapi-generator-cli/main.js generate \
1414
-g rust \
15-
--additional-properties=packageName=vrchatapi,supportAsync=true,avoidBoxedModels=true \
15+
--additional-properties=packageName=vrchatapi,supportAsync=true,avoidBoxedModels=true,library=reqwest,reqwestDefaultFeatures=reqwest/cookies,supportMiddleware=true \
1616
--git-user-id=vrchatapi \
1717
--git-repo-id=vrchatapi-rust \
1818
-o . \
@@ -34,7 +34,7 @@ find src -type f -exec sed -i '/^\s*\/\/\/\s*$/d' {} \;
3434

3535
# Cookie storage
3636
sed -i 's/Client::new()/Client::builder().cookie_store(true).build().unwrap()/g' src/apis/configuration.rs
37-
sed -i 's/, features = \["json", "multipart"\]/, features = \["json", "cookies", "multipart"\]/g' Cargo.toml
37+
#sed -i 's/, features = \["json", "multipart"\]/, features = \["json", "cookies", "multipart"\]/g' Cargo.toml
3838

3939
#Fix example
4040
printf "\n[dev-dependencies]\ntokio = { version = '1', features = ['macros', 'rt-multi-thread'] }" >> Cargo.toml

src/apis/configuration.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pub struct Configuration {
1111
pub base_path: String,
1212
pub user_agent: Option<String>,
13-
pub client: reqwest::Client,
13+
pub client: reqwest_middleware::ClientWithMiddleware,
1414
pub basic_auth: Option<BasicAuth>,
1515
pub oauth_access_token: Option<String>,
1616
pub bearer_access_token: Option<String>,
@@ -36,10 +36,13 @@ impl Default for Configuration {
3636
Configuration {
3737
base_path: "https://api.vrchat.cloud/api/1".to_owned(),
3838
user_agent: Some("vrchatapi-rust".to_owned()),
39-
client: reqwest::Client::builder()
40-
.cookie_store(true)
41-
.build()
42-
.unwrap(),
39+
client: reqwest_middleware::ClientBuilder::new(
40+
reqwest::Client::builder()
41+
.cookie_store(true)
42+
.build()
43+
.unwrap(),
44+
)
45+
.build(),
4346
basic_auth: None,
4447
oauth_access_token: None,
4548
bearer_access_token: None,

src/apis/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct ResponseContent<T> {
1111
#[derive(Debug)]
1212
pub enum Error<T> {
1313
Reqwest(reqwest::Error),
14+
ReqwestMiddleware(reqwest_middleware::Error),
1415
Serde(serde_json::Error),
1516
Io(std::io::Error),
1617
ResponseError(ResponseContent<T>),
@@ -20,6 +21,7 @@ impl<T> fmt::Display for Error<T> {
2021
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2122
let (module, e) = match self {
2223
Error::Reqwest(e) => ("reqwest", e.to_string()),
24+
Error::ReqwestMiddleware(e) => ("reqwest-middleware", e.to_string()),
2325
Error::Serde(e) => ("serde", e.to_string()),
2426
Error::Io(e) => ("IO", e.to_string()),
2527
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
@@ -32,6 +34,7 @@ impl<T: fmt::Debug> error::Error for Error<T> {
3234
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
3335
Some(match self {
3436
Error::Reqwest(e) => e,
37+
Error::ReqwestMiddleware(e) => e,
3538
Error::Serde(e) => e,
3639
Error::Io(e) => e,
3740
Error::ResponseError(_) => return None,
@@ -45,6 +48,12 @@ impl<T> From<reqwest::Error> for Error<T> {
4548
}
4649
}
4750

51+
impl<T> From<reqwest_middleware::Error> for Error<T> {
52+
fn from(e: reqwest_middleware::Error) -> Self {
53+
Error::ReqwestMiddleware(e)
54+
}
55+
}
56+
4857
impl<T> From<serde_json::Error> for Error<T> {
4958
fn from(e: serde_json::Error) -> Self {
5059
Error::Serde(e)

0 commit comments

Comments
 (0)