add ByteStream::new (#128)

also gussy up progenitor-client docs
This commit is contained in:
Adam Leventhal 2022-07-15 10:42:06 -07:00 committed by GitHub
parent 6ca5670819
commit 5a72e1b8e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 24 deletions

View File

@ -4,32 +4,35 @@
//! Support code for generated clients. //! Support code for generated clients.
use std::{ use std::ops::{Deref, DerefMut};
ops::{Deref, DerefMut},
pin::Pin,
};
use bytes::Bytes; use bytes::Bytes;
use futures_core::Stream; use futures_core::Stream;
use reqwest::RequestBuilder; use reqwest::RequestBuilder;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
/// Represents an untyped byte stream for both success and error responses. type InnerByteStream =
pub struct ByteStream( std::pin::Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send + Sync>>;
Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send + Sync>>,
); /// Untyped byte stream used for both success and error responses.
pub struct ByteStream(InnerByteStream);
impl ByteStream { impl ByteStream {
pub fn into_inner( /// Creates a new ByteStream
self, ///
) -> Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send + Sync>> { /// Useful for generating test fixtures.
pub fn new(inner: InnerByteStream) -> Self {
Self(inner)
}
/// Consumes the [`ByteStream`] and return its inner [`Stream`].
pub fn into_inner(self) -> InnerByteStream {
self.0 self.0
} }
} }
impl Deref for ByteStream { impl Deref for ByteStream {
type Target = type Target = InnerByteStream;
Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send + Sync>>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
@ -42,7 +45,10 @@ impl DerefMut for ByteStream {
} }
} }
/// Success value returned by generated client methods. /// Typed value returned by generated client methods.
///
/// This is used for successful responses and may appear in error responses
/// generated from the server (see [`Error::ErrorResponse`])
pub struct ResponseValue<T> { pub struct ResponseValue<T> {
inner: T, inner: T,
status: reqwest::StatusCode, status: reqwest::StatusCode,
@ -99,7 +105,9 @@ impl ResponseValue<()> {
} }
impl<T> ResponseValue<T> { impl<T> ResponseValue<T> {
/// Create an instance for testing /// Creates a [`ResponseValue`] from the inner type, status, and headers.
///
/// Useful for generating test fixtures.
pub fn new( pub fn new(
inner: T, inner: T,
status: reqwest::StatusCode, status: reqwest::StatusCode,
@ -117,17 +125,18 @@ impl<T> ResponseValue<T> {
self.inner self.inner
} }
/// Get the status from this response. /// Gets the status from this response.
pub fn status(&self) -> reqwest::StatusCode { pub fn status(&self) -> reqwest::StatusCode {
self.status self.status
} }
/// Get the headers from this response. /// Gets the headers from this response.
pub fn headers(&self) -> &reqwest::header::HeaderMap { pub fn headers(&self) -> &reqwest::header::HeaderMap {
&self.headers &self.headers
} }
/// Get the parsed value of the Content-Length header, if present and valid. /// Gets the parsed value of the Content-Length header, if present and
/// valid.
pub fn content_length(&self) -> Option<u64> { pub fn content_length(&self) -> Option<u64> {
self.headers self.headers
.get(reqwest::header::CONTENT_LENGTH)? .get(reqwest::header::CONTENT_LENGTH)?
@ -160,10 +169,8 @@ impl<T> ResponseValue<T> {
} }
impl ResponseValue<ByteStream> { impl ResponseValue<ByteStream> {
/// Take ownership of the stream of bytes underpinning this response /// Consumes the `ResponseValue`, returning the wrapped [`Stream`].
pub fn into_inner_stream( pub fn into_inner_stream(self) -> InnerByteStream {
self,
) -> Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send + Sync>> {
self.into_inner().into_inner() self.into_inner().into_inner()
} }
} }
@ -224,8 +231,10 @@ impl<E> Error<E> {
} }
} }
/// Convert this error into one without a typed body for unified error /// Converts this error into one without a typed body.
/// handling with APIs that distinguish various error response bodies. ///
/// This is useful for unified error handling with APIs that distinguish
/// various error response bodies.
pub fn into_untyped(self) -> Error { pub fn into_untyped(self) -> Error {
match self { match self {
Error::InvalidRequest(s) => Error::InvalidRequest(s), Error::InvalidRequest(s) => Error::InvalidRequest(s),