Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build form data request body manually #63

Merged
merged 9 commits into from
Apr 5, 2024
46 changes: 38 additions & 8 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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",
nkzou marked this conversation as resolved.
Show resolved Hide resolved
) {
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 %}
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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 <alex@alexcrichton.com>, Josh Triplett <josh@joshtriplett.org>"
fnv,https://github.com/servo/rust-fnv,Apache-2.0 OR MIT,Alex Crichton <alex@alexcrichton.com>
foreign-types,https://github.com/sfackler/foreign-types,MIT OR Apache-2.0,Steven Fackler <sfackler@gmail.com>
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
Expand Down
23 changes: 15 additions & 8 deletions src/datadogV1/api/api_organizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
46 changes: 30 additions & 16 deletions src/datadogV2/api/api_api_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 15 additions & 8 deletions src/datadogV2/api/api_organizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading