caller 0.2.1

A WebApi Caller
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Caller For Rust

English | [็ฎ€ไฝ“ไธญๆ–‡]https://github.com/lljxww/rust-caller/blob/main/README_CN.md

A flexible, configurable Web API request library built with Rust, supporting multiple API endpoints management through configuration files.

## Features

- ๐Ÿš€ **Async Calls**: Built on Tokio async runtime, supports high concurrency requests
- โšก **Configuration Management**: Define API endpoints through configuration files (JSON/YAML/TOML)
- ๐Ÿ”„ **Hot Reload**: Support for configuration file hot reload, update configuration without restarting
- ๐Ÿ“ฅ **File Download**: Built-in file download functionality with automatic format detection
- ๐Ÿ” **Retry Mechanism**: Automatic retry with exponential backoff for failed requests
- ๐Ÿ” **Authentication Support**: Built-in multiple authentication mechanisms (header, query, basic auth)
- ๐Ÿ“Š **Result Parsing**: Powerful JSON result parsing with deep path access
- ๐Ÿ›ก๏ธ **Error Handling**: Comprehensive error types and error handling mechanisms
- ๐Ÿงช **Test Coverage**: Built-in comprehensive unit and integration tests
- ๐Ÿ—๏ธ **Modular Architecture**: Clear layered architecture, easy to extend and maintain

## Architecture Design

### Project Structure
```
src/
โ”œโ”€โ”€ core/          # Core business logic
โ”‚   โ”œโ”€โ”€ context.rs # Call context management
โ”‚   โ””โ”€โ”€ constants.rs # Constant definitions
โ”œโ”€โ”€ domain/        # Domain models
โ”‚   โ”œโ”€โ”€ api_item.rs      # API item definitions
โ”‚   โ”œโ”€โ”€ api_result.rs    # API response results
โ”‚   โ”œโ”€โ”€ authorization.rs # Authentication related
โ”‚   โ”œโ”€โ”€ caller_config.rs # Call configuration
โ”‚   โ”œโ”€โ”€ download_result.rs # Download result handling
โ”‚   โ”œโ”€โ”€ retry_config.rs  # Retry configuration
โ”‚   โ””โ”€โ”€ service_item.rs  # Service item definitions
โ”œโ”€โ”€ config/        # Configuration management
โ”‚   โ””โ”€โ”€ config_loader.rs # Configuration loader
โ”œโ”€โ”€ infra/         # Infrastructure layer
โ”‚   โ””โ”€โ”€ http.rs     # HTTP client wrapper
โ””โ”€โ”€ shared/        # Shared modules
    โ””โ”€โ”€ error.rs    # Error definitions
```

### Workflow
1. **Configuration Loading**: Load API configuration from file (JSON/YAML/TOML)
2. **Context Management**: Manage call context and middleware
3. **HTTP Call**: Construct and send HTTP requests (with optional retry)
4. **Result Processing**: Parse and format response results

## Quick Start

### Add Dependency

```toml
[dependencies]
caller = "0.2.0"
tokio = { version = "1.0", features = ["full"] }
```

### Basic Usage

```rust
use caller::call;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Simple call
    let result = call("JP.list", None).await?;

    // Parse result
    println!("Status: {}", result.status_code);
    println!("Raw response: {}", result.raw);

    // Get specific field
    if let Some(first_title) = result.get_as_str("0.title") {
        println!("First post title: {}", first_title);
    }

    // Call with parameters
    let params = HashMap::from([
        ("post_id".to_string(), "1".to_string()),
        ("userId".to_string(), "1".to_string()),
    ]);
    let result = call("JP.get", Some(params)).await?;

    Ok(())
}
```

### Call with Retry

```rust
use caller::{call_with_retry, RetryConfig};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create retry configuration
    let retry_config = RetryConfig::new()
        .with_max_retries(3)
        .with_base_delay(Duration::from_millis(500))
        .with_max_delay(Duration::from_secs(30));

    // Call with automatic retry
    let result = call_with_retry("JP.list", None, retry_config).await?;
    
    Ok(())
}
```

### File Download

```rust
use caller::download;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Download file with auto-detected format
    let result = download("api.download", None, None).await?;
    println!("Downloaded {} bytes", result.size_human());
    
    // Save to file
    result.save("./downloads", "myfile")?;
    
    // Download with specified file extension
    let result = download("api.pdf", None, Some("pdf".to_string())).await?;
    result.save("./downloads", "document")?;
    
    Ok(())
}
```

## Configuration File

Create a configuration file to define API endpoints. Supports JSON, YAML, and TOML formats.

### JSON Example (caller.json)
```json
{
  "Authorizations": [
    {
      "Name": "BearerToken",
      "HeaderName": "Authorization",
      "Type": "Bearer",
      "Token": "your-token-here"
    }
  ],
  "ServiceItems": [
    {
      "ApiName": "weibo",
      "BaseUrl": "https://weibo.com/ajax",
      "ApiItems": [
        {
          "Method": "hot",
          "Url": "/side/hotSearch",
          "HttpMethod": "GET",
          "ParamType": "query",
          "Description": "Get Weibo hot search",
          "Timeout": 5000,
          "NeedCache": true,
          "CacheTime": 300
        }
      ]
    },
    {
      "ApiName": "JP",
      "BaseUrl": "https://jsonplaceholder.typicode.com",
      "ApiItems": [
        {
          "Method": "get",
          "Url": "/posts/{post_id}",
          "HttpMethod": "GET",
          "ParamType": "path"
        },
        {
          "Method": "create",
          "Url": "/posts",
          "HttpMethod": "POST",
          "ParamType": "json",
          "ContentType": "application/json",
          "AuthorizationType": "BearerToken"
        }
      ]
    }
  ]
}
```

### Parameter Types

| Parameter Type | Description | Example |
|---------------|-------------|----------|
| `none` | No parameters | `/posts` |
| `query` | Query parameters | `/posts?userId=1&id=1` |
| `path` | Path parameters | `/posts/1` |
| `json` | JSON request body | `{"title": "Hello", "body": "World"}` |
| `path,json` | Path parameters + JSON body | `/posts/1` + `{"title": "Updated"}` |

## API Reference

### Main Types

#### `ApiResult`
```rust
pub struct ApiResult {
    pub status_code: StatusCode,
    pub raw: String,
    pub j_obj: Value,
}
```

#### `DownloadResult`
```rust
pub struct DownloadResult {
    pub status_code: StatusCode,
    pub content: Vec<u8>,
    pub content_type: String,
    pub file_extension: String,
    pub suggested_filename: Option<String>,
}
```

#### `RetryConfig`
```rust
pub struct RetryConfig {
    pub max_retries: u32,
    pub base_delay: Duration,
    pub max_delay: Duration,
    pub retry_status_codes: Vec<u16>,
    pub retry_on_network_error: bool,
}
```

#### `CallerError`
```rust
pub enum CallerError {
    ConfigError(String),
    HttpError(reqwest::Error),
    JsonError(String),
    IoError(String),
    ServiceNotFound(String),
    MethodNotFound(String),
    ParamMissing(String),
    AuthenticationError(String),
}
```

### Core Methods

#### `api_result.get_as_str(key: &str) -> Option<&str>`
Get string value, supports deep path like `"0.title"`

#### `api_result.get_as_i64(key: &str) -> Option<i64>`
Get integer value

#### `api_result.get_as_bool(key: &str) -> Option<bool>`
Get boolean value

#### `api_result.get(key: &str) -> Option<&Value>`
Get native JSON value

#### `download_result.save<P: AsRef<Path>>(directory: P, base_name: &str) -> Result<String, CallerError>`
Save downloaded content to file, returns the filename

#### `download_result.size_human() -> String`
Get human-readable file size (e.g., "1.23 MB")

### Public API Functions

#### `call(method: &str, params: Option<HashMap<String, String>>) -> Result<ApiResult, CallerError>`
```rust
use caller::call;
use std::collections::HashMap;

// Call without parameters
let result = call("JP.list", None).await?;

// Call with parameters
let params = HashMap::from([
    ("post_id".to_string(), "1".to_string()),
]);
let result = call("JP.get", Some(params)).await?;
```

#### `call_with_retry(method: &str, params: Option<HashMap<String, String>>, retry_config: RetryConfig) -> Result<ApiResult, CallerError>`
```rust
use caller::{call_with_retry, RetryConfig};
use std::time::Duration;

let retry_config = RetryConfig::new()
    .with_max_retries(3)
    .with_base_delay(Duration::from_millis(500));

let result = call_with_retry("JP.list", None, retry_config).await?;
```

#### `download(method: &str, params: Option<HashMap<String, String>>, extension: Option<String>) -> Result<DownloadResult, CallerError>`
```rust
use caller::download;

// Download with auto-detected format
let result = download("api.download", None, None).await?;
result.save("./downloads", "file")?;

// Download with specified extension
let result = download("api.pdf", None, Some("pdf".to_string())).await?;
```

### Configuration Management

#### `init_config() -> Result<(), CallerError>`
Initialize configuration by loading from file

#### `reload_config() -> Result<(), CallerError>`
Manually reload configuration from file

#### `watch_config() -> Result<(), CallerError>`
Start watching config file for changes (500ms debounce)

#### `watch_config_with_debounce(debounce: Duration) -> Result<(), CallerError>`
Start watching with custom debounce time

#### `stop_watch_config()`
Stop watching config file

#### `is_watching_config() -> bool`
Check if config is currently being watched

#### `is_config_loaded() -> bool`
Check if configuration is loaded

```rust
use caller::{init_config, watch_config, stop_watch_config, is_watching_config};

// Initialize configuration
init_config()?;

// Start file watching
watch_config()?;

// Check status
if is_watching_config() {
    println!("Configuration file is being watched");
}

// Stop watching
stop_watch_config();
```

## Multi-Format Configuration Support

Caller supports multiple configuration file formats:

### Supported Formats
- **JSON** (`.json`)
- **YAML** (`.yaml`, `.yml`)
- **TOML** (`.toml`)

### Format Conversion

```rust
use caller::config::config_loader::{ConfigLoader, ConfigFormat};

// Convert between formats
ConfigLoader::convert_config("config.json", "config.yaml")?;
ConfigLoader::convert_config("config.yaml", "config.toml")?;

// Explicit format specification
ConfigLoader::convert_config_with_format(
    "config.json",
    "output.txt",
    ConfigFormat::Yaml
)?;
```

See [docs/MULTI_FORMAT_CONFIG.md](docs/MULTI_FORMAT_CONFIG.md) for detailed documentation.

## Testing

Run all tests:

```bash
cargo test
```

Run specific tests:

```bash
cargo test test_call_list_posts
```

## Build and Release

```bash
# Build
cargo build

# Run examples
cargo run --example basic_usage

# Format code
cargo fmt

# Check code
cargo check
```

## Contributing

1. Fork this repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Create a Pull Request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

## Changelog

### v0.2.0
- **Multi-format Configuration Support**: Add support for JSON, YAML, and TOML configuration file formats
- **Configuration Format Conversion**: Add configuration file format conversion capabilities (convert between JSON, YAML, and TOML)
- **File Download**: Add file download functionality with automatic format detection
- **Retry Mechanism**: Add automatic retry with exponential backoff for failed requests
- **Retry Configuration**: Add configurable retry behavior with `RetryConfig`
- **Download Result**: Add comprehensive download result handling with `DownloadResult`
- **Serialization Support**: Add Serialize trait to all configuration structures
- **Complete Examples**: Create complete configuration file examples in all three formats
- **Comprehensive Tests**: Add 16 multi-format configuration test cases
- **Documentation**: Update documentation to explain new features and multi-format support

### v0.1.0
- Initial release
- Support for configuration-based API calls
- Support for multiple HTTP methods and parameter types
- Built-in authentication and caching support
- Comprehensive error handling
- Full test coverage