pub struct HttpClient {
pub http_client: Client,
pub api_base: String,
pub api_key: Option<String>,
pub user_agent: Option<String>,
pub x_title: Option<String>,
pub referer: Option<String>,
}Expand description
HTTP client for making requests to the ObjectiveAI API.
Handles authentication, request building, and response parsing for both unary and streaming endpoints.
§Example
let client = HttpClient::new(
reqwest::Client::new(),
None, // Use default API base
Some("your-api-key"),
None, // user_agent
None, // x_title
None, // referer
);Fields§
§http_client: ClientThe underlying reqwest HTTP client.
api_base: StringBase URL for API requests. Defaults to https://api.objective-ai.io.
api_key: Option<String>API key for authentication. Sent as Bearer token in Authorization header.
user_agent: Option<String>Value for the User-Agent header.
x_title: Option<String>Value for the X-Title header.
referer: Option<String>Value for both Referer and HTTP-Referer headers.
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new(
http_client: Client,
api_base: Option<impl Into<String>>,
api_key: Option<impl Into<String>>,
user_agent: Option<impl Into<String>>,
x_title: Option<impl Into<String>>,
referer: Option<impl Into<String>>,
) -> Self
pub fn new( http_client: Client, api_base: Option<impl Into<String>>, api_key: Option<impl Into<String>>, user_agent: Option<impl Into<String>>, x_title: Option<impl Into<String>>, referer: Option<impl Into<String>>, ) -> Self
Creates a new HTTP client.
§Arguments
http_client- The reqwest client to use for requestsapi_base- Base URL for API requests (defaults tohttps://api.objective-ai.io)api_key- API key for authenticationuser_agent- Optional User-Agent header valuex_title- Optional X-Title header valuereferer- Optional Referer header value
Sourcepub async fn send_unary<T: DeserializeOwned + Send + 'static>(
&self,
method: Method,
path: impl AsRef<str>,
body: Option<impl Serialize>,
) -> Result<T, HttpError>
pub async fn send_unary<T: DeserializeOwned + Send + 'static>( &self, method: Method, path: impl AsRef<str>, body: Option<impl Serialize>, ) -> Result<T, HttpError>
Sends a unary (request-response) API call and deserializes the response.
§Type Parameters
T- The expected response type to deserialize into
§Arguments
method- HTTP method (GET, POST, etc.)path- API endpoint path (will be appended toapi_base)body- Optional request body to serialize as JSON
§Errors
Returns super::HttpError if the request fails, returns a non-success status,
or the response cannot be deserialized.
Sourcepub async fn send_unary_no_response(
&self,
method: Method,
path: impl AsRef<str>,
body: Option<impl Serialize>,
) -> Result<(), HttpError>
pub async fn send_unary_no_response( &self, method: Method, path: impl AsRef<str>, body: Option<impl Serialize>, ) -> Result<(), HttpError>
Sends a unary API call that expects no response body.
Useful for DELETE or other operations that only return a status code.
§Arguments
method- HTTP method (GET, POST, DELETE, etc.)path- API endpoint path (will be appended toapi_base)body- Optional request body to serialize as JSON
§Errors
Returns super::HttpError if the request fails or returns a non-success status.
Sourcepub async fn send_streaming<T: DeserializeOwned + Send + 'static, P: AsRef<str> + Send, B: Serialize + Send>(
&self,
method: Method,
path: P,
body: Option<B>,
) -> Result<impl Stream<Item = Result<T, HttpError>> + Send + 'static + use<T, P, B>, HttpError>
pub async fn send_streaming<T: DeserializeOwned + Send + 'static, P: AsRef<str> + Send, B: Serialize + Send>( &self, method: Method, path: P, body: Option<B>, ) -> Result<impl Stream<Item = Result<T, HttpError>> + Send + 'static + use<T, P, B>, HttpError>
Sends a streaming API call using Server-Sent Events (SSE).
Returns a stream of deserialized chunks. The stream automatically handles:
- SSE
[DONE]messages (filtered out) - Comment lines starting with
:(filtered out) - Empty data lines (filtered out)
- API errors embedded in stream data
§Type Parameters
T- The expected chunk type to deserialize each SSE message into
§Arguments
method- HTTP method (typically POST for streaming)path- API endpoint path (will be appended toapi_base)body- Optional request body to serialize as JSON
§Errors
Returns super::HttpError if the stream cannot be established. Individual
stream items may also contain errors if chunks fail to deserialize.
Trait Implementations§
Source§impl Clone for HttpClient
impl Clone for HttpClient
Source§fn clone(&self) -> HttpClient
fn clone(&self) -> HttpClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more