diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs index 57f7ba9..be895f1 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs @@ -8,6 +8,7 @@ pub struct MethodBuilder { auth_module_path: proc_macro2::TokenStream, return_type: Option, description: Option, + form: bool, } impl MethodBuilder { @@ -22,6 +23,7 @@ impl MethodBuilder { auth_module_path, return_type: None, description: None, + form: false, } } @@ -35,6 +37,11 @@ impl MethodBuilder { self } + pub fn with_form(mut self) -> Self { + self.form = true; + self + } + pub fn build(&self) -> proc_macro2::TokenStream { let method_name = &self.method_name; let (return_type, parse_type) = match &self.return_type { @@ -43,13 +50,19 @@ impl MethodBuilder { }; let url = &self.url; let auth_module_path = &self.auth_module_path; + let form = if self.form { + quote! { .multipart(self.form) } + } else { + quote! {} + }; util::add_docs( &self.description, quote! { - pub async fn #method_name(&self) -> Result<#return_type> { + pub async fn #method_name(self) -> Result<#return_type> { let res = #auth_module_path .authenticated_client(#url) + #form .send() .await? #parse_type diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs index 4e3f80c..6618ce5 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs @@ -3,7 +3,7 @@ use quote::quote; use crate::{generate::util, parser, types}; -use super::return_type::create_return_type; +use super::{method_builder::MethodBuilder, return_type::create_return_type}; pub fn create_method_with_params( group: &parser::ApiGroup, @@ -83,9 +83,13 @@ pub fn create_method_with_params( }); let group_name = util::to_ident(&group.name.to_camel()); + let send_builder = + MethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }).with_form(); let send = match create_return_type(group, method) { Some((return_type_name, return_type)) => { + let send_method = send_builder.return_type(&return_type_name).build(); + quote! { impl<'a> #parameter_type<'a> { fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self { @@ -96,24 +100,15 @@ pub fn create_method_with_params( #(#optional_params)* - pub async fn send(self) -> Result<#return_type_name> { - let res = self.group - .auth - .authenticated_client(#url) - .multipart(self.form) - .send() - .await? - .json::<#return_type_name>() - .await?; - - Ok(res) - } + #send_method } #return_type } } None => { + let send_method = send_builder.build(); + quote! { impl<'a> #parameter_type<'a> { fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self { @@ -124,18 +119,7 @@ pub fn create_method_with_params( #(#optional_params)* - pub async fn send(self) -> Result { - let res = self.group - .auth - .authenticated_client(#url) - .multipart(self.form) - .send() - .await? - .text() - .await?; - - Ok(res) - } + #send_method } } }