Skip to content

Commit

Permalink
feat: allow converting body into a vector
Browse files Browse the repository at this point in the history
This is useful to make a get single compatible with get multi.

Signed-off-by: Tiago Castro <tiagolobocastro@gmail.com>
  • Loading branch information
tiagolobocastro committed Jan 26, 2024
1 parent 4b86cc8 commit 5cff577
Showing 1 changed file with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,45 +95,52 @@ impl ApiClient {
{{/apiInfo}}
}

/// Http Response with status and body
/// Http Response with status and body.
#[derive(Debug, Clone)]
pub struct ResponseContent<T> {
pub(crate) status: hyper::StatusCode,
pub(crate) body: T,
}
impl<T> ResponseContent<T> {
/// Get the status code
/// Get the status code.
pub fn status(&self) -> hyper::StatusCode {
self.status
}
/// Get a reference to the body
/// Get a reference to the body.
pub fn body(&self) -> &T {
&self.body
}
/// Convert self into the body
/// Convert self into the body.
pub fn into_body(self) -> T {
self.body
}
/// Convert ResponseContent<T> into ResponseContent<Vec<T>>.
pub fn with_vec_body(self) -> ResponseContent<Vec<T>> {
ResponseContent {
status: self.status,
body: vec![self.body]
}
}
}

/// Http Response with status and body as text (could not be coerced into the expected type)
/// Http Response with status and body as text (could not be coerced into the expected type).
#[derive(Debug, Clone)]
pub struct ResponseContentUnexpected {
pub(crate) status: hyper::StatusCode,
pub(crate) text: String,
}
impl ResponseContentUnexpected {
/// Get the status code
/// Get the status code.
pub fn status(&self) -> hyper::StatusCode {
self.status
}
/// Get a reference to the text
/// Get a reference to the text.
pub fn text(&self) -> &str {
self.text.as_ref()
}
}

/// Error type for all Requests with the various variants
/// Error type for all Requests with the various variants.
#[derive(Debug)]
pub enum Error<T> {
Request(RequestError),
Expand Down Expand Up @@ -166,18 +173,18 @@ impl<T: fmt::Debug> error::Error for Error<T> {
}
}

/// Failed to issue the request
/// Failed to issue the request.
#[derive(Debug)]
pub enum RequestError {
/// Failed to build the http request
/// Failed to build the http request.
BuildRequest(hyper::http::Error),
/// Service Request call returned an error
/// Service Request call returned an error.
Request(BoxedError),
/// Service was not ready to process the request
/// Service was not ready to process the request.
NotReady(BoxedError),
/// Failed to serialize request payload
/// Failed to serialize request payload.
Serde(serde_json::Error),
/// Failed to encode the url path
/// Failed to encode the url path.
SerdeEncoded(serde_urlencoded::ser::Error),
}
impl fmt::Display for RequestError {
Expand All @@ -204,17 +211,17 @@ impl error::Error for RequestError {
}
}

/// Error type for all Requests with the various variants
/// Error type for all Requests with the various variants.
#[derive(Debug)]
pub enum ResponseError<T> {
/// The OpenAPI call returned the "expected" OpenAPI JSON content
/// The OpenAPI call returned the "expected" OpenAPI JSON content.
Expected(ResponseContent<T>),
/// Failed to convert the response payload to bytes
/// Failed to convert the response payload to bytes.
PayloadError {
status: hyper::StatusCode,
error: hyper::Error,
},
/// The OpenAPI call returned an "unexpected" JSON content
/// The OpenAPI call returned an "unexpected" JSON content.
Unexpected(ResponseContentUnexpected),
}
impl<T: fmt::Debug> fmt::Display for ResponseError<T> {
Expand Down

0 comments on commit 5cff577

Please sign in to comment.