ankiconnect_rs/client/
anki_client.rs

1use crate::client::{CardClient, DeckClient, MediaClient, ModelClient};
2use crate::http::HttpRequestSender;
3use crate::AnkiError;
4use std::sync::Arc;
5
6/// The main client for interacting with Anki via AnkiConnect
7///
8/// This is the primary entry point for the library. It provides access to specialized
9/// clients for different aspects of Anki functionality.
10pub struct AnkiClient {
11    cards_client: CardClient,
12    decks_client: DeckClient,
13    media_client: MediaClient,
14    models_client: ModelClient,
15}
16
17impl AnkiClient {
18    /// Creates a new client with the default connection (localhost:8765)
19    pub fn new() -> Self {
20        Self::with_connection("localhost", 8765)
21    }
22
23    /// Creates a new client with a custom host and port
24    pub fn with_connection(host: &str, port: u16) -> Self {
25        let sender = Arc::new(HttpRequestSender::new(host, port));
26        Self {
27            cards_client: CardClient::new(Arc::clone(&sender)),
28            decks_client: DeckClient::new(Arc::clone(&sender)),
29            media_client: MediaClient::new(Arc::clone(&sender)),
30            models_client: ModelClient::new(sender),
31        }
32    }
33
34    /// Gets the version of the AnkiConnect plugin
35    pub fn version(&self) -> Result<u16, AnkiError> {
36        self.cards_client.get_version()
37    }
38
39    /// Access operations related to cards and notes
40    pub fn cards(&self) -> &CardClient {
41        &self.cards_client
42    }
43
44    /// Access operations related to decks
45    pub fn decks(&self) -> &DeckClient {
46        &self.decks_client
47    }
48
49    /// Access operations related to media files
50    pub fn media(&self) -> &MediaClient {
51        &self.media_client
52    }
53
54    /// Access operations related to note types (models)
55    pub fn models(&self) -> &ModelClient {
56        &self.models_client
57    }
58}
59
60impl Default for AnkiClient {
61    fn default() -> Self {
62        Self::new()
63    }
64}