reka 0.1.0

Async Rust SDK for the Reka API.
Documentation
use serde::Serialize;
use serde::de::DeserializeOwned;

use crate::Result;
use crate::transport::http::{parse_json_response, parse_sse_response};
use crate::transport::sse;

pub(crate) struct RequestBuilder {
    request: reqwest::RequestBuilder,
    path: String,
}

impl RequestBuilder {
    pub(crate) fn new(request: reqwest::RequestBuilder, path: impl Into<String>) -> Self {
        Self {
            request,
            path: path.into(),
        }
    }

    pub(crate) fn accept(self, accept: &'static str) -> Self {
        Self {
            request: self.request.header(reqwest::header::ACCEPT, accept),
            path: self.path,
        }
    }

    pub(crate) fn query<Q>(self, query: &Q) -> Self
    where
        Q: Serialize + ?Sized,
    {
        Self {
            request: self.request.query(query),
            path: self.path,
        }
    }

    pub(crate) fn json<B>(self, body: &B) -> Self
    where
        B: Serialize + ?Sized,
    {
        Self {
            request: self.request.json(body),
            path: self.path,
        }
    }

    pub(crate) async fn send_json<T>(self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let path = self.path;
        let response = self
            .request
            .send()
            .await
            .map_err(crate::Client::map_transport_error)?;
        parse_json_response(response, path).await
    }

    pub(crate) async fn send_sse(self) -> Result<sse::EventStream> {
        let path = self.path;
        let response = self
            .request
            .send()
            .await
            .map_err(crate::Client::map_transport_error)?;
        parse_sse_response(response, path).await
    }
}