diff --git a/.generator/src/generator/templates/api.j2 b/.generator/src/generator/templates/api.j2 index 78dcdf14a..9badd068f 100644 --- a/.generator/src/generator/templates/api.j2 +++ b/.generator/src/generator/templates/api.j2 @@ -395,16 +395,46 @@ impl {{ structName }} { {% if formParameter %} // build form parameters {%- if formParameter.required %} - let mut local_form = reqwest::multipart::Form::new(); - local_form = local_form.part("{{formParameter.name}}", reqwest::multipart::Part::bytes({{formParameter.name}}).file_name("{{formParameter.name}}")); - headers.insert("Content-Type", format!("multipart/form-data; boundary={}", local_form.boundary()).parse().unwrap()); - local_req_builder = local_req_builder.multipart(local_form); + let mut local_form = form_data_builder::FormData::new(Vec::new()); + let cursor = std::io::Cursor::new({{formParameter.name}}); + if let Err(e) = local_form.write_file( + "{{formParameter.name}}", + cursor, + Some("{{formParameter.name}}".as_ref()), + "application/octet-stream", + ) { + return Err(crate::datadog::Error::Io(e)); + }; + headers.insert( + "Content-Type", + local_form.content_type_header().parse().unwrap(), + ); + let form_result = local_form.finish(); + match form_result { + Ok(form) => local_req_builder = local_req_builder.body(form), + Err(e) => return Err(crate::datadog::Error::Io(e)), + }; {%- else %} if let Some({{formParameter.name}}) = {{formParameter.name}} { - let mut local_form = reqwest::multipart::Form::new(); - local_form = local_form.part("{{formParameter.name}}", reqwest::multipart::Part::bytes({{formParameter.name}}).file_name("{{formParameter.name}}")); - headers.insert("Content-Type", format!("multipart/form-data; boundary={}", local_form.boundary()).parse().unwrap()); - local_req_builder = local_req_builder.multipart(local_form); + let mut local_form = form_data_builder::FormData::new(Vec::new()); + let cursor = std::io::Cursor::new({{formParameter.name}}); + if let Err(e) = local_form.write_file( + "{{formParameter.name}}", + cursor, + Some("{{formParameter.name}}".as_ref()), + "application/octet-stream", + ) { + return Err(crate::datadog::Error::Io(e)); + }; + headers.insert( + "Content-Type", + local_form.content_type_header().parse().unwrap(), + ); + let form_result = local_form.finish(); + match form_result { + Ok(form) => local_req_builder = local_req_builder.body(form), + Err(e) => return Err(crate::datadog::Error::Io(e)), + }; }; {%- endif %} {%- endif %} diff --git a/Cargo.toml b/Cargo.toml index ec77933a9..47bc027ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ version = "0.0.1" [dependencies] async-stream = "0.3.5" flate2 = "1.0.28" +form-data-builder = "1.0.1" futures-core = "0.3.30" lazy_static = "1.4.0" log = "0.4.20" diff --git a/LICENSE-3rdparty.csv b/LICENSE-3rdparty.csv index cf1b9dc57..38d930d73 100644 --- a/LICENSE-3rdparty.csv +++ b/LICENSE-3rdparty.csv @@ -24,6 +24,7 @@ fastrand,https://github.com/smol-rs/fastrand,Apache-2.0 OR MIT,Stjepan Glavina < flate2,https://github.com/rust-lang/flate2-rs,MIT OR Apache-2.0,"Alex Crichton , Josh Triplett " fnv,https://github.com/servo/rust-fnv,Apache-2.0 OR MIT,Alex Crichton foreign-types,https://github.com/sfackler/foreign-types,MIT OR Apache-2.0,Steven Fackler +form-data-builder,https://github.com/iliana/form-data-builder,MIT-0,The form-data-builder Authors futures,https://github.com/rust-lang/futures-rs,MIT OR Apache-2.0,The futures Authors futures-channel,https://github.com/rust-lang/futures-rs,MIT OR Apache-2.0,The futures-channel Authors futures-core,https://github.com/rust-lang/futures-rs,MIT OR Apache-2.0,The futures-core Authors diff --git a/src/datadogV1/api/api_organizations.rs b/src/datadogV1/api/api_organizations.rs index e08952ca2..a5bf0c8e7 100644 --- a/src/datadogV1/api/api_organizations.rs +++ b/src/datadogV1/api/api_organizations.rs @@ -846,18 +846,25 @@ impl OrganizationsAPI { }; // build form parameters - let mut local_form = reqwest::multipart::Form::new(); - local_form = local_form.part( + let mut local_form = form_data_builder::FormData::new(Vec::new()); + let cursor = std::io::Cursor::new(idp_file); + if let Err(e) = local_form.write_file( "idp_file", - reqwest::multipart::Part::bytes(idp_file).file_name("idp_file"), - ); + cursor, + Some("idp_file".as_ref()), + "application/octet-stream", + ) { + return Err(crate::datadog::Error::Io(e)); + }; headers.insert( "Content-Type", - format!("multipart/form-data; boundary={}", local_form.boundary()) - .parse() - .unwrap(), + local_form.content_type_header().parse().unwrap(), ); - local_req_builder = local_req_builder.multipart(local_form); + let form_result = local_form.finish(); + match form_result { + Ok(form) => local_req_builder = local_req_builder.body(form), + Err(e) => return Err(crate::datadog::Error::Io(e)), + }; local_req_builder = local_req_builder.headers(headers); let local_req = local_req_builder.build()?; diff --git a/src/datadogV2/api/api_api_management.rs b/src/datadogV2/api/api_api_management.rs index 56a217d60..6b57a4a5d 100644 --- a/src/datadogV2/api/api_api_management.rs +++ b/src/datadogV2/api/api_api_management.rs @@ -242,18 +242,25 @@ impl APIManagementAPI { // build form parameters if let Some(openapi_spec_file) = openapi_spec_file { - let mut local_form = reqwest::multipart::Form::new(); - local_form = local_form.part( + let mut local_form = form_data_builder::FormData::new(Vec::new()); + let cursor = std::io::Cursor::new(openapi_spec_file); + if let Err(e) = local_form.write_file( "openapi_spec_file", - reqwest::multipart::Part::bytes(openapi_spec_file).file_name("openapi_spec_file"), - ); + cursor, + Some("openapi_spec_file".as_ref()), + "application/octet-stream", + ) { + return Err(crate::datadog::Error::Io(e)); + }; headers.insert( "Content-Type", - format!("multipart/form-data; boundary={}", local_form.boundary()) - .parse() - .unwrap(), + local_form.content_type_header().parse().unwrap(), ); - local_req_builder = local_req_builder.multipart(local_form); + let form_result = local_form.finish(); + match form_result { + Ok(form) => local_req_builder = local_req_builder.body(form), + Err(e) => return Err(crate::datadog::Error::Io(e)), + }; }; local_req_builder = local_req_builder.headers(headers); @@ -572,18 +579,25 @@ impl APIManagementAPI { // build form parameters if let Some(openapi_spec_file) = openapi_spec_file { - let mut local_form = reqwest::multipart::Form::new(); - local_form = local_form.part( + let mut local_form = form_data_builder::FormData::new(Vec::new()); + let cursor = std::io::Cursor::new(openapi_spec_file); + if let Err(e) = local_form.write_file( "openapi_spec_file", - reqwest::multipart::Part::bytes(openapi_spec_file).file_name("openapi_spec_file"), - ); + cursor, + Some("openapi_spec_file".as_ref()), + "application/octet-stream", + ) { + return Err(crate::datadog::Error::Io(e)); + }; headers.insert( "Content-Type", - format!("multipart/form-data; boundary={}", local_form.boundary()) - .parse() - .unwrap(), + local_form.content_type_header().parse().unwrap(), ); - local_req_builder = local_req_builder.multipart(local_form); + let form_result = local_form.finish(); + match form_result { + Ok(form) => local_req_builder = local_req_builder.body(form), + Err(e) => return Err(crate::datadog::Error::Io(e)), + }; }; local_req_builder = local_req_builder.headers(headers); diff --git a/src/datadogV2/api/api_organizations.rs b/src/datadogV2/api/api_organizations.rs index 463d8a87a..b117b6f76 100644 --- a/src/datadogV2/api/api_organizations.rs +++ b/src/datadogV2/api/api_organizations.rs @@ -169,18 +169,25 @@ impl OrganizationsAPI { // build form parameters if let Some(idp_file) = idp_file { - let mut local_form = reqwest::multipart::Form::new(); - local_form = local_form.part( + let mut local_form = form_data_builder::FormData::new(Vec::new()); + let cursor = std::io::Cursor::new(idp_file); + if let Err(e) = local_form.write_file( "idp_file", - reqwest::multipart::Part::bytes(idp_file).file_name("idp_file"), - ); + cursor, + Some("idp_file".as_ref()), + "application/octet-stream", + ) { + return Err(crate::datadog::Error::Io(e)); + }; headers.insert( "Content-Type", - format!("multipart/form-data; boundary={}", local_form.boundary()) - .parse() - .unwrap(), + local_form.content_type_header().parse().unwrap(), ); - local_req_builder = local_req_builder.multipart(local_form); + let form_result = local_form.finish(); + match form_result { + Ok(form) => local_req_builder = local_req_builder.body(form), + Err(e) => return Err(crate::datadog::Error::Io(e)), + }; }; local_req_builder = local_req_builder.headers(headers);