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};
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
use crate::boxes::map_box::MapBox;
|
2025-08-10 03:21:24 +00:00
|
|
|
use std::any::Any;
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
use std::sync::{Arc, Mutex};
|
2025-08-10 02:45:57 +00:00
|
|
|
use reqwest::blocking::Client;
|
|
|
|
|
use reqwest::Result;
|
|
|
|
|
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
#[derive(Debug, Clone)]
|
2025-08-10 02:45:57 +00:00
|
|
|
pub struct HttpClientBox {
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
client: Arc<Mutex<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 {
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
client: Arc::new(Mutex::new(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> {
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
let client = self.client.lock().unwrap();
|
|
|
|
|
let res = client.get(url).send()?.text()?;
|
2025-08-10 02:45:57 +00:00
|
|
|
Ok(res)
|
|
|
|
|
}
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
|
|
|
|
|
/// HTTP GETリクエスト
|
|
|
|
|
pub fn http_get(&self, url: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
let url_str = url.to_string_box().value;
|
|
|
|
|
match self.get(&url_str) {
|
|
|
|
|
Ok(response) => Box::new(StringBox::new(&response)),
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP GET: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// HTTP POSTリクエスト
|
|
|
|
|
pub fn post(&self, url: Box<dyn NyashBox>, body: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
let url_str = url.to_string_box().value;
|
|
|
|
|
let body_str = body.to_string_box().value;
|
|
|
|
|
|
|
|
|
|
let client = self.client.lock().unwrap();
|
|
|
|
|
match client.post(&url_str).body(body_str).send() {
|
|
|
|
|
Ok(response) => {
|
|
|
|
|
match response.text() {
|
|
|
|
|
Ok(text) => Box::new(StringBox::new(&text)),
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP POST: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// HTTP PUT リクエスト
|
|
|
|
|
pub fn put(&self, url: Box<dyn NyashBox>, body: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
let url_str = url.to_string_box().value;
|
|
|
|
|
let body_str = body.to_string_box().value;
|
|
|
|
|
|
|
|
|
|
let client = self.client.lock().unwrap();
|
|
|
|
|
match client.put(&url_str).body(body_str).send() {
|
|
|
|
|
Ok(response) => {
|
|
|
|
|
match response.text() {
|
|
|
|
|
Ok(text) => Box::new(StringBox::new(&text)),
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP PUT: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// HTTP DELETE リクエスト
|
|
|
|
|
pub fn delete(&self, url: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
let url_str = url.to_string_box().value;
|
|
|
|
|
|
|
|
|
|
let client = self.client.lock().unwrap();
|
|
|
|
|
match client.delete(&url_str).send() {
|
|
|
|
|
Ok(response) => {
|
|
|
|
|
match response.text() {
|
|
|
|
|
Ok(text) => Box::new(StringBox::new(&text)),
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP DELETE: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// ヘッダー付きHTTPリクエスト
|
|
|
|
|
pub fn request(&self, method: Box<dyn NyashBox>, url: Box<dyn NyashBox>, options: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
let method_str = method.to_string_box().value.to_uppercase();
|
|
|
|
|
let url_str = url.to_string_box().value;
|
|
|
|
|
|
|
|
|
|
// optionsはMapBoxと仮定
|
|
|
|
|
if let Some(map_box) = options.as_any().downcast_ref::<MapBox>() {
|
|
|
|
|
let client = self.client.lock().unwrap();
|
|
|
|
|
let mut request = match method_str.as_str() {
|
|
|
|
|
"GET" => client.get(&url_str),
|
|
|
|
|
"POST" => client.post(&url_str),
|
|
|
|
|
"PUT" => client.put(&url_str),
|
|
|
|
|
"DELETE" => client.delete(&url_str),
|
|
|
|
|
_ => return Box::new(StringBox::new(&format!("Unsupported HTTP method: {}", method_str))),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ヘッダー設定
|
|
|
|
|
if let Some(headers_box) = map_box.get(Box::new(StringBox::new("headers"))).as_any().downcast_ref::<MapBox>() {
|
|
|
|
|
let headers_map = headers_box.map.lock().unwrap();
|
|
|
|
|
for (key, value) in headers_map.iter() {
|
|
|
|
|
request = request.header(key, value.to_string_box().value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ボディ設定
|
|
|
|
|
if let Some(body_box) = map_box.get(Box::new(StringBox::new("body"))).as_any().downcast_ref::<StringBox>() {
|
|
|
|
|
request = request.body(body_box.value.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match request.send() {
|
|
|
|
|
Ok(response) => {
|
|
|
|
|
match response.text() {
|
|
|
|
|
Ok(text) => Box::new(StringBox::new(&text)),
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP request: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Box::new(StringBox::new("Error: options must be a MapBox"))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
|
|
|
|
impl NyashBox for HttpClientBox {
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
Box::new(self.clone())
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|