Use method builder
This commit is contained in:
parent
0684b4681a
commit
6d996f4253
|
@ -8,6 +8,7 @@ pub struct MethodBuilder {
|
||||||
auth_module_path: proc_macro2::TokenStream,
|
auth_module_path: proc_macro2::TokenStream,
|
||||||
return_type: Option<proc_macro2::TokenStream>,
|
return_type: Option<proc_macro2::TokenStream>,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
|
form: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MethodBuilder {
|
impl MethodBuilder {
|
||||||
|
@ -22,6 +23,7 @@ impl MethodBuilder {
|
||||||
auth_module_path,
|
auth_module_path,
|
||||||
return_type: None,
|
return_type: None,
|
||||||
description: None,
|
description: None,
|
||||||
|
form: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +37,11 @@ impl MethodBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_form(mut self) -> Self {
|
||||||
|
self.form = true;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn build(&self) -> proc_macro2::TokenStream {
|
pub fn build(&self) -> proc_macro2::TokenStream {
|
||||||
let method_name = &self.method_name;
|
let method_name = &self.method_name;
|
||||||
let (return_type, parse_type) = match &self.return_type {
|
let (return_type, parse_type) = match &self.return_type {
|
||||||
|
@ -43,13 +50,19 @@ impl MethodBuilder {
|
||||||
};
|
};
|
||||||
let url = &self.url;
|
let url = &self.url;
|
||||||
let auth_module_path = &self.auth_module_path;
|
let auth_module_path = &self.auth_module_path;
|
||||||
|
let form = if self.form {
|
||||||
|
quote! { .multipart(self.form) }
|
||||||
|
} else {
|
||||||
|
quote! {}
|
||||||
|
};
|
||||||
|
|
||||||
util::add_docs(
|
util::add_docs(
|
||||||
&self.description,
|
&self.description,
|
||||||
quote! {
|
quote! {
|
||||||
pub async fn #method_name(&self) -> Result<#return_type> {
|
pub async fn #method_name(self) -> Result<#return_type> {
|
||||||
let res = #auth_module_path
|
let res = #auth_module_path
|
||||||
.authenticated_client(#url)
|
.authenticated_client(#url)
|
||||||
|
#form
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
#parse_type
|
#parse_type
|
||||||
|
|
|
@ -3,7 +3,7 @@ use quote::quote;
|
||||||
|
|
||||||
use crate::{generate::util, parser, types};
|
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(
|
pub fn create_method_with_params(
|
||||||
group: &parser::ApiGroup,
|
group: &parser::ApiGroup,
|
||||||
|
@ -83,9 +83,13 @@ pub fn create_method_with_params(
|
||||||
});
|
});
|
||||||
|
|
||||||
let group_name = util::to_ident(&group.name.to_camel());
|
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) {
|
let send = match create_return_type(group, method) {
|
||||||
Some((return_type_name, return_type)) => {
|
Some((return_type_name, return_type)) => {
|
||||||
|
let send_method = send_builder.return_type(&return_type_name).build();
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl<'a> #parameter_type<'a> {
|
impl<'a> #parameter_type<'a> {
|
||||||
fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self {
|
fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self {
|
||||||
|
@ -96,24 +100,15 @@ pub fn create_method_with_params(
|
||||||
|
|
||||||
#(#optional_params)*
|
#(#optional_params)*
|
||||||
|
|
||||||
pub async fn send(self) -> Result<#return_type_name> {
|
#send_method
|
||||||
let res = self.group
|
|
||||||
.auth
|
|
||||||
.authenticated_client(#url)
|
|
||||||
.multipart(self.form)
|
|
||||||
.send()
|
|
||||||
.await?
|
|
||||||
.json::<#return_type_name>()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#return_type
|
#return_type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
let send_method = send_builder.build();
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl<'a> #parameter_type<'a> {
|
impl<'a> #parameter_type<'a> {
|
||||||
fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self {
|
fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self {
|
||||||
|
@ -124,18 +119,7 @@ pub fn create_method_with_params(
|
||||||
|
|
||||||
#(#optional_params)*
|
#(#optional_params)*
|
||||||
|
|
||||||
pub async fn send(self) -> Result<String> {
|
#send_method
|
||||||
let res = self.group
|
|
||||||
.auth
|
|
||||||
.authenticated_client(#url)
|
|
||||||
.multipart(self.form)
|
|
||||||
.send()
|
|
||||||
.await?
|
|
||||||
.text()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user