callix 0.1.1

A flexible HTTP client library for calling various AI APIs or somthing with configuration and templating support
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
# Callix


A flexible, configuration-driven HTTP client library for Rust, designed for seamless integration with AI APIs and RESTful services.

## Features


- **Configuration-Driven** - Define providers and endpoints in YAML for easy management
- **Auto Retry** - Built-in retry mechanism with configurable delays and exponential backoff
- **Template Engine** - Dynamic variable substitution in URLs, headers, and request bodies
- **Multi-Provider Support** - Pre-configured for OpenAI, Gemini, Claude, and more
- **Type-Safe** - Full Rust type safety with serde integration
- **Zero Config** - Works out of the box with default configurations for popular AI services
- **Async/Await** - Built on Tokio and Reqwest for high-performance async operations

## Table of Contents


- [Installation]#installation
- [Quick Start]#quick-start
- [Configuration]#configuration
- [Examples]#examples
- [Documentation]#documentation
- [Contributing]#contributing

## Installation


Add Callix to your `Cargo.toml`:

```toml
[dependencies]
callix = "0.1.0"
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
```

### Step-by-Step Setup


1. **Create a new Rust project**:
   ```bash
   cargo new my_callix_project

   cd my_callix_project

   ```

2. **Add dependencies** using `cargo add` (Rust 1.62+):
   ```bash
   cargo add callix

   cargo add tokio --features full

   cargo add serde_json

   ```

   Or manually in `Cargo.toml`:
   ```toml
   [dependencies]
   callix = "0.1.0"
   tokio = { version = "1", features = ["full"] }
   serde_json = "1.0"
   ```

3. **Write your first code** in `src/main.rs`:
   ```rust
   use callix::CallixBuilder;
   use std::time::Duration;

   #[tokio::main]
   async fn main() -> Result<(), Box<dyn std::error::Error>> {
       let callix = CallixBuilder::new()
           .timeout(Duration::from_secs(60))
           .build()?;

       println!("Callix is ready!");
       Ok(())
   }
   ```

4. **Run your project**:
   ```bash
   cargo run

   ```

### Optional: Environment Variables Setup


For secure API key management, create a `.env` file:

```bash
# .env

OPENAI_API_KEY=sk-your-key-here
GEMINI_API_KEY=your-key-here
ANTHROPIC_API_KEY=sk-ant-your-key-here
```

Add the `dotenv` crate:
```bash
cargo add dotenv
```

Load environment variables in your code:
```rust
use dotenv::dotenv;
use std::env;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    dotenv().ok();
    let api_key = env::var("OPENAI_API_KEY")?;
    // Use api_key...
    Ok(())
}
```

## Quick Start


### Basic Usage


```rust
use callix::CallixBuilder;
use serde_json::json;
use std::time::Duration;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a client
    let callix = CallixBuilder::new()
        .timeout(Duration::from_secs(60))
        .retries(3)
        .retry_delay(Duration::from_secs(1))
        .build()?;

    // Make a request to Gemini
    let response = callix
        .request("gemini", "generate")?
        .var("API_KEY", "your-api-key")
        .var("model", "gemini-2.0-flash-exp")
        .var("prompt", "Hello, world!")
        .send()
        .await?;

    // Handle the response
    if response.is_success() {
        let body = response.text().await?;
        println!("Response: {}", body);
    }

    Ok(())
}
```

### OpenAI Example


```rust
use serde_json::json;

let response = callix
    .request("openai", "chat")?
    .var("API_KEY", "sk-...")
    .var("model", "gpt-4")
    .var("messages", json!([
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain Rust ownership in simple terms"}
    ]))
    .send()
    .await?;

let json: serde_json::Value = response.json().await?;
println!("{}", json["choices"][0]["message"]["content"]);
```

### Anthropic Claude Example


```rust
let response = callix
    .request("anthropic", "messages")?
    .var("API_KEY", "sk-ant-...")
    .var("model", "claude-3-5-sonnet-20241022")
    .var("max_tokens", 1024)
    .var("messages", json!([
        {"role": "user", "content": "Explain quantum computing"}
    ]))
    .send()
    .await?;
```

## Configuration


### Default Configuration


Callix includes pre-configured settings for popular AI providers:

- **OpenAI** - GPT-4, GPT-3.5 Turbo
- **Google Gemini** - Gemini Pro, Gemini Flash
- **Anthropic Claude** - Claude 3.5 Sonnet, Opus, Haiku

### Custom Configuration


Create a `config.yaml` file to define your own API endpoints:

```yaml
providers:
  my_api:
    base_url: "https://api.example.com"
    headers:
      Authorization: "Bearer {{API_KEY}}"
      Content-Type: "application/json"
    timeout: 30  # seconds (optional)
    endpoints:
      predict:
        path: "/v1/predict"
        method: "POST"
        body_template: |
          {
            "input": "{{text}}",
            "model": "{{model}}",
            "temperature": {{temperature}}
          }
        query_params:
          version: "{{api_version}}"
```

Use your custom configuration:

```rust
let callix = CallixBuilder::new()
    .config("config.yaml")
    .build()?;

let response = callix
    .request("my_api", "predict")?
    .var("API_KEY", "secret")
    .var("text", "Hello")
    .var("model", "v2")
    .var("temperature", 0.7)
    .var("api_version", "latest")
    .send()
    .await?;
```

## Examples


### Custom Headers


```rust
let response = callix
    .request("openai", "chat")?
    .var("API_KEY", "sk-...")
    .var("model", "gpt-4")
    .var("messages", json!([
        {"role": "user", "content": "Hello"}
    ]))
    .header("X-Custom-Header", "value")
    .header("X-Request-ID", "12345")
    .send()
    .await?;
```

### Retry Configuration


```rust
let callix = CallixBuilder::new()
    .retries(5)
    .retry_delay(Duration::from_secs(2))
    .timeout(Duration::from_secs(120))
    .build()?;
```

### Error Handling


```rust
match response.status() {
    200..=299 => {
        let json: serde_json::Value = response.json().await?;
        println!("Success: {:#?}", json);
    }
    400 => println!("Bad Request - check your input"),
    401 => println!("Unauthorized - verify your API key"),
    429 => println!("Rate Limited - please retry later"),
    500..=599 => println!("Server Error - try again"),
    _ => println!("Unexpected status: {}", response.status()),
}
```

### Batch Processing


```rust
let prompts = vec![
    "Explain machine learning",
    "What is quantum computing?",
    "Describe neural networks"
];

for prompt in prompts {
    let response = callix
        .request("openai", "chat")?
        .var("API_KEY", api_key)
        .var("model", "gpt-3.5-turbo")
        .var("messages", json!([
            {"role": "user", "content": prompt}
        ]))
        .send()
        .await?;

    // Process response
    let json: serde_json::Value = response.json().await?;
    println!("{}: {}", prompt, json["choices"][0]["message"]["content"]);

    // Rate limiting
    tokio::time::sleep(Duration::from_millis(500)).await;
}
```

### Concurrent Requests


```rust
use futures::future::join_all;

let prompts = vec!["Prompt 1", "Prompt 2", "Prompt 3"];

let futures: Vec<_> = prompts.iter().map(|&prompt| {
    let callix = callix.clone();
    let api_key = api_key.clone();
    async move {
        callix
            .request("openai", "chat")?
            .var("API_KEY", api_key)
            .var("model", "gpt-3.5-turbo")
            .var("messages", json!([
                {"role": "user", "content": prompt}
            ]))
            .send()
            .await
    }
}).collect();

let results = join_all(futures).await;
```

## Documentation


- **[Full API Documentation]https://docs.rs/callix** - Complete API reference
- **[Wiki & Advanced Guide]README.wiki.md** - Comprehensive guide with advanced usage
- **[Examples Directory]examples/** - Working code examples
  - [`openai.rs`]examples/openai.rs - OpenAI ChatGPT integration
  - [`gemini.rs`]examples/gemini.rs - Google Gemini API
  - [`anthropic.rs`]examples/anthropic.rs - Anthropic Claude API

### Running Examples


```bash
cargo run --example openai
cargo run --example gemini
cargo run --example anthropic
```

## Feature Flags


Customize Callix with feature flags:

```toml
[dependencies]
callix = { version = "0.1", features = ["rustls-tls", "gzip"] }
```

### Available Features


| Feature | Description | Default |
|---------|-------------|---------|
| `native-tls` | Use system's native TLS ||
| `rustls-tls` | Use Rustls (pure Rust TLS) ||
| `blocking` | Blocking HTTP client support ||
| `cookies` | Cookie store support ||
| `gzip` | Gzip compression ||
| `brotli` | Brotli compression ||
| `stream` | Streaming response support ||

## Architecture


### Component Overview


```
CallixBuilder → Callix → RequestBuilder → HTTP Request → CallixResponse
                  ↓           ↓
               Config    TemplateEngine
```

### Core Modules


- **`client`** - Main client implementation and HTTP method parsing
- **`config`** - Configuration management and provider definitions
- **`request`** - Request building and execution with retry logic
- **`response`** - Response handling and parsing utilities
- **`template`** - Variable substitution and template rendering
- **`error`** - Comprehensive error types and conversions

## Minimum Supported Rust Version (MSRV)


Callix requires **Rust 1.75** or higher.

## Contributing


Contributions are welcome! Here's how to get started:

1. Fork the repository
2. Create your 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. Open a Pull Request

### Development Setup


```bash
git clone https://github.com/naseridev/callix.git
cd callix
cargo build
cargo test
```

### Guidelines


- Follow Rust naming conventions and idioms
- Add tests for new features
- Update documentation
- Run `cargo fmt` and `cargo clippy` before committing

## Security


- **Never hardcode API keys** - Use environment variables
- **Validate all user input** - Sanitize before sending requests
- **Use HTTPS** - All default configurations use secure connections
- **Rotate keys regularly** - Follow security best practices

## Acknowledgments


Built with these amazing Rust crates:

- [**reqwest**]https://github.com/seanmonstar/reqwest - High-performance HTTP client
- [**serde**]https://github.com/serde-rs/serde - Serialization framework
- [**tokio**]https://github.com/tokio-rs/tokio - Async runtime
- [**serde_yaml**]https://github.com/dtolnay/serde-yaml - YAML configuration parsing

## Support


- **GitHub Issues**: [Report bugs or request features]https://github.com/naseridev/callix/issues
- **Discussions**: [Ask questions or share ideas]https://github.com/naseridev/callix/discussions
- **Documentation**: [Read the docs]https://docs.rs/callix