use std::{collections::HashMap, ffi::c_void};
use super::{RawObject, RawObjectTrait};
use crate::{baml_unreachable, error::BamlError, proto::baml_cffi_v1::BamlObjectType};
define_raw_object_wrapper! {
HTTPBody => ObjectHttpBody
}
impl HTTPBody {
pub fn text(&self) -> Result<String, BamlError> {
self.raw.try_call_method("text", ())
}
pub fn json(&self) -> Result<serde_json::Value, BamlError> {
let text: String = self.raw.try_call_method("text", ())?;
serde_json::from_str(&text)
.map_err(|e| BamlError::internal(format!("failed to parse JSON: {e}")))
}
}
define_raw_object_wrapper! {
HTTPRequest => ObjectHttpRequest
}
impl HTTPRequest {
pub fn id(&self) -> String {
self.raw.call_method("id", ())
}
pub fn url(&self) -> String {
self.raw.call_method("url", ())
}
pub fn method(&self) -> String {
self.raw.call_method("method", ())
}
pub fn headers(&self) -> HashMap<String, String> {
self.raw.call_method("headers", ())
}
pub fn body(&self) -> HTTPBody {
self.raw
.call_method_for_object("body", ())
.unwrap_or_else(|e| baml_unreachable!("Failed to get body: {e}"))
}
}
define_raw_object_wrapper! {
HTTPResponse => ObjectHttpResponse
}
impl HTTPResponse {
pub fn id(&self) -> String {
self.raw.call_method("id", ())
}
pub fn status(&self) -> i64 {
self.raw.call_method("status", ())
}
pub fn headers(&self) -> HashMap<String, String> {
self.raw.call_method("headers", ())
}
pub fn body(&self) -> HTTPBody {
self.raw
.call_method_for_object("body", ())
.unwrap_or_else(|e| baml_unreachable!("Failed to get body: {e}"))
}
}
define_raw_object_wrapper! {
SSEResponse => ObjectSseResponse
}
impl SSEResponse {
pub fn text(&self) -> String {
self.raw.call_method("text", ())
}
pub fn json(&self) -> Option<serde_json::Value> {
let text: String = self.raw.call_method("text", ());
serde_json::from_str(&text).ok()
}
}