diff options
author | Cyborus <cyborus@cyborus.xyz> | 2024-10-23 19:53:43 +0200 |
---|---|---|
committer | Cyborus <cyborus@cyborus.xyz> | 2024-10-23 19:57:00 +0200 |
commit | 0d93867d7b0ad393b32bacb607ff35984c128159 (patch) | |
tree | 9a5907e2193161614a97acb8ed76702e1cc24213 | |
parent | feat: update to forgejo 9 (diff) | |
download | forgejo-api-0d93867d7b0ad393b32bacb607ff35984c128159.tar.xz forgejo-api-0d93867d7b0ad393b32bacb607ff35984c128159.zip |
feat: support optional formData parameters
-rw-r--r-- | generator/src/methods.rs | 30 | ||||
-rw-r--r-- | src/generated/methods.rs | 83 |
2 files changed, 68 insertions, 45 deletions
diff --git a/generator/src/methods.rs b/generator/src/methods.rs index 1f3f697..ffed912 100644 --- a/generator/src/methods.rs +++ b/generator/src/methods.rs @@ -174,10 +174,14 @@ fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> { args.push_str(": "); args.push_str(&ty); } - ParameterIn::FormData { param: _ } => { + ParameterIn::FormData { param } => { args.push_str(", "); args.push_str(&crate::sanitize_ident(&full_param.name)); - args.push_str(": Vec<u8>"); + if param.required { + args.push_str(": Vec<u8>"); + } else { + args.push_str(": Option<Vec<u8>>"); + } } } } @@ -360,6 +364,7 @@ fn create_method_request( let mut has_query = false; // let mut has_headers = false; let mut body_method = String::new(); + let mut form_methods = String::new(); if let Some(params) = &op.parameters { for param in params { let param = param.deref(spec)?; @@ -378,8 +383,15 @@ fn create_method_request( body_method = format!(".json(&{name})"); } } - ParameterIn::FormData { param: _ } => { - body_method += &format!(".multipart(reqwest::multipart::Form::new().part(\"attachment\", reqwest::multipart::Part::bytes({name}).file_name(\"file\").mime_str(\"*/*\").unwrap()))"); + ParameterIn::FormData { param } => { + if param.required { + form_methods += &format!("let builder = builder.multipart(reqwest::multipart::Form::new().part(\"attachment\", reqwest::multipart::Part::bytes({name}).file_name(\"file\").mime_str(\"*/*\").unwrap()));"); + } else { + form_methods += &format!("let builder = match {name} {{ + Some({name}) => builder.multipart(reqwest::multipart::Form::new().part(\"attachment\", reqwest::multipart::Part::bytes({name}).file_name(\"file\").mime_str(\"*/*\").unwrap())), + None => builder, + }};"); + } } } } @@ -394,7 +406,15 @@ fn create_method_request( format!("\"{fmt_str}\"") }; - let out = format!("let request = self.{method}({path_arg}){body_method}.build()?;"); + let out = if form_methods.is_empty() { + format!("let request = self.{method}({path_arg}){body_method}.build()?;") + } else { + format!( + "let builder = self.{method}({path_arg}){body_method}; + {form_methods}\ + let request = builder.build()?;" + ) + }; Ok(out) } diff --git a/src/generated/methods.rs b/src/generated/methods.rs index 29be892..bb14bdf 100644 --- a/src/generated/methods.rs +++ b/src/generated/methods.rs @@ -428,7 +428,7 @@ impl crate::Forgejo { /// Deletes a quota rule /// /// - `quotarule`: quota rule to delete - pub async fn admin_d_elete_quota_rule(&self, quotarule: &str) -> Result<(), ForgejoError> { + pub async fn admin_delete_quota_rule(&self, quotarule: &str) -> Result<(), ForgejoError> { let request = self .delete(&format!("admin/quota/rules/{quotarule}")) .build()?; @@ -3621,20 +3621,19 @@ impl crate::Forgejo { attachment: Vec<u8>, query: IssueCreateIssueCommentAttachmentQuery, ) -> Result<Attachment, ForgejoError> { - let request = self - .post(&format!( - "repos/{owner}/{repo}/issues/comments/{id}/assets?{query}" - )) - .multipart( - reqwest::multipart::Form::new().part( - "attachment", - reqwest::multipart::Part::bytes(attachment) - .file_name("file") - .mime_str("*/*") - .unwrap(), - ), - ) - .build()?; + let builder = self.post(&format!( + "repos/{owner}/{repo}/issues/comments/{id}/assets?{query}" + )); + let builder = builder.multipart( + reqwest::multipart::Form::new().part( + "attachment", + reqwest::multipart::Part::bytes(attachment) + .file_name("file") + .mime_str("*/*") + .unwrap(), + ), + ); + let request = builder.build()?; let response = self.execute(request).await?; match response.status().as_u16() { 201 => Ok(response.json().await?), @@ -3916,20 +3915,19 @@ impl crate::Forgejo { attachment: Vec<u8>, query: IssueCreateIssueAttachmentQuery, ) -> Result<Attachment, ForgejoError> { - let request = self - .post(&format!( - "repos/{owner}/{repo}/issues/{index}/assets?{query}" - )) - .multipart( - reqwest::multipart::Form::new().part( - "attachment", - reqwest::multipart::Part::bytes(attachment) - .file_name("file") - .mime_str("*/*") - .unwrap(), - ), - ) - .build()?; + let builder = self.post(&format!( + "repos/{owner}/{repo}/issues/{index}/assets?{query}" + )); + let builder = builder.multipart( + reqwest::multipart::Form::new().part( + "attachment", + reqwest::multipart::Part::bytes(attachment) + .file_name("file") + .mime_str("*/*") + .unwrap(), + ), + ); + let request = builder.build()?; let response = self.execute(request).await?; match response.status().as_u16() { 201 => Ok(response.json().await?), @@ -6184,15 +6182,15 @@ impl crate::Forgejo { owner: &str, repo: &str, id: u64, - attachment: Vec<u8>, - external_url: Vec<u8>, + attachment: Option<Vec<u8>>, + external_url: Option<Vec<u8>>, query: RepoCreateReleaseAttachmentQuery, ) -> Result<Attachment, ForgejoError> { - let request = self - .post(&format!( - "repos/{owner}/{repo}/releases/{id}/assets?{query}" - )) - .multipart( + let builder = self.post(&format!( + "repos/{owner}/{repo}/releases/{id}/assets?{query}" + )); + let builder = match attachment { + Some(attachment) => builder.multipart( reqwest::multipart::Form::new().part( "attachment", reqwest::multipart::Part::bytes(attachment) @@ -6200,8 +6198,11 @@ impl crate::Forgejo { .mime_str("*/*") .unwrap(), ), - ) - .multipart( + ), + None => builder, + }; + let builder = match external_url { + Some(external_url) => builder.multipart( reqwest::multipart::Form::new().part( "attachment", reqwest::multipart::Part::bytes(external_url) @@ -6209,8 +6210,10 @@ impl crate::Forgejo { .mime_str("*/*") .unwrap(), ), - ) - .build()?; + ), + None => builder, + }; + let request = builder.build()?; let response = self.execute(request).await?; match response.status().as_u16() { 201 => Ok(response.json().await?), |