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
use std::{env, marker::PhantomData};
use crate::index;
const ALGOLIA_APPLICATION_ID_VARIABLE: &str = "ALGOLIA_APPLICATION_ID";
const ALGOLIA_API_KEY_VARIABLE: &str = "ALGOLIA_API_KEY";
/// Algolia client
#[derive(Debug)]
pub struct Client {
application_id: Option<String>,
api_key: Option<String>,
}
impl Client {
/// Initialize the client, providing your [APPLICATION_ID](https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/importing-with-the-api/#application-id)
/// and your [API_KEY](https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/importing-with-the-api/#api-key).
pub fn new(application_id: &str, api_key: &str) -> Client {
Client {
application_id: Some(application_id.to_owned()),
api_key: Some(api_key.to_owned()),
}
}
/// Set your client's [APPLICATION_ID](https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/importing-with-the-api/#application-id).
pub fn application_id(mut self, application_id: &str) -> Client {
self.application_id = Some(application_id.to_owned());
self
}
/// Set you client's [API_KEY](https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/importing-with-the-api/#api-key)
pub fn api_key(mut self, api_key: &str) -> Client {
self.api_key = Some(api_key.to_owned());
self
}
/// Initialize the client index, providing your [INDEX_NAME](#).
/// ```no_run
/// # #[macro_use] extern crate serde_derive;
/// # use algoliasearch::Client;
/// # #[derive(Serialize, Deserialize)]
/// # struct User;
/// # fn main() {
/// let index = Client::default().init_index::<User>("users");
/// # }
/// ```
pub fn init_index<T>(self, index_name: &str) -> index::Index<T> {
if self.application_id.is_none() || self.api_key.is_none() {
panic!("application_id and/or api_key are not initialized");
}
index::Index {
application_id: self.application_id.clone().expect("can't panic"),
api_key: self.api_key.expect("can't panic"),
index_name: index_name.to_owned(),
base_url: format!(
"https://{}-dsn.algolia.net/1",
self.application_id.expect("can't panic")
),
index_type: PhantomData,
}
}
}
impl Default for Client {
/// Itinialize the client. It will read the environenment or .env `ALGOLIA_APPLICATION_ID` and `ALGOLIA_API_KEY` variables to be used as
/// [APPLICATION_ID](https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/importing-with-the-api/#application-id)
/// and [API_KEY](https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/importing-with-the-api/#api-key).
fn default() -> Client {
Client {
application_id: env::var(ALGOLIA_APPLICATION_ID_VARIABLE).ok(),
api_key: env::var(ALGOLIA_API_KEY_VARIABLE).ok(),
}
}
}
#[cfg(test)]
mod client_tests {
use super::*;
struct User;
#[test]
#[should_panic(expected = "application_id and/or api_key are not initialized")]
fn test_missing_application_id() {
Client::default()
.api_key("api")
.init_index::<User>("will fail");
}
#[test]
#[should_panic(expected = "application_id and/or api_key are not initialized")]
fn test_missing_api_key() {
Client::default()
.application_id("application")
.init_index::<User>("will fail");
}
#[test]
#[should_panic(expected = "application_id and/or api_key are not initialized")]
fn test_missing_application_id_and_api_key() {
Client::default().init_index::<User>("will fail");
}
}