use crate::error::Result;
use crate::protocol::route::FetchResponse;
use crate::server::channel::Channel;
use crate::server::channel_owner::{
ChannelOwner, ChannelOwnerImpl, DisposeReason, ParentOrConnection,
};
use crate::server::connection::ConnectionLike;
use serde_json::{Value, json};
use std::any::Any;
use std::sync::Arc;
#[derive(Clone)]
pub struct APIRequestContext {
base: ChannelOwnerImpl,
}
impl APIRequestContext {
pub fn new(
parent: ParentOrConnection,
type_name: String,
guid: Arc<str>,
initializer: Value,
) -> Result<Self> {
Ok(Self {
base: ChannelOwnerImpl::new(parent, type_name, guid, initializer),
})
}
pub(crate) async fn inner_fetch(
&self,
url: &str,
options: Option<InnerFetchOptions>,
) -> Result<FetchResponse> {
let opts = options.unwrap_or_default();
let mut params = json!({
"url": url,
"timeout": opts.timeout.unwrap_or(crate::DEFAULT_TIMEOUT_MS)
});
if let Some(method) = opts.method {
params["method"] = json!(method);
}
if let Some(headers) = opts.headers {
let headers_array: Vec<Value> = headers
.into_iter()
.map(|(name, value)| json!({"name": name, "value": value}))
.collect();
params["headers"] = json!(headers_array);
}
if let Some(post_data) = opts.post_data {
use base64::Engine;
let encoded = base64::engine::general_purpose::STANDARD.encode(post_data.as_bytes());
params["postData"] = json!(encoded);
}
if let Some(post_data_bytes) = opts.post_data_bytes {
use base64::Engine;
let encoded = base64::engine::general_purpose::STANDARD.encode(&post_data_bytes);
params["postData"] = json!(encoded);
}
if let Some(max_redirects) = opts.max_redirects {
params["maxRedirects"] = json!(max_redirects);
}
if let Some(max_retries) = opts.max_retries {
params["maxRetries"] = json!(max_retries);
}
#[derive(serde::Deserialize)]
struct FetchResult {
response: ApiResponseData,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct ApiResponseData {
fetch_uid: String,
#[allow(dead_code)]
url: String,
status: u16,
status_text: String,
headers: Vec<HeaderEntry>,
}
#[derive(serde::Deserialize)]
struct HeaderEntry {
name: String,
value: String,
}
let result: FetchResult = self.base.channel().send("fetch", params).await?;
let body = self.fetch_response_body(&result.response.fetch_uid).await?;
let _ = self.dispose_api_response(&result.response.fetch_uid).await;
Ok(FetchResponse {
status: result.response.status,
status_text: result.response.status_text,
headers: result
.response
.headers
.into_iter()
.map(|h| (h.name, h.value))
.collect(),
body,
})
}
async fn fetch_response_body(&self, fetch_uid: &str) -> Result<Vec<u8>> {
#[derive(serde::Deserialize)]
struct BodyResult {
#[serde(default)]
binary: Option<String>,
}
let result: BodyResult = self
.base
.channel()
.send("fetchResponseBody", json!({ "fetchUid": fetch_uid }))
.await?;
match result.binary {
Some(encoded) if !encoded.is_empty() => {
use base64::Engine;
base64::engine::general_purpose::STANDARD
.decode(&encoded)
.map_err(|e| {
crate::error::Error::ProtocolError(format!(
"Failed to decode response body: {}",
e
))
})
}
_ => Ok(vec![]),
}
}
async fn dispose_api_response(&self, fetch_uid: &str) -> Result<()> {
self.base
.channel()
.send_no_result("disposeAPIResponse", json!({ "fetchUid": fetch_uid }))
.await
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct InnerFetchOptions {
pub method: Option<String>,
pub headers: Option<std::collections::HashMap<String, String>>,
pub post_data: Option<String>,
pub post_data_bytes: Option<Vec<u8>>,
pub max_redirects: Option<u32>,
pub max_retries: Option<u32>,
pub timeout: Option<f64>,
}
impl ChannelOwner for APIRequestContext {
fn guid(&self) -> &str {
self.base.guid()
}
fn type_name(&self) -> &str {
self.base.type_name()
}
fn parent(&self) -> Option<Arc<dyn ChannelOwner>> {
self.base.parent()
}
fn connection(&self) -> Arc<dyn ConnectionLike> {
self.base.connection()
}
fn initializer(&self) -> &Value {
self.base.initializer()
}
fn channel(&self) -> &Channel {
self.base.channel()
}
fn dispose(&self, reason: DisposeReason) {
self.base.dispose(reason)
}
fn adopt(&self, child: Arc<dyn ChannelOwner>) {
self.base.adopt(child)
}
fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>) {
self.base.add_child(guid, child)
}
fn remove_child(&self, guid: &str) {
self.base.remove_child(guid)
}
fn on_event(&self, method: &str, params: Value) {
self.base.on_event(method, params)
}
fn was_collected(&self) -> bool {
self.base.was_collected()
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl std::fmt::Debug for APIRequestContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("APIRequestContext")
.field("guid", &self.guid())
.finish()
}
}