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
use crate::{
    indexes,
    documents,
};
use crate::Config;
use crate::error::ServiceError;
use crate::{
    Index, 
    Indexes,

    CreateIndexRequest,
    UpdateIndexRequest,
    DeleteIndexRequest,
};
use crate::{
    Document,
    DocumentRequest,
    DocumentState,
};

pub struct Client {
    pub config: Config,
}

// Client constructor
impl Client {
    pub fn new(config: Config) -> Self {
        Client {
            config
        }
    }
}

// impl [indexes] APIs
impl Client {
    pub async fn get_indexes(&self) -> Result<Indexes, ServiceError> {
        indexes::get_indexes(&self.config).await
    }
    
    pub async fn get_index(&self, uid: &'static str) -> Result<Index, ServiceError> {
        indexes::get_index(&self.config, uid).await
    }
    
    pub async fn create_index(&self, create_index_req: CreateIndexRequest) -> Result<Index, ServiceError> {
        indexes::create_index(&self.config, create_index_req).await
    }

    pub async fn update_index(&self, update_index_req: UpdateIndexRequest) -> Result<Index, ServiceError> {
        indexes::update_index(&self.config, update_index_req).await
    }

    pub async fn delete_index(&self, delete_index_req: DeleteIndexRequest) -> Result<String, ServiceError> {
        indexes::delete_index(&self.config, delete_index_req).await
    }
}

// impl [documents] APIs
impl Client {
    pub async fn get_document<T: Document>(&self, uid: String, did: String) -> Result<T, ServiceError> {
        documents::get_document(&self.config, uid, did).await
    }

    pub async fn get_documents<T: Document>(&self, uid: String, offset: Option<usize>, limit: Option<usize>, attributes: Option<&str>) -> Result<Vec<T>, ServiceError> {
        documents::get_documents::<T>(&self.config, uid, offset, limit, attributes).await
    }

    pub async fn add_or_replace<T: Document>(&self, document_req: DocumentRequest<T>) -> Result<DocumentState, ServiceError> {
        documents::add_or_replace(&self.config, document_req).await
    }
}