use reqwest::Method;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::client::core::CratestackClient;
use crate::client::decode::decode_sequence_response;
use crate::codec::HttpClientCodec;
use crate::error::{ClientError, HeaderPair};
use crate::runtime::wire::{RuntimeRequestWire, RuntimeResponseWire};
use crate::streaming::pump_streamed_response_typed;
impl<C> CratestackClient<C>
where
C: HttpClientCodec,
{
pub async fn post_list<Input, Output>(
&self,
path: &str,
input: &Input,
headers: &[HeaderPair<'_>],
) -> Result<Vec<Output>, ClientError>
where
Input: Serialize,
Output: DeserializeOwned,
{
let body = self.codec.encode(input)?;
let response = self
.request_raw_with_query_and_accept(
Method::POST,
path,
Some(body),
None,
headers,
Some(self.codec.sequence_accept_header_value()),
)
.await?;
decode_sequence_response(&self.codec, &response)
}
pub async fn post_list_streamed<Input, Output>(
&self,
path: &str,
input: &Input,
headers: &[HeaderPair<'_>],
) -> Result<tokio::sync::mpsc::Receiver<Result<Output, ClientError>>, ClientError>
where
Input: Serialize,
Output: DeserializeOwned + Send + 'static,
{
let body = self.codec.encode(input)?;
let response = self
.request_streamed_with_query_and_accept(
Method::POST,
path,
Some(body),
None,
headers,
self.codec.sequence_accept_header_value(),
)
.await?;
let (tx, rx) = tokio::sync::mpsc::channel(16);
tokio::spawn(pump_streamed_response_typed::<Output, ClientError, _>(
response,
tx,
std::convert::identity,
));
Ok(rx)
}
pub async fn execute_raw_transport(
&self,
request: RuntimeRequestWire,
) -> Result<RuntimeResponseWire, ClientError> {
let method = Method::from_bytes(request.method.as_bytes()).map_err(|error| {
ClientError::BadInput(format!("invalid HTTP method '{}': {error}", request.method))
})?;
let header_pairs = request
.headers
.iter()
.map(|header| (header.name.as_str(), header.value.as_str()))
.collect::<Vec<_>>();
self.request_raw_with_query(
method,
&request.path,
if request.body.is_empty() {
None
} else {
Some(request.body)
},
request.canonical_query.as_deref(),
&header_pairs,
)
.await
}
}