openai_interface/rest/
mod.rs

1//! REST API client module for OpenAI interface
2//!
3//! This module provides the core HTTP functionality for making requests to OpenAI-compatible APIs.
4//! It includes traits and implementations for both streaming and non-streaming API calls.
5//!
6//! # Overview
7//!
8//! The `rest` module contains:
9//! - [`post`]: HTTP POST request functionality with streaming and non-streaming support
10//! - [`get`]: HTTP GET request functionality with various parameter handling options
11//! - Traits for defining API request behavior
12//! - Error handling for HTTP communication
13//!
14//! # Usage
15//!
16//! The module is designed to be used through the higher-level API modules (`chat`, `completions`,
17//! etc.). However, you can use the traits directly if needed:
18//!
19//! ## POST Requests
20//!
21//! ```rust
22//! use openai_interface::rest::post::{Post, PostNoStream, PostStream};
23//! use openai_interface::errors::OapiError;
24//! use serde::{Serialize, Deserialize};
25//!
26//! use std::str::FromStr;
27//!
28//! #[derive(Serialize)]
29//! struct MyRequest {
30//!     prompt: String,
31//!     stream: bool,
32//! }
33//!
34//! #[derive(Deserialize)]
35//! struct MyResponse {
36//!     // Define the fields of your response here
37//!     id: String,
38//! }
39//!
40//! impl FromStr for MyResponse {
41//!     type Err = OapiError;
42//!
43//!     fn from_str(content: &str) -> Result<Self, Self::Err> {
44//!         let parse_result: Result<Self, _> = serde_json::from_str(content)
45//!             .map_err(|e| OapiError::DeserializationError(e.to_string()));
46//!         parse_result
47//!     }
48//! }
49//!
50//! impl Post for MyRequest {
51//!     fn is_streaming(&self) -> bool {
52//!         self.stream
53//!     }
54//! }
55//!
56//! impl PostNoStream for MyRequest {
57//!     type Response = MyResponse;
58//! }
59//! // or impl PostStream for MyRequest {} for streaming requests
60//! ```
61//!
62//! ## GET Requests
63//!
64//! ```rust
65//! use openai_interface::rest::get::{Get, GetNoStream};
66//! use openai_interface::errors::OapiError;
67//! use serde::Deserialize;
68//!
69//! use std::str::FromStr;
70//!
71//! #[derive(Deserialize)]
72//! struct MyResponse {
73//!     id: String,
74//!     name: String,
75//! }
76//!
77//! impl FromStr for MyResponse {
78//!     type Err = OapiError;
79//!
80//!     fn from_str(content: &str) -> Result<Self, Self::Err> {
81//!         let parse_result: Result<Self, _> = serde_json::from_str(content)
82//!             .map_err(|e| OapiError::DeserializationError(e.to_string()));
83//!         parse_result
84//!     }
85//! }
86//!
87//! // GET request with URL building
88//! struct ComplexRequest {
89//!     resource_id: String,
90//!     limit: Option<u32>,
91//! }
92//!
93//! impl Get for ComplexRequest {
94//!     fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
95//!         let mut url = format!("{}/{}", base_url, self.resource_id);
96//!         if let Some(limit) = self.limit {
97//!             url.push_str(&format!("?limit={}", limit));
98//!         }
99//!         Ok(url)
100//!     }
101//! }
102//!
103//! impl GetNoStream for ComplexRequest {
104//!     type Response = MyResponse;
105//! }
106//! ```
107
108pub mod get;
109pub mod post;