From 5705f29f1a5de344a05b428e0197d3cfed910183 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Fri, 15 Jul 2022 00:26:54 +0000 Subject: [PATCH] Add form args --- .../src/generate/group/method/method_with_params.rs | 12 ++++++++---- .../src/generate/group/method/send_method_builder.rs | 12 +++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) 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 d6d9dc6..5796699 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 @@ -57,9 +57,14 @@ struct MethodGenerator<'a> { impl<'a> MethodGenerator<'a> { fn generate_method_without_builder(&self) -> MethodsAndExtra { + let form_builder = self.parameters.mandatory.form_builder(); + let form_attributes = + quote! { .multipart(reqwest::multipart::Form::new()#(#form_builder)*) }; + let builder = SendMethodBuilder::new(self.method_name, self.url, quote! { self.auth }) .description(&self.method.description) - .with_args(&self.parameters.mandatory.generate_params()); + .with_args(&self.parameters.mandatory.generate_params()) + .with_extra_form_args(&[form_attributes]); match create_return_type(self.group, self.method) { Some((return_type_name, return_type)) => { @@ -169,8 +174,7 @@ impl<'a> SendImplGenerator<'a> { quote! { impl<'a> #param_type<'a> { fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self { - let form = reqwest::multipart::Form::new(); - #(#mandatory_param_form_build)* + let form = reqwest::multipart::Form::new()#(#mandatory_param_form_build)*; Self { group, form } } @@ -226,7 +230,7 @@ impl<'a> MandatoryParams<'a> { .map(|param| { let name_ident = param.name_ident(); let name = param.name(); - quote! { let form = form.text(#name, #name_ident.to_string()); } + quote! { .text(#name, #name_ident.to_string()) } }) .collect() } diff --git a/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs b/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs index 0b1194a..ffd3ad8 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs @@ -9,6 +9,7 @@ pub struct SendMethodBuilder { return_type: Option, description: Option, args: Vec, + extra_form_args: Vec, form: bool, } @@ -26,6 +27,7 @@ impl SendMethodBuilder { description: None, form: false, args: vec![], + extra_form_args: vec![], } } @@ -45,10 +47,12 @@ impl SendMethodBuilder { } pub fn with_args(mut self, value: &[proc_macro2::TokenStream]) -> Self { - for v in value { - self.args.push(v.clone()); - } + self.args = value.to_vec(); + self + } + pub fn with_extra_form_args(mut self, value: &[proc_macro2::TokenStream]) -> Self { + self.extra_form_args = value.to_vec(); self } @@ -71,6 +75,7 @@ impl SendMethodBuilder { } else { quote! {} }; + let extra_form_args = &self.extra_form_args; util::add_docs( &self.description, @@ -79,6 +84,7 @@ impl SendMethodBuilder { let res = #auth_module_path .authenticated_client(#url) #form + #(#extra_form_args)* .send() .await? #parse_type