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

a bunch of feature extensions - WIP #466

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ members = [
#[patch."https://github.com/oxidecomputer/dropshot"]
#dropshot = { path = "../dropshot/dropshot" }

#[patch."https://github.com/oxidecomputer/typify"]
#typify = { path = "../typify/typify" }
[patch."https://github.com/oxidecomputer/typify"]
typify = { git = "https://github.com/drahnr/typify", branch = "main" }
# typify = { path = "../typify/typify" }

#[patch.crates-io]
#serde_tokenstream = { path = "../serde_tokenstream" }
Expand Down
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"
60 changes: 51 additions & 9 deletions progenitor-client/src/progenitor_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

//! Support code for generated clients.

use std::ops::{Deref, DerefMut};
use std::{
borrow::Cow,
ops::{Deref, DerefMut}, fs,
};

use bytes::Bytes;
use futures_core::Stream;
use reqwest::RequestBuilder;
use reqwest::{RequestBuilder, multipart};
use serde::{de::DeserializeOwned, Serialize};

type InnerByteStream =
Expand Down Expand Up @@ -63,10 +66,13 @@ impl<T: DeserializeOwned> ResponseValue<T> {
) -> Result<Self, Error<E>> {
let status = response.status();
let headers = response.headers().clone();
let inner = response
.json()
.await
.map_err(Error::InvalidResponsePayload)?;
let response = response.text().await?;
fs::write("responsefcuk.json", &response).unwrap();
let inner: T = serde_json::from_str(&response).unwrap();
// let inner = response
// .json()
// .await
// .map_err(|e| Error::InvalidResponsePayload(reqwest::Error::from(e)))?;

Ok(Self {
inner,
Expand Down Expand Up @@ -386,11 +392,22 @@ 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>,
I: Sized + IntoIterator<Item = (S, reqwest::multipart::Part)>,
>(
self,
iter: I,
) -> Result<Self, Error<E>>;
}

impl<E> RequestBuilderExt<E> for RequestBuilder {
Expand All @@ -405,8 +422,33 @@ 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>,
I: Sized + IntoIterator<Item = (S, reqwest::multipart::Part)>,
>(
self,
mut iter: I,
) -> Result<Self, Error<E>> {
use reqwest::multipart::{Form, Part};

let mut form = Form::new();
for (name, part) in iter {
form = form.part(
name.as_ref().to_owned(), part,
);
}

dbg!(&form);

Ok(self
.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
4 changes: 4 additions & 0 deletions progenitor-impl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use quote::{format_ident, quote};
use typify::{TypeSpaceImpl, TypeStructPropInfo};

use crate::{
Security,
method::{
OperationParameterKind, OperationParameterType, OperationResponseStatus,
},
Expand Down Expand Up @@ -49,6 +50,8 @@ impl Generator {
) -> Result<TokenStream> {
validate_openapi(spec)?;

let security = Security::from(spec);

// Convert our components dictionary to schemars
let schemas = spec.components.iter().flat_map(|components| {
components.schemas.iter().map(|(name, ref_or_schema)| {
Expand All @@ -74,6 +77,7 @@ impl Generator {
&spec.components,
path,
method,
&security,
path_parameters,
)
})
Expand Down
Loading