Skip to content

Commit 6b29797

Browse files
author
daniel.eades
committed
fixup
1 parent 43b0bc6 commit 6b29797

11 files changed

Lines changed: 1132 additions & 640 deletions

Cargo.lock

Lines changed: 147 additions & 617 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ futures = "0.3.31"
3232
futures-core = "0.3.31"
3333
heck = "0.5.0"
3434
http = "1.4.0"
35-
httpmock = "0.7.0"
35+
httpmock = "0.8"
3636
hyper = "1.8.1"
3737
indexmap = "2.13.0"
3838
openapiv3 = "2.2.0"
@@ -63,7 +63,8 @@ uuid = { version = "1.19.0", features = ["serde", "v4"] }
6363
#[patch."https://github.com/oxidecomputer/typify"]
6464
#typify = { path = "../typify/typify" }
6565

66-
#[patch.crates-io]
66+
[patch.crates-io]
67+
httpmock = { git = "https://github.com/danieleades/httpmock", branch = "unused-bound" }
6768
#serde_tokenstream = { path = "../serde_tokenstream" }
6869
#typify = { path = "../typify/typify" }
6970
#rustfmt-wrapper = { path = "../rustfmt-wrapper" }

progenitor-impl/src/httpmock.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -244,21 +244,12 @@ impl Generator {
244244
},
245245
),
246246
OperationParameterKind::Body(body_content_type) => match typ {
247-
OperationParameterType::Type(type_id) => {
248-
let ty = self.type_space.get_type(type_id).unwrap();
249-
let is_string =
250-
matches!(ty.details(), typify::TypeDetails::String);
251-
// String types need `to_owned()` because `str` doesn't
252-
// implement Deserialize and is unsized
253-
let value_expr =
254-
if is_string { quote! { &value.to_owned() } } else { quote! { value } };
255-
(
256-
true,
257-
quote! {
258-
Self(self.0.json_body_obj(#value_expr))
259-
},
260-
)
261-
}
247+
OperationParameterType::Type(_) => (
248+
true,
249+
quote! {
250+
Self(self.0.json_body_obj(value))
251+
},
252+
),
262253
OperationParameterType::RawBody => match body_content_type {
263254
BodyContentType::OctetStream => (
264255
true,
@@ -344,18 +335,13 @@ impl Generator {
344335
crate::method::OperationResponseKind::Type(arg_type_id) => {
345336
let arg_type = self.type_space.get_type(arg_type_id).unwrap();
346337
let arg_type_ident = arg_type.parameter_ident();
347-
let is_string =
348-
matches!(arg_type.details(), typify::TypeDetails::String);
349-
// String types need `to_owned()` because `str` is unsized
350-
let value_expr =
351-
if is_string { quote! { &value.to_owned() } } else { quote! { value } };
352338
(
353339
quote! {
354340
value: #arg_type_ident,
355341
},
356342
quote! {
357343
.header("content-type", "application/json")
358-
.json_body_obj(#value_expr)
344+
.json_body_obj(value)
359345
},
360346
)
361347
}
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
#[allow(unused_imports)]
2+
use progenitor_client::{encode_path, ClientHooks, OperationInfo, RequestBuilderExt};
3+
#[allow(unused_imports)]
4+
pub use progenitor_client::{ByteStream, ClientInfo, Error, ResponseValue};
5+
/// Types used as operation parameters and responses.
6+
#[allow(clippy::all)]
7+
pub mod types {
8+
/// Error types.
9+
pub mod error {
10+
/// Error from a `TryFrom` or `FromStr` implementation.
11+
pub struct ConversionError(::std::borrow::Cow<'static, str>);
12+
impl ::std::error::Error for ConversionError {}
13+
impl ::std::fmt::Display for ConversionError {
14+
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {
15+
::std::fmt::Display::fmt(&self.0, f)
16+
}
17+
}
18+
19+
impl ::std::fmt::Debug for ConversionError {
20+
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {
21+
::std::fmt::Debug::fmt(&self.0, f)
22+
}
23+
}
24+
25+
impl From<&'static str> for ConversionError {
26+
fn from(value: &'static str) -> Self {
27+
Self(value.into())
28+
}
29+
}
30+
31+
impl From<String> for ConversionError {
32+
fn from(value: String) -> Self {
33+
Self(value.into())
34+
}
35+
}
36+
}
37+
}
38+
39+
#[derive(Clone, Debug)]
40+
///Client for httpmock-string-body
41+
///
42+
///Version: 0.0.0
43+
pub struct Client {
44+
pub(crate) baseurl: String,
45+
pub(crate) client: reqwest::Client,
46+
}
47+
48+
impl Client {
49+
/// Create a new client.
50+
///
51+
/// `baseurl` is the base URL provided to the internal
52+
/// `reqwest::Client`, and should include a scheme and hostname,
53+
/// as well as port and a path stem if applicable.
54+
pub fn new(baseurl: &str) -> Self {
55+
#[cfg(not(target_arch = "wasm32"))]
56+
let client = {
57+
let dur = ::std::time::Duration::from_secs(15u64);
58+
reqwest::ClientBuilder::new()
59+
.connect_timeout(dur)
60+
.timeout(dur)
61+
};
62+
#[cfg(target_arch = "wasm32")]
63+
let client = reqwest::ClientBuilder::new();
64+
Self::new_with_client(baseurl, client.build().unwrap())
65+
}
66+
67+
/// Construct a new client with an existing `reqwest::Client`,
68+
/// allowing more control over its configuration.
69+
///
70+
/// `baseurl` is the base URL provided to the internal
71+
/// `reqwest::Client`, and should include a scheme and hostname,
72+
/// as well as port and a path stem if applicable.
73+
pub fn new_with_client(baseurl: &str, client: reqwest::Client) -> Self {
74+
Self {
75+
baseurl: baseurl.to_string(),
76+
client,
77+
}
78+
}
79+
}
80+
81+
impl ClientInfo<()> for Client {
82+
fn api_version() -> &'static str {
83+
"0.0.0"
84+
}
85+
86+
fn baseurl(&self) -> &str {
87+
self.baseurl.as_str()
88+
}
89+
90+
fn client(&self) -> &reqwest::Client {
91+
&self.client
92+
}
93+
94+
fn inner(&self) -> &() {
95+
&()
96+
}
97+
}
98+
99+
impl ClientHooks<()> for &Client {}
100+
impl Client {
101+
///Sends a `GET` request to `/message`
102+
///
103+
///```ignore
104+
/// let response = client.get_message()
105+
/// .send()
106+
/// .await;
107+
/// ```
108+
pub fn get_message(&self) -> builder::GetMessage<'_> {
109+
builder::GetMessage::new(self)
110+
}
111+
112+
///Sends a `POST` request to `/message`
113+
///
114+
///```ignore
115+
/// let response = client.send_message()
116+
/// .body(body)
117+
/// .send()
118+
/// .await;
119+
/// ```
120+
pub fn send_message(&self) -> builder::SendMessage<'_> {
121+
builder::SendMessage::new(self)
122+
}
123+
}
124+
125+
/// Types for composing operation parameters.
126+
#[allow(clippy::all)]
127+
pub mod builder {
128+
use super::types;
129+
#[allow(unused_imports)]
130+
use super::{
131+
encode_path, ByteStream, ClientHooks, ClientInfo, Error, OperationInfo, RequestBuilderExt,
132+
ResponseValue,
133+
};
134+
///Builder for [`Client::get_message`]
135+
///
136+
///[`Client::get_message`]: super::Client::get_message
137+
#[derive(Debug, Clone)]
138+
pub struct GetMessage<'a> {
139+
client: &'a super::Client,
140+
}
141+
142+
impl<'a> GetMessage<'a> {
143+
pub fn new(client: &'a super::Client) -> Self {
144+
Self { client: client }
145+
}
146+
147+
///Sends a `GET` request to `/message`
148+
pub async fn send(self) -> Result<ResponseValue<::std::string::String>, Error<()>> {
149+
let Self { client } = self;
150+
let url = format!("{}/message", client.baseurl,);
151+
let mut header_map = ::reqwest::header::HeaderMap::with_capacity(1usize);
152+
header_map.append(
153+
::reqwest::header::HeaderName::from_static("api-version"),
154+
::reqwest::header::HeaderValue::from_static(super::Client::api_version()),
155+
);
156+
#[allow(unused_mut)]
157+
let mut request = client
158+
.client
159+
.get(url)
160+
.header(
161+
::reqwest::header::ACCEPT,
162+
::reqwest::header::HeaderValue::from_static("application/json"),
163+
)
164+
.headers(header_map)
165+
.build()?;
166+
let info = OperationInfo {
167+
operation_id: "get_message",
168+
};
169+
client.pre(&mut request, &info).await?;
170+
let result = client.exec(request, &info).await;
171+
client.post(&result, &info).await?;
172+
let response = result?;
173+
match response.status().as_u16() {
174+
200u16 => ResponseValue::from_response(response).await,
175+
_ => Err(Error::UnexpectedResponse(response)),
176+
}
177+
}
178+
}
179+
180+
///Builder for [`Client::send_message`]
181+
///
182+
///[`Client::send_message`]: super::Client::send_message
183+
#[derive(Debug, Clone)]
184+
pub struct SendMessage<'a> {
185+
client: &'a super::Client,
186+
body: Result<::std::string::String, String>,
187+
}
188+
189+
impl<'a> SendMessage<'a> {
190+
pub fn new(client: &'a super::Client) -> Self {
191+
Self {
192+
client: client,
193+
body: Err("body was not initialized".to_string()),
194+
}
195+
}
196+
197+
pub fn body<V>(mut self, value: V) -> Self
198+
where
199+
V: std::convert::TryInto<::std::string::String>,
200+
{
201+
self.body = value.try_into().map_err(|_| {
202+
"conversion to `:: std :: string :: String` for body failed".to_string()
203+
});
204+
self
205+
}
206+
207+
///Sends a `POST` request to `/message`
208+
pub async fn send(self) -> Result<ResponseValue<()>, Error<()>> {
209+
let Self { client, body } = self;
210+
let body = body.map_err(Error::InvalidRequest)?;
211+
let url = format!("{}/message", client.baseurl,);
212+
let mut header_map = ::reqwest::header::HeaderMap::with_capacity(1usize);
213+
header_map.append(
214+
::reqwest::header::HeaderName::from_static("api-version"),
215+
::reqwest::header::HeaderValue::from_static(super::Client::api_version()),
216+
);
217+
#[allow(unused_mut)]
218+
let mut request = client
219+
.client
220+
.post(url)
221+
.json(&body)
222+
.headers(header_map)
223+
.build()?;
224+
let info = OperationInfo {
225+
operation_id: "send_message",
226+
};
227+
client.pre(&mut request, &info).await?;
228+
let result = client.exec(request, &info).await;
229+
client.post(&result, &info).await?;
230+
let response = result?;
231+
match response.status().as_u16() {
232+
204u16 => Ok(ResponseValue::empty(response)),
233+
_ => Err(Error::UnexpectedResponse(response)),
234+
}
235+
}
236+
}
237+
}
238+
239+
/// Items consumers will typically use such as the Client.
240+
pub mod prelude {
241+
pub use self::super::Client;
242+
}

0 commit comments

Comments
 (0)