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

Handle byte streams for both success and error responses #35

Merged
merged 2 commits into from
Mar 15, 2022
Merged
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
32 changes: 15 additions & 17 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ default-members = [

#[patch."https://github.com/oxidecomputer/typify"]
#typify = { path = "../typify/typify" }
#[patch."https://github.com/oxidecomputer/dropshot"]
#dropshot = { path = "../dropshot/dropshot" }
2 changes: 2 additions & 0 deletions progenitor-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ repository = "https://github.com/oxidecomputer/progenitor.git"
description = "An OpenAPI client generator - client support"

[dependencies]
bytes = "1.1.0"
futures-core = "0.3.21"
percent-encoding = "2.1"
reqwest = { version = "0.11", features = ["json"] }
serde = "1.0"
Expand Down
41 changes: 32 additions & 9 deletions progenitor-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// Copyright 2021 Oxide Computer Company
// Copyright 2022 Oxide Computer Company

//! Support code for generated clients.

use std::ops::{Deref, DerefMut};

use bytes::Bytes;
use futures_core::Stream;
use serde::de::DeserializeOwned;

/// Represents a streaming, untyped byte stream for both success and error
/// responses.
pub type ByteStream = Box<dyn Stream<Item = reqwest::Result<Bytes>>>;

/// Success value returned by generated client methods.
pub struct ResponseValue<T> {
inner: T,
Expand Down Expand Up @@ -34,6 +40,19 @@ impl<T: DeserializeOwned> ResponseValue<T> {
}
}

impl ResponseValue<ByteStream> {
#[doc(hidden)]
pub fn stream(response: reqwest::Response) -> Self {
let status = response.status();
let headers = response.headers().clone();
Self {
inner: Box::new(response.bytes_stream()),
status,
headers,
}
}
}

impl ResponseValue<()> {
#[doc(hidden)]
pub fn empty(response: reqwest::Response) -> Self {
Expand Down Expand Up @@ -112,8 +131,7 @@ impl<T: std::fmt::Debug> std::fmt::Debug for ResponseValue<T> {
/// The type parameter may be a struct if there's a single expected error type
/// or an enum if there are multiple valid error types. It can be the unit type
/// if there are no structured returns expected.
#[derive(Debug)]
pub enum Error<E: std::fmt::Debug = ()> {
pub enum Error<E = ()> {
/// A server error either with the data, or with the connection.
CommunicationError(reqwest::Error),

Expand All @@ -129,7 +147,7 @@ pub enum Error<E: std::fmt::Debug = ()> {
UnexpectedResponse(reqwest::Response),
}

impl<E: std::fmt::Debug> Error<E> {
impl<E> Error<E> {
/// Returns the status code, if the error was generated from a response.
pub fn status(&self) -> Option<reqwest::StatusCode> {
match self {
Expand Down Expand Up @@ -162,20 +180,20 @@ impl<E: std::fmt::Debug> Error<E> {
}
}

impl<E: std::fmt::Debug> From<reqwest::Error> for Error<E> {
impl<E> From<reqwest::Error> for Error<E> {
fn from(e: reqwest::Error) -> Self {
Self::CommunicationError(e)
}
}

impl<E: std::fmt::Debug> std::fmt::Display for Error<E> {
impl<E> std::fmt::Display for Error<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::CommunicationError(e) => {
write!(f, "Communication Error {}", e)
}
Error::ErrorResponse(rv) => {
write!(f, "Error Response {:?}", rv)
Error::ErrorResponse(_) => {
write!(f, "Error Response")
}
Error::InvalidResponsePayload(e) => {
write!(f, "Invalid Response Payload {}", e)
Expand All @@ -186,7 +204,12 @@ impl<E: std::fmt::Debug> std::fmt::Display for Error<E> {
}
}
}
impl<E: std::fmt::Debug> std::error::Error for Error<E> {
impl<E> std::fmt::Debug for Error<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
impl<E> std::error::Error for Error<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::CommunicationError(e) => Some(e),
Expand Down
4 changes: 3 additions & 1 deletion progenitor-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ typify = { git = "https://github.com/oxidecomputer/typify" }
unicode-xid = "0.2"

[dev-dependencies]
expectorate = "1.0"
dropshot = { git = "https://github.com/oxidecomputer/dropshot" }
expectorate = "1.0"
http = "0.2.6"
hyper = "0.14.17"
Loading