#[cfg(feature = "native-http")]
use crate::shared::errors::NetworkError;
use crate::shared::errors::{HttpError, Result, StreamError};
use async_stream::try_stream;
use bytes::Bytes;
use futures::Stream;
use futures::StreamExt;
pub const MAX_DOWNLOAD_SIZE: usize = 50 * 1024 * 1024;
pub const MAX_ERROR_BODY_SIZE: usize = 1024 * 1024;
pub const MAX_SSE_LINE_SIZE: usize = 1024 * 1024;
pub const MODEL_CACHE_TTL_MS: u64 = 5 * 60 * 1000;
pub const DEFAULT_JSON_TIMEOUT_MS: u64 = 30_000;
pub const CHAT_REQUEST_TIMEOUT_MS: u64 = 11 * 60 * 1000;
pub const COEF_CACHE_TTL_MS: u64 = 8 * 1000;
pub fn parse_http_error(status_code: u16, body: &str) -> HttpError {
parse_http_error_with_retry_after(status_code, body, 0)
}
pub fn parse_http_error_with_retry_after(
status_code: u16,
body: &str,
retry_after: i64,
) -> HttpError {
if body.is_empty() {
return HttpError::new(status_code, "", "", retry_after, "");
}
let mut type_ = String::new();
let mut message = String::new();
if let Ok(obj) = serde_json::from_str::<serde_json::Value>(body) {
if let Some(err_obj) = obj.get("error").filter(|v| v.is_object()) {
if let Some(m) = err_obj.get("message").and_then(|v| v.as_str()) {
message = m.to_string();
}
if let Some(t) = err_obj.get("type").and_then(|v| v.as_str()) {
type_ = t.to_string();
}
}
}
HttpError::new(status_code, type_, message, retry_after, body)
}
#[cfg(feature = "native-http")]
pub fn classify_transport(op: &str, url: &str, err: &reqwest::Error) -> NetworkError {
let mut ne = NetworkError::new(op, url, err.to_string());
if err.is_timeout() {
ne.timeout = true;
return ne;
}
if err.is_connect() {
ne.eof = true;
return ne;
}
let msg = err.to_string().to_lowercase();
if msg.contains("connection reset")
|| msg.contains("eof")
|| msg.contains("broken pipe")
|| msg.contains("connection closed")
{
ne.eof = true;
}
ne
}
pub fn parse_stream_error(data: &str) -> StreamError {
let payload: serde_json::Value = match serde_json::from_str(data) {
Ok(v) => v,
Err(_) => {
return StreamError {
raw_error: data.to_string(),
..Default::default()
}
}
};
let mut code = payload
.get("errorCode")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let stage = payload
.get("stage")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let mut message = payload
.get("message")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let retryable = payload
.get("retryable")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let mut raw_error = String::new();
if let Some(err_val) = payload.get("error") {
if let Some(s) = err_val.as_str() {
raw_error = s.to_string();
} else if err_val.is_object() {
raw_error = err_val.to_string();
if message.is_empty() {
if let Some(m) = err_val.get("message").and_then(|v| v.as_str()) {
message = m.to_string();
}
}
if code.is_empty() {
if let Some(t) = err_val.get("type").and_then(|v| v.as_str()) {
code = t.to_string();
}
}
}
}
StreamError {
code,
stage,
user_message: message,
raw_error,
retryable,
}
}
pub fn is_order_success(status: &str) -> bool {
matches!(status, "PAID" | "SUCCESS" | "COMPLETED")
}
pub fn is_order_terminal(status: &str) -> bool {
matches!(
status,
"PAID"
| "SUCCESS"
| "COMPLETED"
| "FAILED"
| "CANCELLED"
| "CLOSED"
| "EXPIRED"
| "REFUNDED"
)
}
#[cfg(feature = "native-http")]
pub fn iter_sse_lines<S>(body: S) -> impl Stream<Item = Result<String>>
where
S: Stream<Item = reqwest::Result<Bytes>>,
{
iter_sse_lines_with_cap(body, MAX_SSE_LINE_SIZE)
}
#[cfg(feature = "native-http")]
pub fn iter_sse_lines_with_cap<S>(
body: S,
max_line_bytes: usize,
) -> impl Stream<Item = Result<String>>
where
S: Stream<Item = reqwest::Result<Bytes>>,
{
iter_sse_lines_result_with_cap(body.map(|v| v.map_err(Into::into)), max_line_bytes)
}
pub fn iter_sse_lines_result<S>(body: S) -> impl Stream<Item = Result<String>>
where
S: Stream<Item = Result<Bytes>>,
{
iter_sse_lines_result_with_cap(body, MAX_SSE_LINE_SIZE)
}
pub fn iter_sse_lines_result_with_cap<S>(
body: S,
max_line_bytes: usize,
) -> impl Stream<Item = Result<String>>
where
S: Stream<Item = Result<Bytes>>,
{
try_stream! {
futures::pin_mut!(body);
let mut buf: Vec<u8> = Vec::new();
while let Some(chunk) = body.next().await {
let chunk = chunk?;
let mut remaining = chunk.as_ref();
while !remaining.is_empty() {
let nl = remaining.iter().position(|&b| b == b'\n');
let end = nl.unwrap_or(remaining.len());
let part = &remaining[..end];
let trailing_cr = part.last().or_else(|| buf.last()) == Some(&b'\r');
let size = buf.len().checked_add(part.len())
.ok_or_else(|| crate::shared::errors::Error::other("SSE line size overflow"))?;
let content_size = size.saturating_sub(usize::from(trailing_cr));
if content_size > max_line_bytes {
Err(crate::shared::errors::Error::other(format!(
"SSE line exceeds {max_line_bytes} bytes"
)))?;
}
buf.extend_from_slice(part);
if let Some(nl) = nl {
if buf.last() == Some(&b'\r') { buf.pop(); }
let line = String::from_utf8(std::mem::take(&mut buf))
.map_err(|_| crate::shared::errors::Error::other("SSE line contains invalid UTF-8"))?;
yield line;
remaining = &remaining[nl + 1..];
} else {
break;
}
}
}
if !buf.is_empty() {
if buf.last() == Some(&b'\r') {
buf.pop();
}
if !buf.is_empty() {
yield String::from_utf8(buf).map_err(|_| crate::shared::errors::Error::other("SSE line contains invalid UTF-8"))?;
}
}
}
}
#[cfg(feature = "native-http")]
pub async fn read_limited<S>(body: S, max_bytes: usize) -> Result<Vec<u8>>
where
S: Stream<Item = reqwest::Result<Bytes>>,
{
read_limited_result(body.map(|v| v.map_err(Into::into)), max_bytes).await
}
pub async fn read_limited_result<S>(body: S, max_bytes: usize) -> Result<Vec<u8>>
where
S: Stream<Item = Result<Bytes>>,
{
futures::pin_mut!(body);
let mut out: Vec<u8> = Vec::new();
while let Some(chunk) = body.next().await {
let chunk = chunk?;
if out.len() + chunk.len() > max_bytes {
let remain = max_bytes - out.len();
if remain > 0 {
out.extend_from_slice(&chunk[..remain]);
}
break;
}
out.extend_from_slice(&chunk);
}
Ok(out)
}
#[cfg(feature = "native-http")]
pub async fn read_limited_text<S>(body: S, max_bytes: usize) -> Result<String>
where
S: Stream<Item = reqwest::Result<Bytes>>,
{
let buf = read_limited(body, max_bytes).await?;
Ok(String::from_utf8_lossy(&buf).into_owned())
}
pub async fn read_limited_text_result<S>(body: S, max_bytes: usize) -> Result<String>
where
S: Stream<Item = Result<Bytes>>,
{
Ok(String::from_utf8_lossy(&read_limited_result(body, max_bytes).await?).into_owned())
}