statbook 0.0.2

A Rust library for accessing sports statistics and data (early development)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# Statbook

[![CI](https://github.com/daguenette/statbook/workflows/CI/badge.svg)](https://github.com/daguenette/statbook/actions)
[![Crates.io](https://img.shields.io/crates/v/statbook.svg)](https://crates.io/crates/statbook)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/License-MIT%20OR%20Apache--2.0-blue.svg)](https://github.com/daguenette/statbook#license)
[![Rust Version](https://img.shields.io/badge/rust-1.70+-blue.svg)](https://www.rust-lang.org)

(Early in development)
<!--toc:start-->
- [Statbook]#statbook
  - [Features]#features
  - [Setup]#setup
    - [1. Get API Credentials]#1-get-api-credentials
    - [2. Add to Cargo.toml]#2-add-to-cargotoml
    - [3. Set Environment Variables]#3-set-environment-variables
  - [Quick Start]#quick-start
  - [Configuration Options]#configuration-options
    - [Environment Variables (Recommended)]#environment-variables-recommended
    - [Custom Configuration with Builder Pattern]#custom-configuration-with-builder-pattern
    - [Direct Configuration]#direct-configuration
  - [API Reference]#api-reference
    - [Core Functions]#core-functions
    - [Data Types]#data-types
    - [Fetch Strategies]#fetch-strategies
  - [Error Handling]#error-handling
    - [Partial Failure Handling]#partial-failure-handling
  - [Testing]#testing
    - [Unit Testing with Mock Providers]#unit-testing-with-mock-providers
    - [Custom Mock Data]#custom-mock-data
    - [Integration Testing]#integration-testing
    - [Running Tests]#running-tests
  - [Advanced Usage]#advanced-usage
    - [Custom Providers]#custom-providers
  - [Features]#features
  - [Future Plans]#future-plans
  - [License]#license
<!--toc:end-->

A high-performance Rust library for accessing sports statistics and news data
with concurrent API calls, comprehensive error handling, and flexible
configuration options.

**Perfect for fantasy sports apps, sports analytics, news aggregation,
and data-driven sports applications.**

Currently supports NFL player data via [MySportsFeeds.com](https://www.mysportsfeeds.com)
and news data via [NewsAPI.org](https://newsapi.org) with plans
to expand to other sports and data sources.

## Features

- **Flexible configuration** with builder pattern and environment variables
- **Multiple fetch strategies** (stats-only, news-only, or both)
- **Comprehensive error handling** with detailed error types
- **Built-in testing utilities** with mock providers
- **Extensible architecture** with trait-based providers
- **Concurrent API calls** for improved performance
- **Clean API** with intuitive imports and type safety

## Setup

### 1. Get API Credentials

1. Sign up for a free account at [MySportsFeeds.com]https://www.mysportsfeeds.com
   to get your stats API key
2. Sign up for a free account at [NewsAPI.org]https://newsapi.org
   to get your news API key

### 2. Add to Cargo.toml

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

### 3. Set Environment Variables

```bash
export STATS_API_KEY="your-mysportsfeeds-api-key"
export NEWS_API_KEY="your-newsapi-key"
```

## Quick Start

```rust
use statbook::{StatbookClient, FetchStrategy, api::players::get_player_summary};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create client from environment variables
    let client = StatbookClient::from_env()?;

    // Fetch player data with concurrent API calls
    let result = get_player_summary(&client, "josh-allen", 
        FetchStrategy::Both { fail_on_news_error: false }).await?;

    let stats = &result.player_stats;
    println!("{} {} - {} #{}", 
        stats.first_name, 
        stats.last_name, 
        stats.primary_position,
        stats.jersey_number
    );
    println!("Team: {} | Games: {}", stats.current_team, stats.games_played);

    // Handle news with graceful error handling
    match result.news_result {
        Ok(articles) => {
            println!("Recent News ({} articles):", articles.len());
            for article in articles.iter().take(2) {
                println!("  • {}", article.title);
            }
        }
        Err(e) => println!("News unavailable: {}", e),
    }

    Ok(())
}
```

## Configuration Options

### Environment Variables (Recommended)

```rust
use statbook::StatbookClient;

let client = StatbookClient::from_env()?;
```

### Custom Configuration with Builder Pattern

```rust
use statbook::{StatbookClient, StatbookConfig, NewsConfig, SortBy};

// Custom news settings
let news_config = NewsConfig::new()
    .with_max_articles(15)
    .with_days_back(30)
    .with_sort_by(SortBy::Relevancy);

let config = StatbookConfig::builder()
    .stats_api_key("your-mysportsfeeds-api-key")
    .news_api_key("your-newsapi-key")
    .news_config(news_config)
    .build()?;

let client = StatbookClient::new(config);
```

### Direct Configuration

```rust
use statbook::{StatbookClient, StatbookConfig};

let config = StatbookConfig::new(
    "your-mysportsfeeds-api-key".to_string(), 
    "your-newsapi-key".to_string()
);
let client = StatbookClient::new(config);
```

## API Reference

### Core Functions

```rust
use statbook::{
    StatbookClient, FetchStrategy, NewsQuery,
    api::players::{get_player_stats, get_player_news, get_player_summary}
};

// Get only player statistics (fastest - single API call)
let stats = get_player_stats(&client, "josh-allen").await?;
println!("{} plays {} for {}", stats.first_name, stats.primary_position, stats.current_team);

// Get only news articles with custom query
let query = NewsQuery::for_player("josh-allen")
    .with_page_size(10)
    .with_date_range("2024-01-01".to_string());
let news = get_player_news(&client, &query).await?;

// Get both with concurrent fetching (recommended)
let result = get_player_summary(&client, "josh-allen", 
    FetchStrategy::Both { fail_on_news_error: false }).await?;
```

### Data Types

```rust
use statbook::{PlayerSummary, PlayerStats, Article, NewsQuery, FetchStrategy};

// PlayerSummary - Complete player information
// PlayerStats - Just the statistics
// Article - News article with title, description, content, published_at
// NewsQuery - Configurable news search parameters
// FetchStrategy - Control how data is fetched (StatsOnly, NewsOnly, Both)
```

### Fetch Strategies

Choose the right strategy for your use case:

```rust
use statbook::{FetchStrategy, api::players::get_player_summary};

// Stats only - fastest option (single API call)
let result = get_player_summary(&client, "josh-allen", FetchStrategy::StatsOnly).await?;
// Use case: Live scoreboards, quick player lookups

// News only - for news aggregation
let result = get_player_summary(&client, "josh-allen", FetchStrategy::NewsOnly).await?;
// Use case: Sports news apps, content aggregation

// Both with graceful degradation (recommended)
let result = get_player_summary(&client, "josh-allen", 
    FetchStrategy::Both { fail_on_news_error: false }).await?;
// Use case: Fantasy apps, comprehensive player profiles

// Both with strict error handling
let result = get_player_summary(&client, "josh-allen", 
    FetchStrategy::Both { fail_on_news_error: true }).await?;
// Use case: Critical applications requiring complete data
```

**Performance Comparison:**

- `StatsOnly`: ~200ms (1 API call)
- `NewsOnly`: ~300ms (1 API call)  
- `Both`: ~400ms (2 concurrent API calls)

## Error Handling

The library provides comprehensive error types with detailed context:

```rust
use statbook::{StatbookClient, StatbookError, api::players::get_player_stats};

match get_player_stats(&client, "unknown-player").await {
    Ok(stats) => {
        println!("Found: {} {}", stats.first_name, stats.last_name);
    }
    Err(StatbookError::PlayerNotFound { name }) => {
        println!("No player named '{}'", name);
        // Suggest similar names, check spelling, etc.
    }
    Err(StatbookError::Network(e)) => {
        println!("Network error: {}", e);
        // Retry logic, check connectivity
    }
    Err(StatbookError::StatsApi { status, message }) => {
        match status {
            401 => println!("Invalid API key: {}", message),
            429 => println!("Rate limited: {}", message),
            _ => println!("Stats API error {}: {}", status, message),
        }
    }
    Err(StatbookError::NewsApi { status, message }) => {
        println!("News API error {}: {}", status, message);
    }
    Err(StatbookError::MissingApiKey { key }) => {
        println!("Missing API key: {}. Set environment variable.", key);
    }
    Err(StatbookError::Config(msg)) => {
        println!("Configuration error: {}", msg);
    }
    Err(StatbookError::Validation(msg)) => {
        println!("Validation error: {}", msg);
    }
    Err(e) => println!("Unexpected error: {}", e),
}
```

### Partial Failure Handling

```rust
use statbook::{FetchStrategy, api::players::get_player_summary};

let result = get_player_summary(&client, "josh-allen", 
    FetchStrategy::Both { fail_on_news_error: false }).await?;

println!("Player: {} {}", result.player_stats.first_name, result.player_stats.last_name);

match result.news_result {
    Ok(articles) => println!("Found {} news articles", articles.len()),
    Err(e) => println!("News failed but stats succeeded: {}", e),
}
```

## Testing

The library provides comprehensive testing utilities
for both unit and integration testing:

### Unit Testing with Mock Providers

```rust
use statbook::{create_mock_client, api::players::get_player_stats};

#[tokio::test]
async fn test_player_stats() {
    // Mock client - no real API calls, instant responses
    let client = create_mock_client();
    let stats = get_player_stats(&client, "josh-allen").await.unwrap();

    assert_eq!(stats.first_name, "Josh");
    assert_eq!(stats.last_name, "Allen");
    assert_eq!(stats.primary_position, "QB");
    assert_eq!(stats.current_team, "BUF");
}

#[tokio::test]
async fn test_fetch_strategies() {
    let client = create_mock_client();

    // Test different strategies
    let stats_only = get_player_summary(&client, "josh-allen", FetchStrategy::StatsOnly).await.unwrap();
    let both = get_player_summary(&client, "josh-allen", 
        FetchStrategy::Both { fail_on_news_error: false }).await.unwrap();

    assert_eq!(stats_only.player_stats.first_name, both.player_stats.first_name);
    assert!(both.news_result.is_ok());
}
```

### Custom Mock Data

```rust
use statbook::{create_custom_mock_client, MockStatsProvider, MockNewsProvider, PlayerStats};

#[tokio::test]
async fn test_custom_data() {
    let mut mock_stats = MockStatsProvider::new();
    mock_stats.add_player_stats("custom-player", PlayerStats {
        first_name: "Custom".to_string(),
        last_name: "Player".to_string(),
        // ... other fields
    });

    let client = create_custom_mock_client(mock_stats, MockNewsProvider::new());
    // Test with your custom data
}
```

### Integration Testing

```rust
use statbook::{skip_if_no_credentials, api::players::get_player_stats};

#[tokio::test]
async fn test_real_api() {
    // Skip test if API credentials not available
    let client = match skip_if_no_credentials() {
        Some(client) => client,
        None => {
            println!("Skipping integration test - no API credentials");
            return;
        }
    };
    
    // Test with real API calls
    let stats = get_player_stats(&client, "josh-allen").await.unwrap();
    assert!(!stats.first_name.is_empty());
    assert_eq!(stats.first_name, "Josh");
}
```

### Running Tests

```bash
# Unit tests only (fast, no API calls)
cargo test

# Integration tests (requires API keys)
STATS_API_KEY="your-key" NEWS_API_KEY="your-key" INTEGRATION_TESTS=1 cargo test

# Run specific test
cargo test test_player_stats
```

## Advanced Usage

### Custom Providers

Implement your own data sources:

```rust
use statbook::{StatsProvider, NewsProvider, PlayerStats, Article, NewsQuery, Result, StatbookClient};
use async_trait::async_trait;
use std::sync::Arc;

struct MyCustomStatsProvider;

#[async_trait]
impl StatsProvider for MyCustomStatsProvider {
    async fn fetch_player_stats(&self, name: &str) -> Result<PlayerStats> {
        // Your custom implementation - fetch from your own API, database, etc.
        Ok(PlayerStats {
            first_name: "Custom".to_string(),
            last_name: "Player".to_string(),
            primary_position: "QB".to_string(),
            jersey_number: 1,
            current_team: "CUSTOM".to_string(),
            injury: String::new(),
            rookie: false,
            games_played: 16,
        })
    }
}

struct MyCustomNewsProvider;

#[async_trait]
impl NewsProvider for MyCustomNewsProvider {
    async fn fetch_player_news(&self, query: &NewsQuery) -> Result<Vec<Article>> {
        // Your custom news implementation
        Ok(vec![Article {
            title: format!("Custom news about {}", query.player_name),
            description: "Custom news description".to_string(),
            published_at: "2024-01-01T00:00:00Z".to_string(),
            content: "Custom news content".to_string(),
        }])
    }
}

// Use your custom providers
let client = StatbookClient::with_providers(
    Arc::new(MyCustomStatsProvider),
    Arc::new(MyCustomNewsProvider),
);

// Or mix custom with mock providers for testing
let client = StatbookClient::with_providers(
    Arc::new(MyCustomStatsProvider),
    Arc::new(statbook::MockNewsProvider::new()),
);
```

## Future Plans

- **Caching layer** for improved performance and reduced API calls
- **Enhanced NFL data** (team statistics, game data, season analytics)
- **Advanced news filtering** (sentiment analysis, relevance scoring)
- **Additional sports** (NHL, NBA, MLB, etc.)
- **More data providers** (ESPN, The Athletic, etc.)
- **Real-time updates** via WebSocket connections
- **Data export** (JSON, CSV, database integration)

## License

Licensed under either of

- Apache License, Version 2.0
- MIT license

at your option.