Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 13m53s
## HTTP Client Refactoring - Create unified HttpClient module (src/http_client.rs) - Refactor 24 files to use shared HTTP client - Replace .unwrap() with proper error handling - Eliminate code duplication in HTTP requests ## Project Restructuring - Rename package: ai → aibot - Add dual binary support: aibot (main) + ai (compatibility alias) - Migrate config directory: ~/.config/ai/ → ~/.config/syui/ai/bot/ - Implement backward compatibility with automatic migration ## Testing Infrastructure - Add unit tests for HttpClient - Create test infrastructure with cargo-make - Add test commands: test, test-quick, test-verbose ## Documentation - Complete migration guide with step-by-step instructions - Updated development guide with new structure - HTTP client API reference documentation - Comprehensive refactoring summary ## Files Changed - Modified: 24 source files (HTTP client integration) - Added: src/http_client.rs, src/alias.rs, src/tests/ - Added: 5 documentation files in docs/ - Added: migration setup script 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
6.1 KiB
6.1 KiB
ai.bot リファクタリング作業サマリー
概要
2025年6月6日に実施されたai.botプロジェクトのリファクタリング作業の完全な記録です。
実施した作業
1. HTTPクライアントの共通化
- 作成したファイル:
src/http_client.rs
- 対象: 24個のファイルを統合
- 削減: 重複するHTTPリクエストコードを一箇所に集約
HttpClientモジュールの機能
// 認証付きリクエスト
client.get_with_auth(&url).await
client.post_json_with_auth(&url, &json_data).await
client.delete_with_auth(&url).await
// 認証なしリクエスト
client.get(&url).await
client.post_json(&url, &json_data).await
// カスタムヘッダー付きリクエスト
client.post_with_headers(&url, &json_data, headers).await
2. リファクタリング対象ファイル一覧
コアAPIファイル (8個)
src/post.rs
- 投稿作成src/like.rs
- いいね機能src/repost.rs
- リポスト機能src/follow.rs
- フォロー/アンフォローsrc/reply.rs
- 返信機能src/profile.rs
- プロフィール取得src/delete_record.rs
- レコード削除src/describe.rs
- ユーザー説明取得
認証・セッション管理 (3個)
src/session.rs
- セッション管理src/refresh.rs
- トークンリフレッシュsrc/token.rs
- ログイン認証
通知・メンション (3個)
src/mention.rs
- メンション投稿src/notify.rs
- 通知取得src/notify_read.rs
- 通知既読
フィード・タイムライン (4個)
src/feed_get.rs
- フィード取得src/timeline_author.rs
- 作者タイムラインsrc/followers.rs
- フォロワー取得src/follows.rs
- フォロー取得
画像関連 (3個)
src/img.rs
- 画像投稿src/img_reply.rs
- 画像付き返信src/img_upload.rs
- 画像アップロード
リンク・リッチテキスト (3個)
src/post_link.rs
- リンク付き投稿src/reply_link.rs
- リンク付き返信src/reply_og.rs
- OGデータ付き返信
ゲームモジュール (5個)
src/game/post_card.rs
- ゲームカード投稿src/game/post_card_verify.rs
- カード検証src/game/post_game.rs
- ゲームデータ投稿src/game/post_game_login.rs
- ゲームログインsrc/game/post_game_user.rs
- ゲームユーザーデータ
3. 除外したファイル
src/openai.rs
- OpenAI API用(異なる認証方式のため)src/feed_watch.rs
- reqwest使用していないため
4. 共通の変更パターン
Before (変更前)
extern crate reqwest;
use crate::data_refresh;
pub async fn some_request() -> String {
let token = data_refresh(&"access");
let client = reqwest::Client::new();
let res = client
.post(url)
.json(&post)
.header("Authorization", "Bearer ".to_owned() + &token)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
res
}
After (変更後)
use crate::http_client::HttpClient;
pub async fn some_request() -> String {
let client = HttpClient::new();
match client.post_json_with_auth(&url, &post).await {
Ok(response) => response,
Err(e) => {
eprintln!("Error: {}", e);
"err".to_string()
}
}
}
5. テストインフラの追加
作成したテストファイル
src/tests/mod.rs
- テストモジュール宣言src/tests/http_client_tests.rs
- HttpClientのテスト
テストコマンド
# 基本テスト
cargo test
# Makefileを使用したテスト
cargo make test # cleanしてからテスト
cargo make test-quick # 素早いテスト
cargo make test-verbose # 詳細出力
追加した依存関係 (Cargo.toml)
[dev-dependencies]
mockito = "1.2"
tokio-test = "0.4"
改善された点
コード品質
- コード重複の削除: 同じHTTPリクエストパターンの重複を排除
- エラーハンドリング:
.unwrap()
を適切なmatch
文に置換 - 保守性: HTTP関連のロジックが一箇所に集約
開発効率
- 変更容易性: HTTPクライアントの変更が1ファイルで完結
- テスト可能性: ユニットテストの追加でバグ検出が容易
- デバッグ性: エラーメッセージの改善
安定性
- パニック回避:
.unwrap()
によるパニックを防止 - エラー処理: 適切なエラーレスポンスの返却
次のステップ(推奨)
1. bot.rsのリファクタリング (高優先度)
- 500行以上の巨大な関数を分割
- コマンド処理の構造化
- 深いif-else文の改善
2. 設定管理の統一 (中優先度)
- 複数の設定構造体の統合
- 設定ファイルパスの一元管理
3. main.rsの整理 (中優先度)
- コマンド定義の外部ファイル化
- モジュール構造の改善
ファイル構造
src/
├── http_client.rs # 新規作成:共通HTTPクライアント
├── tests/ # 新規作成:テストディレクトリ
│ ├── mod.rs
│ └── http_client_tests.rs
├── main.rs # 更新:http_clientモジュール追加
├── Cargo.toml # 更新:テスト依存関係追加
├── Makefile.toml # 更新:テストコマンド追加
└── [24個のリファクタリング済みファイル]
注意事項
- OpenAI API:
src/openai.rs
は意図的に除外(異なる認証方式) - 後方互換性: 既存のAPI呼び出しは同じ形式を維持
- エラー処理: "err"文字列を返すパターンは既存仕様に合わせて維持
検証方法
# コンパイル確認
cargo check
# テスト実行
cargo test
# フォーマット確認
cargo fmt --check
# 全体ビルド
cargo build
この作業により、ai.botプロジェクトのHTTP通信部分が大幅に改善され、今後の開発・保守が容易になりました。