cameo 0.2.0

Unified movie/TV show database SDK for Rust
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! AniList GraphQL client implementation.

use serde::de::DeserializeOwned;
use serde_json::{Value, json};

use super::{
    config::AniListConfig,
    error::AniListError,
    query,
    response::{
        GraphQlResponse, MediaDetailResponse, MediaPageResponse, StaffDetailResponse,
        StaffPageResponse,
    },
};
use crate::{
    core::{config::TimeWindow, pagination::Page},
    unified::{
        conversions::anilist::{anilist_media_to_movie, anilist_media_to_tv},
        models::{
            UnifiedMovie, UnifiedMovieDetails, UnifiedPerson, UnifiedPersonDetails,
            UnifiedSearchResult, UnifiedTvShow, UnifiedTvShowDetails,
        },
    },
};

/// AniList format strings for anime that map to the "movie" media type.
const MOVIE_FORMATS: &[&str] = &["MOVIE"];

/// AniList format strings for anime that map to the "TV show" media type.
const TV_FORMATS: &[&str] = &["TV", "TV_SHORT", "ONA", "OVA", "SPECIAL"];

// ── Client ─────────────────────────────────────────────────────────────────────

/// Low-level AniList GraphQL client.
///
/// Sends typed GraphQL queries to the AniList API and returns deserialized
/// results. No authentication is needed for public data.
///
/// **Rate limit:** AniList enforces 90 requests per minute. This client paces
/// requests with a token-bucket limiter matching that limit by default (see
/// [`AniListConfig::with_rate_limit`](super::config::AniListConfig::with_rate_limit)).
///
/// # Cloning
///
/// `AniListClient` is cheaply cloneable — the underlying `reqwest::Client` is
/// reference-counted, so clones share the same connection pool.
///
/// # Example
///
/// ```no_run
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// use cameo::providers::anilist::{AniListClient, AniListConfig};
/// use cameo::unified::SearchProvider;
///
/// let client = AniListClient::new(AniListConfig::new())?;
/// let results = client.search_movies("Your Name", None).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct AniListClient {
    http: reqwest::Client,
    config: AniListConfig,
    rate_limiter: crate::providers::rate_limit::RateLimiter,
}

impl AniListClient {
    /// Create a new AniList client from the given configuration.
    ///
    /// # Errors
    ///
    /// Returns [`AniListError::InvalidConfig`] if the rate-limit configuration
    /// is invalid, or [`AniListError::Http`] if the underlying HTTP client
    /// cannot be built (e.g. invalid TLS configuration).
    pub fn new(config: AniListConfig) -> Result<Self, AniListError> {
        config
            .rate_limit
            .validate()
            .map_err(AniListError::InvalidConfig)?;
        let http = crate::providers::http::build_client(
            config.connect_timeout,
            config.request_timeout,
            reqwest::header::HeaderMap::new(),
        )?;
        let rate_limiter = crate::providers::rate_limit::RateLimiter::new(config.rate_limit);
        Ok(Self {
            http,
            config,
            rate_limiter,
        })
    }

    /// Returns a reference to the client configuration.
    pub fn config(&self) -> &AniListConfig {
        &self.config
    }

    // ── GraphQL execution engine ───────────────────────────────────────────────

    /// Execute a GraphQL query and deserialize the `data` field.
    ///
    /// The response body is read **before** the HTTP status is inspected, so
    /// GraphQL errors and rate-limit headers are surfaced for every status
    /// (AniList returns `404`/`429` as GraphQL errors on an otherwise-2xx
    /// response, and rate-limit info in `Retry-After`).
    ///
    /// # Partial-data policy
    ///
    /// When a response carries **both** `data` and `errors` (a partial
    /// success), the usable `data` is returned and the errors are logged at
    /// `warn` level rather than discarded — failing would throw away good data.
    /// Errors are only surfaced as [`AniListError::GraphQL`] when no `data` is
    /// present. A GraphQL error carrying `status: 404` always maps to
    /// [`AniListError::NotFound`].
    async fn graphql<T: DeserializeOwned>(
        &self,
        query: &str,
        variables: Value,
    ) -> Result<T, AniListError> {
        let body = json!({
            "query": query,
            "variables": variables,
        });

        let policy = self.config.retry;
        let mut retry_index = 0u32;
        loop {
            // Pace requests (including retries) within the rate limit.
            self.rate_limiter.acquire().await;
            match self.graphql_attempt::<T>(&body).await {
                Ok(data) => return Ok(data),
                Err(err) => {
                    if policy.should_retry(retry_index) && err.is_retryable() {
                        let delay = policy.backoff(retry_index, err.retry_after());
                        tracing::debug!(
                            retry = retry_index,
                            ?delay,
                            error = %err,
                            "anilist: retrying transient error"
                        );
                        tokio::time::sleep(delay).await;
                        retry_index += 1;
                        continue;
                    }
                    return Err(err);
                }
            }
        }
    }

    /// A single GraphQL request attempt (no rate limiting or retrying).
    async fn graphql_attempt<T: DeserializeOwned>(&self, body: &Value) -> Result<T, AniListError> {
        let resp = self
            .http
            .post(&self.config.base_url)
            .header(reqwest::header::CONTENT_TYPE, "application/json")
            .header(reqwest::header::ACCEPT, "application/json")
            .json(body)
            .send()
            .await?;

        // Capture status and rate-limit headers before consuming the body.
        let status = resp.status();
        let retry_after = super::error::parse_retry_after(resp.headers());
        let text = resp.text().await?;

        // Parse the GraphQL envelope regardless of HTTP status.
        let gql_resp: GraphQlResponse<T> = match serde_json::from_str(&text) {
            Ok(parsed) => parsed,
            Err(parse_err) => {
                // The body was not a GraphQL envelope. An error status is the
                // more useful explanation; otherwise it is a real decode error.
                if !status.is_success() {
                    return Err(AniListError::Status {
                        status: status.as_u16(),
                        retry_after,
                        message: truncate_body(&text),
                    });
                }
                return Err(AniListError::Deserialization(parse_err));
            }
        };

        if let Some(errors) = gql_resp.errors.filter(|e| !e.is_empty()) {
            // Derive not-found from the structured `status`, not the message.
            if errors.iter().any(|e| e.status == Some(404)) {
                return Err(AniListError::NotFound);
            }
            // Partial-data policy: prefer usable data over failing.
            if let Some(data) = gql_resp.data {
                tracing::warn!(?errors, "anilist: returning partial data alongside errors");
                return Ok(data);
            }
            tracing::warn!(?errors, "anilist: graphql errors");
            return Err(AniListError::GraphQL {
                errors,
                retry_after,
            });
        }

        if let Some(data) = gql_resp.data {
            return Ok(data);
        }

        // No data and no errors: distinguish an error status from an empty body.
        if !status.is_success() {
            return Err(AniListError::Status {
                status: status.as_u16(),
                retry_after,
                message: truncate_body(&text),
            });
        }
        Err(AniListError::NoData)
    }

    // ── Helper: build JSON array for format_in variable ───────────────────────

    fn format_in_value(formats: &[&str]) -> Value {
        Value::Array(
            formats
                .iter()
                .map(|s| Value::String(s.to_string()))
                .collect(),
        )
    }

    fn page_vars(page: Option<u32>, per_page: u32) -> (i64, i64) {
        (page.unwrap_or(1) as i64, per_page as i64)
    }

    // ── Search ─────────────────────────────────────────────────────────────────

    /// Search for anime movies by title.
    pub async fn search_movies(
        &self,
        query: &str,
        page: Option<u32>,
    ) -> Result<Page<UnifiedMovie>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "query": query,
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(MOVIE_FORMATS),
        });
        tracing::debug!(
            query,
            page = page_num,
            "anilist: graphql SEARCH_ANIME (search_movies)"
        );
        let resp: MediaPageResponse = self.graphql(query::SEARCH_ANIME, vars).await?;
        media_page_to_movies(resp)
    }

    /// Search for anime series (TV, OVA, ONA, Special) by title.
    pub async fn search_tv_shows(
        &self,
        query: &str,
        page: Option<u32>,
    ) -> Result<Page<UnifiedTvShow>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "query": query,
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(TV_FORMATS),
        });
        tracing::debug!(
            query,
            page = page_num,
            "anilist: graphql SEARCH_ANIME (search_tv_shows)"
        );
        let resp: MediaPageResponse = self.graphql(query::SEARCH_ANIME, vars).await?;
        media_page_to_tv(resp)
    }

    /// Search for staff (voice actors, directors, animators, etc.) by name.
    pub async fn search_people(
        &self,
        query: &str,
        page: Option<u32>,
    ) -> Result<Page<UnifiedPerson>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "query": query,
            "page": page_num,
            "perPage": per_page,
        });
        tracing::debug!(query, page = page_num, "anilist: graphql SEARCH_STAFF");
        let resp: StaffPageResponse = self.graphql(query::SEARCH_STAFF, vars).await?;
        let page_data = resp.page.ok_or(AniListError::NoData)?;
        let pi = &page_data.page_info;
        Ok(Page::with_has_next(
            pi.current_page.unwrap_or(1) as u32,
            page_data
                .staff
                .into_iter()
                .map(crate::unified::conversions::anilist::staff_to_person)
                .collect(),
            pi.has_next_page,
            pi.last_page.unwrap_or(1) as u32,
            pi.total.unwrap_or(0) as u32,
        ))
    }

    /// Search across all anime formats. Returns movies and TV shows mixed.
    pub async fn search_multi(
        &self,
        query: &str,
        page: Option<u32>,
    ) -> Result<Page<UnifiedSearchResult>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        // Omit formatIn entirely (do not pass null) so AniList searches all formats.
        let vars = json!({
            "query": query,
            "page": page_num,
            "perPage": per_page,
        });
        tracing::debug!(
            query,
            page = page_num,
            "anilist: graphql SEARCH_ANIME (search_multi)"
        );
        let resp: MediaPageResponse = self.graphql(query::SEARCH_ANIME, vars).await?;
        let page_data = resp.page.ok_or(AniListError::NoData)?;
        let pi = &page_data.page_info;
        let results = page_data
            .media
            .into_iter()
            .filter_map(crate::unified::conversions::anilist::anilist_media_to_search_result)
            .collect();
        Ok(Page::with_has_next(
            pi.current_page.unwrap_or(1) as u32,
            results,
            pi.has_next_page,
            pi.last_page.unwrap_or(1) as u32,
            pi.total.unwrap_or(0) as u32,
        ))
    }

    // ── Details ────────────────────────────────────────────────────────────────

    /// Get full details for an anime movie by AniList ID.
    pub async fn movie_details(&self, id: i32) -> Result<UnifiedMovieDetails, AniListError> {
        let vars = json!({ "id": id });
        tracing::debug!(id, "anilist: graphql MEDIA_DETAILS (movie_details)");
        let resp: MediaDetailResponse = self.graphql(query::MEDIA_DETAILS, vars).await?;
        let media = resp.media.ok_or(AniListError::NotFound)?;
        Ok(crate::unified::conversions::anilist::anilist_media_detail_to_movie_details(media))
    }

    /// Get full details for an anime TV series by AniList ID.
    pub async fn tv_show_details(&self, id: i32) -> Result<UnifiedTvShowDetails, AniListError> {
        let vars = json!({ "id": id });
        tracing::debug!(id, "anilist: graphql MEDIA_DETAILS (tv_show_details)");
        let resp: MediaDetailResponse = self.graphql(query::MEDIA_DETAILS, vars).await?;
        let media = resp.media.ok_or(AniListError::NotFound)?;
        Ok(crate::unified::conversions::anilist::anilist_media_detail_to_tv_details(media))
    }

    /// Get full details for a staff member by AniList ID.
    pub async fn person_details(&self, id: i32) -> Result<UnifiedPersonDetails, AniListError> {
        let vars = json!({ "id": id });
        tracing::debug!(id, "anilist: graphql STAFF_DETAILS (person_details)");
        let resp: StaffDetailResponse = self.graphql(query::STAFF_DETAILS, vars).await?;
        let staff = resp.staff.ok_or(AniListError::NotFound)?;
        Ok(crate::unified::conversions::anilist::staff_detail_to_person_details(staff))
    }

    // ── Discovery ──────────────────────────────────────────────────────────────

    /// Get trending anime movies.
    ///
    /// # Note
    ///
    /// AniList has no time-window concept. The `time_window` argument is
    /// accepted for trait compatibility but is always ignored — AniList
    /// returns a single global trending list regardless of the requested window.
    pub async fn trending_movies(
        &self,
        _time_window: TimeWindow,
        page: Option<u32>,
    ) -> Result<Page<UnifiedMovie>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(MOVIE_FORMATS),
        });
        tracing::debug!(
            page = page_num,
            "anilist: graphql LIST_TRENDING_ANIME (trending_movies)"
        );
        let resp: MediaPageResponse = self.graphql(query::LIST_TRENDING_ANIME, vars).await?;
        media_page_to_movies(resp)
    }

    /// Get trending anime series.
    ///
    /// # Note
    ///
    /// AniList has no time-window concept. The `time_window` argument is
    /// accepted for trait compatibility but is always ignored — AniList
    /// returns a single global trending list regardless of the requested window.
    pub async fn trending_tv(
        &self,
        _time_window: TimeWindow,
        page: Option<u32>,
    ) -> Result<Page<UnifiedTvShow>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(TV_FORMATS),
        });
        tracing::debug!(
            page = page_num,
            "anilist: graphql LIST_TRENDING_ANIME (trending_tv)"
        );
        let resp: MediaPageResponse = self.graphql(query::LIST_TRENDING_ANIME, vars).await?;
        media_page_to_tv(resp)
    }

    /// Get popular anime movies (sorted by popularity).
    pub async fn popular_movies(
        &self,
        page: Option<u32>,
    ) -> Result<Page<UnifiedMovie>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(MOVIE_FORMATS),
        });
        tracing::debug!(
            page = page_num,
            "anilist: graphql LIST_POPULAR_ANIME (popular_movies)"
        );
        let resp: MediaPageResponse = self.graphql(query::LIST_POPULAR_ANIME, vars).await?;
        media_page_to_movies(resp)
    }

    /// Get top-scored anime movies (sorted by average score).
    pub async fn top_rated_movies(
        &self,
        page: Option<u32>,
    ) -> Result<Page<UnifiedMovie>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(MOVIE_FORMATS),
        });
        tracing::debug!(
            page = page_num,
            "anilist: graphql LIST_TOP_SCORED_ANIME (top_rated_movies)"
        );
        let resp: MediaPageResponse = self.graphql(query::LIST_TOP_SCORED_ANIME, vars).await?;
        media_page_to_movies(resp)
    }

    /// Get popular anime TV shows (sorted by popularity).
    pub async fn popular_tv_shows(
        &self,
        page: Option<u32>,
    ) -> Result<Page<UnifiedTvShow>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(TV_FORMATS),
        });
        tracing::debug!(
            page = page_num,
            "anilist: graphql LIST_POPULAR_ANIME (popular_tv_shows)"
        );
        let resp: MediaPageResponse = self.graphql(query::LIST_POPULAR_ANIME, vars).await?;
        media_page_to_tv(resp)
    }

    /// Get top-scored anime TV shows (sorted by average score).
    pub async fn top_rated_tv_shows(
        &self,
        page: Option<u32>,
    ) -> Result<Page<UnifiedTvShow>, AniListError> {
        let (page_num, per_page) = Self::page_vars(page, self.config.per_page);
        let vars = json!({
            "page": page_num,
            "perPage": per_page,
            "formatIn": Self::format_in_value(TV_FORMATS),
        });
        tracing::debug!(
            page = page_num,
            "anilist: graphql LIST_TOP_SCORED_ANIME (top_rated_tv_shows)"
        );
        let resp: MediaPageResponse = self.graphql(query::LIST_TOP_SCORED_ANIME, vars).await?;
        media_page_to_tv(resp)
    }
}

// ── Page conversion helpers ───────────────────────────────────────────────────

/// Truncate a response body to a bounded length for use in an error message.
fn truncate_body(body: &str) -> String {
    const MAX_CHARS: usize = 200;
    let trimmed = body.trim();
    if trimmed.is_empty() {
        return "empty response body".to_string();
    }
    let truncated: String = trimmed.chars().take(MAX_CHARS).collect();
    if truncated.len() < trimmed.len() {
        format!("{truncated}")
    } else {
        truncated
    }
}

fn media_page_to_movies(
    resp: MediaPageResponse,
) -> Result<Page<UnifiedMovie>, super::error::AniListError> {
    let page = resp.page.ok_or(super::error::AniListError::NoData)?;
    let pi = &page.page_info;
    Ok(Page::with_has_next(
        pi.current_page.unwrap_or(1) as u32,
        page.media.into_iter().map(anilist_media_to_movie).collect(),
        pi.has_next_page,
        pi.last_page.unwrap_or(1) as u32,
        pi.total.unwrap_or(0) as u32,
    ))
}

fn media_page_to_tv(
    resp: MediaPageResponse,
) -> Result<Page<UnifiedTvShow>, super::error::AniListError> {
    let page = resp.page.ok_or(super::error::AniListError::NoData)?;
    let pi = &page.page_info;
    Ok(Page::with_has_next(
        pi.current_page.unwrap_or(1) as u32,
        page.media.into_iter().map(anilist_media_to_tv).collect(),
        pi.has_next_page,
        pi.last_page.unwrap_or(1) as u32,
        pi.total.unwrap_or(0) as u32,
    ))
}