Add form args

This commit is contained in:
Joel Wachsler 2022-07-15 00:26:54 +00:00
parent cc9aeaf6db
commit 5705f29f1a
2 changed files with 17 additions and 7 deletions

View File

@ -57,9 +57,14 @@ struct MethodGenerator<'a> {
impl<'a> MethodGenerator<'a> { impl<'a> MethodGenerator<'a> {
fn generate_method_without_builder(&self) -> MethodsAndExtra { 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 }) let builder = SendMethodBuilder::new(self.method_name, self.url, quote! { self.auth })
.description(&self.method.description) .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) { match create_return_type(self.group, self.method) {
Some((return_type_name, return_type)) => { Some((return_type_name, return_type)) => {
@ -169,8 +174,7 @@ impl<'a> SendImplGenerator<'a> {
quote! { quote! {
impl<'a> #param_type<'a> { impl<'a> #param_type<'a> {
fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self { fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self {
let form = reqwest::multipart::Form::new(); let form = reqwest::multipart::Form::new()#(#mandatory_param_form_build)*;
#(#mandatory_param_form_build)*
Self { group, form } Self { group, form }
} }
@ -226,7 +230,7 @@ impl<'a> MandatoryParams<'a> {
.map(|param| { .map(|param| {
let name_ident = param.name_ident(); let name_ident = param.name_ident();
let name = param.name(); let name = param.name();
quote! { let form = form.text(#name, #name_ident.to_string()); } quote! { .text(#name, #name_ident.to_string()) }
}) })
.collect() .collect()
} }

View File

@ -9,6 +9,7 @@ pub struct SendMethodBuilder {
return_type: Option<proc_macro2::TokenStream>, return_type: Option<proc_macro2::TokenStream>,
description: Option<String>, description: Option<String>,
args: Vec<proc_macro2::TokenStream>, args: Vec<proc_macro2::TokenStream>,
extra_form_args: Vec<proc_macro2::TokenStream>,
form: bool, form: bool,
} }
@ -26,6 +27,7 @@ impl SendMethodBuilder {
description: None, description: None,
form: false, form: false,
args: vec![], args: vec![],
extra_form_args: vec![],
} }
} }
@ -45,10 +47,12 @@ impl SendMethodBuilder {
} }
pub fn with_args(mut self, value: &[proc_macro2::TokenStream]) -> Self { pub fn with_args(mut self, value: &[proc_macro2::TokenStream]) -> Self {
for v in value { self.args = value.to_vec();
self.args.push(v.clone()); self
} }
pub fn with_extra_form_args(mut self, value: &[proc_macro2::TokenStream]) -> Self {
self.extra_form_args = value.to_vec();
self self
} }
@ -71,6 +75,7 @@ impl SendMethodBuilder {
} else { } else {
quote! {} quote! {}
}; };
let extra_form_args = &self.extra_form_args;
util::add_docs( util::add_docs(
&self.description, &self.description,
@ -79,6 +84,7 @@ impl SendMethodBuilder {
let res = #auth_module_path let res = #auth_module_path
.authenticated_client(#url) .authenticated_client(#url)
#form #form
#(#extra_form_args)*
.send() .send()
.await? .await?
#parse_type #parse_type