2025-08-10 02:45:57 +00:00
|
|
|
//! HttpClientBox 🌐 - HTTP通信
|
|
|
|
|
// Nyashの箱システムによるHTTP通信を提供します。
|
|
|
|
|
// 参考: 既存Boxの設計思想
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
|
|
|
|
use std::any::Any;
|
2025-08-10 02:45:57 +00:00
|
|
|
use reqwest::blocking::Client;
|
|
|
|
|
use reqwest::Result;
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
#[derive(Debug)]
|
2025-08-10 02:45:57 +00:00
|
|
|
pub struct HttpClientBox {
|
|
|
|
|
pub client: Client,
|
2025-08-10 03:21:24 +00:00
|
|
|
id: u64,
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HttpClientBox {
|
|
|
|
|
pub fn new() -> Self {
|
2025-08-10 03:21:24 +00:00
|
|
|
static mut COUNTER: u64 = 0;
|
|
|
|
|
let id = unsafe {
|
|
|
|
|
COUNTER += 1;
|
|
|
|
|
COUNTER
|
|
|
|
|
};
|
2025-08-10 02:45:57 +00:00
|
|
|
HttpClientBox {
|
|
|
|
|
client: Client::new(),
|
2025-08-10 03:21:24 +00:00
|
|
|
id,
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
pub fn get(&self, url: &str) -> Result<String> {
|
|
|
|
|
let res = self.client.get(url).send()?.text()?;
|
|
|
|
|
Ok(res)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
|
|
|
|
impl NyashBox for HttpClientBox {
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
// Create a new client instance since Client doesn't implement Clone in a straightforward way
|
|
|
|
|
Box::new(HttpClientBox::new())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
|
|
|
|
StringBox::new(format!("HttpClientBox(id: {})", self.id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
"HttpClientBox"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn box_id(&self) -> u64 {
|
|
|
|
|
self.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
|
|
|
|
if let Some(other_http) = other.as_any().downcast_ref::<HttpClientBox>() {
|
|
|
|
|
BoolBox::new(self.id == other_http.id)
|
|
|
|
|
} else {
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|