Skip to content

Commit

Permalink
first take at form data
Browse files Browse the repository at this point in the history
  • Loading branch information
drahnr committed Apr 11, 2023
1 parent 3a5fe99 commit af28d71
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 37 deletions.
42 changes: 39 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ You'll need to add the following to `Cargo.toml`:
[dependencies]
+futures = "0.3"
+progenitor = { git = "https://github.com/oxidecomputer/progenitor" }
+reqwest = { version = "0.11", features = ["json", "stream"] }
+reqwest = { version = "0.11", features = ["json", "stream", "multipart"] }
+serde = { version = "1.0", features = ["derive"] }
```

Expand Down Expand Up @@ -123,7 +123,7 @@ You'll need to add the following to `Cargo.toml`:
[dependencies]
+futures = "0.3"
+progenitor-client = { git = "https://github.com/oxidecomputer/progenitor" }
+reqwest = { version = "0.11", features = ["json", "stream"] }
+reqwest = { version = "0.11", features = ["json", "stream", "multipart"] }
+serde = { version = "1.0", features = ["derive"] }

[build-dependencies]
Expand Down Expand Up @@ -180,7 +180,7 @@ bytes = "1.3.0"
chrono = { version = "0.4.23", default-features=false, features = ["serde"] }
futures-core = "0.3.25"
percent-encoding = "2.2.0"
reqwest = { version = "0.11.13", default-features=false, features = ["json", "stream"] }
reqwest = { version = "0.11.13", default-features=false, features = ["json", "stream", "multipart"] }
serde = { version = "1.0.152", features = ["derive"] }
serde_urlencoded = "0.7.1"

Expand Down
2 changes: 1 addition & 1 deletion example-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
progenitor-client = { path = "../progenitor-client" }
reqwest = { version = "0.11.16", features = ["json", "stream"] }
reqwest = { version = "0.11.16", features = ["json", "stream", "multipart"] }
base64 = "0.21"
rand = "0.8"
serde = { version = "1.0", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion example-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
progenitor = { path = "../progenitor" }
reqwest = { version = "0.11.16", features = ["json", "stream"] }
reqwest = { version = "0.11.16", features = ["json", "stream", "multipart"] }
schemars = { version = "0.8.12", features = ["uuid1"] }
serde = { version = "1.0", features = ["derive"] }
uuid = { version = "1.3", features = ["serde", "v4"] }
2 changes: 1 addition & 1 deletion progenitor-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "An OpenAPI client generator - client support"
bytes = "1.4.0"
futures-core = "0.3.27"
percent-encoding = "2.2"
reqwest = { version = "0.11.16", default-features = false, features = ["json", "stream"] }
reqwest = { version = "0.11.16", default-features = false, features = ["json", "stream", "multipart"] }
serde = "1.0"
serde_json = "1.0"
serde_urlencoded = "0.7.1"
47 changes: 44 additions & 3 deletions progenitor-client/src/progenitor_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,23 @@ pub fn encode_path(pc: &str) -> String {
}

#[doc(hidden)]
pub trait RequestBuilderExt<E> {
pub trait RequestBuilderExt<E>
where
Self: Sized,
{
fn form_urlencoded<T: Serialize + ?Sized>(
self,
body: &T,
) -> Result<RequestBuilder, Error<E>>;

fn form_from_raw<
S: AsRef<str>,
T: AsRef<[u8]>,
I: Sized + IntoIterator<Item = (S, T)>,
>(
self,
iter: I,
) -> Result<Self, Error<E>>;
}

impl<E> RequestBuilderExt<E> for RequestBuilder {
Expand All @@ -405,8 +417,37 @@ impl<E> RequestBuilderExt<E> for RequestBuilder {
"application/x-www-form-urlencoded",
),
)
.body(serde_urlencoded::to_string(body).map_err(|_| {
Error::InvalidRequest("failed to serialize body".to_string())
.body(serde_urlencoded::to_string(body).map_err(|e| {
Error::InvalidRequest(format!(
"failed to serialize body: {e:?}"
))
})?))
}

fn form_from_raw<
S: AsRef<str>,
T: AsRef<[u8]>,
I: Sized + IntoIterator<Item = (S, T)>,
>(
self,
mut iter: I,
) -> Result<Self, Error<E>> {
use reqwest::multipart::{Form, Part};

let mut form = Form::new();
for (name, value) in iter {
form = form.part(
name.as_ref().to_owned(),
Part::stream(Vec::from(value.as_ref())),
);
}
Ok(self
.header(
reqwest::header::CONTENT_TYPE,
reqwest::header::HeaderValue::from_static(
"multipart/form-data",
),
)
.multipart(form))
}
}
1 change: 1 addition & 0 deletions progenitor-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ readme = "../README.md"
heck = "0.4.1"
getopts = "0.2"
indexmap = "1.9"
itertools = "0.10"
openapiv3 = "1.0.0"
proc-macro2 = "1.0"
quote = "1.0"
Expand Down
Loading

0 comments on commit af28d71

Please sign in to comment.