deepseekClient_rs/
http.rs1use reqwest::{Client, Response};
2use std::io::{Error, ErrorKind};
3
4pub async fn post(url: &str, body: String, api_key: &str) -> Result<reqwest::Response, reqwest::Error> {
5 let response = Client::new()
6 .post(url)
7 .header("Authorization", format!("Bearer {}", api_key))
8 .header("Content-Type", "application/json")
9 .body(body)
10 .send()
11 .await;
12 response
13}
14
15pub async fn get(url: &str, api_key: &str) -> Result<reqwest::Response, reqwest::Error> {
16 let response = Client::new()
17 .get(url)
18 .header("Authorization", format!("Bearer {}", api_key))
19 .header("Content-Type", "application/json")
20 .send()
21 .await;
22 response
23}
24
25pub async fn process_response(response: Result<Response, reqwest::Error>) -> Result<String, Error> {
26 let response = response.map_err(|_| Error::new(ErrorKind::Other, "Request error"))?;
27 if response.status().is_success() {
28 response.text().await.map_err(|_| Error::new(ErrorKind::Other, "Failed to read response text"))
29 } else {
30 Err(Error::new(ErrorKind::Other, "Request failed"))
31 }
32}