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//! fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
55//! Ok(format!("{}/service", base_url))
56//! }
57//! }
58//!
59//! impl PostNoStream for MyRequest {
60//! type Response = MyResponse;
61//! }
62//! // or impl PostStream for MyRequest {} for streaming requests
63//! ```
64//!
65//! ## GET Requests
66//!
67//! ```rust
68//! use openai_interface::rest::get::{Get, GetNoStream};
69//! use openai_interface::errors::OapiError;
70//! use serde::Deserialize;
71//!
72//! use std::str::FromStr;
73//!
74//! #[derive(Deserialize)]
75//! struct MyResponse {
76//! id: String,
77//! name: String,
78//! }
79//!
80//! impl FromStr for MyResponse {
81//! type Err = OapiError;
82//!
83//! fn from_str(content: &str) -> Result<Self, Self::Err> {
84//! let parse_result: Result<Self, _> = serde_json::from_str(content)
85//! .map_err(|e| OapiError::DeserializationError(e.to_string()));
86//! parse_result
87//! }
88//! }
89//!
90//! // GET request with URL building
91//! struct ComplexRequest {
92//! resource_id: String,
93//! limit: Option<u32>,
94//! }
95//!
96//! impl Get for ComplexRequest {
97//! fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
98//! let mut url = format!("{}/{}", base_url, self.resource_id);
99//! if let Some(limit) = self.limit {
100//! url.push_str(&format!("?limit={}", limit));
101//! }
102//! Ok(url)
103//! }
104//! }
105//!
106//! impl GetNoStream for ComplexRequest {
107//! type Response = MyResponse;
108//! }
109//! ```
110
111pub mod delete;
112pub mod get;
113pub mod post;