openai-openapi-http 0.6.0

Client for OpenAI API generated from OpenAPI spec
Documentation
mod event_stream;
mod json;
mod send;

mod __combinators {
    pub(crate) use crate::event_stream::EventStream;
    pub(crate) use crate::json::Json;
    pub(crate) use crate::send::Send;
}

use bytes::Bytes;
pub use event_stream::EventStream;
pub use generated::*;
use openai_openapi_types as __types;
use std::fmt;

#[derive(Debug, thiserror::Error)]
pub enum Error<C, B> {
    #[error(transparent)]
    Client(C),
    #[error(transparent)]
    Body(B),
    #[error(transparent)]
    Http(#[from] http::Error),
    #[error(transparent)]
    Json(#[from] serde_json::Error),
    #[error(transparent)]
    Urlencode(#[from] serde_urlencoded::ser::Error),
    #[error(transparent)]
    Utf8(#[from] std::str::Utf8Error),
    #[error(transparent)]
    ContentType(#[from] ContentTypeError),
    #[error(transparent)]
    EventStream(#[from] EventStreamError),
    #[error(transparent)]
    Status(#[from] StatusError),
}

impl<C, B> Error<C, B> {
    pub fn boxed(self) -> Box<dyn std::error::Error + Send + Sync + 'static>
    where
        C: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
        B: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
    {
        match self {
            Self::Client(e) => e.into(),
            Self::Body(e) => e.into(),
            Self::Http(e) => e.into(),
            Self::Json(e) => e.into(),
            Self::Urlencode(e) => e.into(),
            Self::Utf8(e) => e.into(),
            Self::ContentType(e) => e.into(),
            Self::EventStream(e) => e.into(),
            Self::Status(e) => e.into(),
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub struct ContentTypeError(pub http::Response<Bytes>);

impl fmt::Display for ContentTypeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "invalid content-type: {:?}", send::content_type(&self.0))
    }
}

#[derive(Debug, thiserror::Error)]
#[error("error in event stream: {0:?}")]
pub struct EventStreamError(pub String);

#[derive(Debug, thiserror::Error)]
pub struct StatusError(pub http::Response<Bytes>);

impl fmt::Display for StatusError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "invalid status: {}", self.0.status())
    }
}

#[cfg(test)]
mod tests;

macro_rules! future {
    ($ident:ident, $fut:ty, $output:ty) => {
        #[pin_project::pin_project]
        pub struct $ident<Fut, B, E>(
            #[pin]
            #[allow(clippy::type_complexity)]
            $fut,
        )
        where
            Fut: Future<Output = Result<http::Response<B>, E>>,
            B: http_body::Body;

        impl<Fut, B, E> Future for $ident<Fut, B, E>
        where
            Fut: Future<Output = Result<http::Response<B>, E>>,
            B: http_body::Body,
        {
            type Output = Result<$output, crate::Error<E, B::Error>>;
            fn poll(
                self: std::pin::Pin<&mut Self>,
                cx: &mut std::task::Context<'_>,
            ) -> std::task::Poll<Self::Output> {
                self.project().0.poll(cx)
            }
        }
    };
}
mod generated;