Skip to content

Commit

Permalink
Use async file io and don't unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
C0D3-M4513R committed Nov 12, 2024
1 parent 3dcecf1 commit d22df3b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "MIT"
edition = "2021"

[dependencies]
async-std = "1"
serde = { version = "^1.0", features = ["derive"] }
serde_with = { version = "^3.8", default-features = false, features = ["base64", "std", "macros"] }
serde_json = "^1.0"
Expand Down
13 changes: 11 additions & 2 deletions generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ rm src/apis src/models docs -rf
--git-user-id=vrchatapi \
--git-repo-id=vrchatapi-rust \
-o . \
-i https://raw.githubusercontent.com/vrchatapi/specification/gh-pages/openapi.yaml \
-i ./openapi.yaml \
--http-user-agent="vrchatapi-rust"
#https://raw.githubusercontent.com/vrchatapi/specification/gh-pages/openapi.yaml \
#--global-property debugOperations=true

# Update entire description (replace entire line, match the random data there) line in Cargo.toml
Expand Down Expand Up @@ -42,7 +43,15 @@ sed -i 's/Result<models::CurrentUser, Error<GetCurrentUserError>>/Result<models:
# https://github.com/vrchatapi/vrchatapi-rust/pull/29
sed -i "s/local_var_req_builder = local_var_req_builder.json(&\(.*\));/if let Some(\1) = \1 { \0 }/g" src/apis/files_api.rs

sed -i 's|// TODO: support file upload for '\''file'\'' parameter|let part = reqwest::multipart::Part::bytes(std::fs::read(\&file).unwrap()).file_name(file.file_name().unwrap().to_string_lossy().to_string()).mime_str(if file.file_name().unwrap().to_string_lossy().to_string().ends_with("png") { "image/png" } else { "application/octet-stream" })?;\n\tlocal_var_form = local_var_form.part("file", part);|' src/apis/files_api.rs
#https://github.com/vrchatapi/vrchatapi-rust/pull/30
#Add async_std for async file io
sed -i 's/\[dependencies\]/\0\nasync-std = "1"/' Cargo.toml
sed -i "s/pub\s\+enum\s\+Error<T>\s\+{/\0\nAsyncStdIo(::async_std::io::Error),/" src/apis/mod.rs
sed -E -i 's/Error::Reqwest\(e\)\s+=>\s+\("reqwest", e\.to_string\(\)\),/Error::AsyncStdIo(e) => ("async_std", e.to_string()),\0/' src/apis/mod.rs #Fix Debug
sed -E -i 's/Error::Reqwest\(e\)\s+=>\s+\e,/Error::AsyncStdIo(e) => e,\n\0/' src/apis/mod.rs #Fix Error
sed -i "s/impl<T> From<reqwest::Error> for Error<T>/impl<T> From<::async_std::io::Error> for Error<T> {\nfn from(val: ::async_std::io::Error) -> Self {\n Error::AsyncStdIo(val)\n}\n\}\n\0/" src/apis/mod.rs #Add From impl
#Use the async file io
sed -i 's|// TODO: support file upload for '\''file'\'' parameter|let part = {let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(\&file).await?);\nif let Some(filename) = file.file_name() { \npart = part.file_name(filename.to_string_lossy().to_string()).mime_str(if filename.to_string_lossy().ends_with("png") { "image/png" } else { "application/octet-stream" })?;} \n else { part = part.mime_str("application/octet-stream")?; }\n part };\n\tlocal_var_form = local_var_form.part("file", part);|' src/apis/files_api.rs

cargo fmt
cargo build
Expand Down
90 changes: 45 additions & 45 deletions src/apis/files_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,21 +585,21 @@ pub async fn upload_gallery_image(
local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
let mut local_var_form = reqwest::multipart::Form::new();
let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap())
.file_name(file.file_name().unwrap().to_string_lossy().to_string())
.mime_str(
if file
.file_name()
.unwrap()
.to_string_lossy()
.to_string()
.ends_with("png")
{
"image/png"
} else {
"application/octet-stream"
},
)?;
let part = {
let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?);
if let Some(filename) = file.file_name() {
part = part
.file_name(filename.to_string_lossy().to_string())
.mime_str(if filename.to_string_lossy().ends_with("png") {
"image/png"
} else {
"application/octet-stream"
})?;
} else {
part = part.mime_str("application/octet-stream")?;
}
part
};
local_var_form = local_var_form.part("file", part);
local_var_req_builder = local_var_req_builder.multipart(local_var_form);

Expand Down Expand Up @@ -641,21 +641,21 @@ pub async fn upload_icon(
local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
let mut local_var_form = reqwest::multipart::Form::new();
let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap())
.file_name(file.file_name().unwrap().to_string_lossy().to_string())
.mime_str(
if file
.file_name()
.unwrap()
.to_string_lossy()
.to_string()
.ends_with("png")
{
"image/png"
} else {
"application/octet-stream"
},
)?;
let part = {
let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?);
if let Some(filename) = file.file_name() {
part = part
.file_name(filename.to_string_lossy().to_string())
.mime_str(if filename.to_string_lossy().ends_with("png") {
"image/png"
} else {
"application/octet-stream"
})?;
} else {
part = part.mime_str("application/octet-stream")?;
}
part
};
local_var_form = local_var_form.part("file", part);
local_var_req_builder = local_var_req_builder.multipart(local_var_form);

Expand Down Expand Up @@ -700,21 +700,21 @@ pub async fn upload_image(
local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
let mut local_var_form = reqwest::multipart::Form::new();
let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap())
.file_name(file.file_name().unwrap().to_string_lossy().to_string())
.mime_str(
if file
.file_name()
.unwrap()
.to_string_lossy()
.to_string()
.ends_with("png")
{
"image/png"
} else {
"application/octet-stream"
},
)?;
let part = {
let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?);
if let Some(filename) = file.file_name() {
part = part
.file_name(filename.to_string_lossy().to_string())
.mime_str(if filename.to_string_lossy().ends_with("png") {
"image/png"
} else {
"application/octet-stream"
})?;
} else {
part = part.mime_str("application/octet-stream")?;
}
part
};
local_var_form = local_var_form.part("file", part);
local_var_form = local_var_form.text("tag", tag.to_string());
if let Some(local_var_param_value) = animation_style {
Expand Down
3 changes: 3 additions & 0 deletions src/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct ResponseContent<T> {

#[derive(Debug)]
pub enum Error<T> {
AsyncStdIo(::async_std::io::Error),
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Io(std::io::Error),
Expand All @@ -19,6 +20,7 @@ pub enum Error<T> {
impl<T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::AsyncStdIo(e) => ("async_std", e.to_string()),
Error::Reqwest(e) => ("reqwest", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Expand All @@ -31,6 +33,7 @@ impl<T> fmt::Display for Error<T> {
impl<T: fmt::Debug> error::Error for Error<T> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::AsyncStdIo(e) => e,
Error::Reqwest(e) => e,
Error::Serde(e) => e,
Error::Io(e) => e,
Expand Down

0 comments on commit d22df3b

Please sign in to comment.