1use std::{collections::HashMap, ffi::c_void};
6
7use super::{RawObject, RawObjectTrait};
8use crate::{baml_unreachable, error::BamlError, proto::baml_cffi_v1::BamlObjectType};
9
10define_raw_object_wrapper! {
15 HTTPBody => ObjectHttpBody
17}
18
19impl HTTPBody {
20 pub fn text(&self) -> Result<String, BamlError> {
22 self.raw.try_call_method("text", ())
23 }
24
25 pub fn json(&self) -> Result<serde_json::Value, BamlError> {
30 let text: String = self.raw.try_call_method("text", ())?;
31 serde_json::from_str(&text)
32 .map_err(|e| BamlError::internal(format!("failed to parse JSON: {e}")))
33 }
34}
35
36define_raw_object_wrapper! {
41 HTTPRequest => ObjectHttpRequest
43}
44
45impl HTTPRequest {
46 pub fn id(&self) -> String {
48 self.raw.call_method("id", ())
49 }
50
51 pub fn url(&self) -> String {
53 self.raw.call_method("url", ())
54 }
55
56 pub fn method(&self) -> String {
58 self.raw.call_method("method", ())
59 }
60
61 pub fn headers(&self) -> HashMap<String, String> {
63 self.raw.call_method("headers", ())
64 }
65
66 pub fn body(&self) -> HTTPBody {
68 self.raw
69 .call_method_for_object("body", ())
70 .unwrap_or_else(|e| baml_unreachable!("Failed to get body: {e}"))
71 }
72}
73
74define_raw_object_wrapper! {
79 HTTPResponse => ObjectHttpResponse
81}
82
83impl HTTPResponse {
84 pub fn id(&self) -> String {
86 self.raw.call_method("id", ())
87 }
88
89 pub fn status(&self) -> i64 {
91 self.raw.call_method("status", ())
92 }
93
94 pub fn headers(&self) -> HashMap<String, String> {
96 self.raw.call_method("headers", ())
97 }
98
99 pub fn body(&self) -> HTTPBody {
101 self.raw
102 .call_method_for_object("body", ())
103 .unwrap_or_else(|e| baml_unreachable!("Failed to get body: {e}"))
104 }
105}
106
107define_raw_object_wrapper! {
112 SSEResponse => ObjectSseResponse
114}
115
116impl SSEResponse {
117 pub fn text(&self) -> String {
119 self.raw.call_method("text", ())
120 }
121
122 pub fn json(&self) -> Option<serde_json::Value> {
124 let text: String = self.raw.call_method("text", ());
125 serde_json::from_str(&text).ok()
126 }
127}