openai_core/
response_meta.rs1use std::ops::{Deref, DerefMut};
4
5use bytes::Bytes;
6use http::{HeaderMap, StatusCode};
7
8use crate::providers::ProviderKind;
9
10#[derive(Debug, Clone)]
12pub struct ResponseMeta {
13 pub status: StatusCode,
15 pub headers: HeaderMap,
17 pub request_id: Option<String>,
19 pub provider: ProviderKind,
21 pub attempts: usize,
23 pub url: String,
25}
26
27#[derive(Debug, Clone)]
29pub struct ApiResponse<T> {
30 pub data: T,
32 pub meta: ResponseMeta,
34}
35
36impl<T> ApiResponse<T> {
37 pub fn new(data: T, meta: ResponseMeta) -> Self {
39 Self { data, meta }
40 }
41
42 pub fn into_parts(self) -> (T, ResponseMeta) {
44 (self.data, self.meta)
45 }
46}
47
48impl<T> Deref for ApiResponse<T> {
49 type Target = T;
50
51 fn deref(&self) -> &Self::Target {
52 &self.data
53 }
54}
55
56impl<T> DerefMut for ApiResponse<T> {
57 fn deref_mut(&mut self) -> &mut Self::Target {
58 &mut self.data
59 }
60}
61
62pub fn into_http_response(meta: &ResponseMeta, body: Bytes) -> http::Response<Bytes> {
64 let mut response = http::Response::builder().status(meta.status);
65
66 if let Some(headers) = response.headers_mut() {
67 headers.extend(meta.headers.clone());
68 }
69
70 response
71 .body(body)
72 .unwrap_or_else(|_| http::Response::new(Bytes::new()))
73}