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
use crate::database::Database;
use crate::error::{CouchError, CouchResult};
use crate::types::system::{CouchResponse, CouchStatus, DbInfo};
use base64::write::EncoderWriter as Base64Encoder;
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE, REFERER, USER_AGENT};
use reqwest::{self, Method, StatusCode, Url};
use reqwest::{header, RequestBuilder};
use std::collections::HashMap;
use std::io::Write;
use std::time::Duration;

fn construct_json_headers(uri: Option<&str>) -> HeaderMap {
    let mut headers = HeaderMap::new();
    headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));
    headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

    if let Some(u) = uri {
        headers.insert(REFERER, HeaderValue::from_str(u).unwrap());
    }

    headers
}

/// Client handles the URI manipulation logic and the HTTP calls to the CouchDB REST API.
/// It is also responsible for the creation/access/destruction of databases.
#[derive(Debug, Clone)]
pub struct Client {
    _client: reqwest::Client,
    dbs: Vec<&'static str>,
    _gzip: bool,
    _timeout: u64,
    pub uri: String,
    username: Option<String>,
    password: Option<String>,
    pub db_prefix: String,
}

const TEST_DB_HOST: &str = "http://localhost:5984";
const TEST_DB_USER: &str = "admin";
const TEST_DB_PW: &str = "password";
const DEFAULT_TIME_OUT: u64 = 10;

impl Client {
    /// new creates a new Couch client with a default timeout of 10 seconds.
    /// The timeout is applied from when the request starts connecting until the response body has finished.
    /// The URI has to be in this format: http://hostname:5984, for example: http://192.168.64.5:5984
    pub fn new(uri: &str, username: &str, password: &str) -> CouchResult<Client> {
        Client::new_with_timeout(uri, Some(username), Some(password), DEFAULT_TIME_OUT)
    }

    /// new_no_auth creates a new Couch client with a default timeout of 10 seconds. *Without authentication*.
    /// The timeout is applied from when the request starts connecting until the response body has finished.
    /// The URI has to be in this format: http://hostname:5984, for example: http://192.168.64.5:5984
    pub fn new_no_auth(uri: &str) -> CouchResult<Client> {
        Client::new_with_timeout(uri, None, None, DEFAULT_TIME_OUT)
    }

    /// new_local_test creates a new Couch client *for testing purposes* with a default timeout of 10 seconds.
    /// The timeout is applied from when the request starts connecting until the response body has finished.
    /// The URI that will be used is: http://hostname:5984, with a username of "admin" and a password
    /// of "password". Use this only for testing!!!
    pub fn new_local_test() -> CouchResult<Client> {
        Client::new_with_timeout(TEST_DB_HOST, Some(TEST_DB_USER), Some(TEST_DB_PW), DEFAULT_TIME_OUT)
    }

    /// new_with_timeout creates a new Couch client. The URI has to be in this format: http://hostname:5984,
    /// The timeout is applied from when the request starts connecting until the response body has finished.
    /// Timeout is in seconds.
    pub fn new_with_timeout(
        uri: &str,
        username: Option<&str>,
        password: Option<&str>,
        timeout: u64,
    ) -> CouchResult<Client> {
        let mut headers = header::HeaderMap::new();

        if let Some(username) = username {
            let mut header_value = b"Basic ".to_vec();
            {
                let mut encoder = Base64Encoder::new(&mut header_value, base64::STANDARD);
                // The unwraps here are fine because Vec::write* is infallible.
                write!(encoder, "{}:", username).unwrap();
                if let Some(password) = password {
                    write!(encoder, "{}", password).unwrap();
                }
            }

            let auth_header = header::HeaderValue::from_bytes(&header_value).expect("can not set AUTHORIZATION header");
            headers.insert(header::AUTHORIZATION, auth_header);
        }

        let client = reqwest::Client::builder()
            .default_headers(headers)
            .gzip(true)
            .timeout(Duration::new(timeout, 0))
            .build()?;

        Ok(Client {
            _client: client,
            uri: uri.to_string(),
            _gzip: true,
            _timeout: timeout,
            dbs: Vec::new(),
            db_prefix: String::new(),
            username: username.map(|u| u.to_string()),
            password: password.map(|p| p.to_string()),
        })
    }

    pub fn get_self(&mut self) -> &mut Self {
        self
    }

    pub fn set_uri(&mut self, uri: String) -> &Self {
        self.uri = uri;
        self
    }

    pub fn set_prefix(&mut self, prefix: String) -> &Self {
        self.db_prefix = prefix;
        self
    }

    /// List the databases in CouchDB
    ///
    /// Usage:
    /// ```
    /// use std::error::Error;
    ///
    /// const DB_HOST: &str = "http://localhost:5984";
    /// const DB_USER: &str = "admin";
    /// const DB_PW: &str = "password";
    /// const TEST_DB: &str = "test_db";
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn Error>> {
    ///     let client = couch_rs::Client::new(DB_HOST, DB_USER, DB_PW)?;
    ///     let db = client.db(TEST_DB).await?;
    ///     let dbs = client.list_dbs().await?;
    ///     dbs.iter().for_each(|db| println!("Database: {}", db));
    ///     Ok(())
    /// }
    ///```     
    pub async fn list_dbs(&self) -> CouchResult<Vec<String>> {
        let response = self.get(String::from("/_all_dbs"), None)?.send().await?;
        let data = response.json().await?;

        Ok(data)
    }

    fn build_dbname(&self, dbname: &str) -> String {
        self.db_prefix.clone() + dbname
    }

    /// Connect to an existing database, or create a new one, when this one does not exist.
    pub async fn db(&self, dbname: &str) -> CouchResult<Database> {
        let name = self.build_dbname(dbname);

        let db = Database::new(name.clone(), self.clone());

        let path = self.create_path(name, None)?;

        let head_response = self
            ._client
            .head(&path)
            .headers(construct_json_headers(None))
            .send()
            .await?;

        match head_response.status() {
            StatusCode::OK => Ok(db),
            _ => self.make_db(dbname).await,
        }
    }

    /// Create a new database with the given name
    pub async fn make_db(&self, dbname: &str) -> CouchResult<Database> {
        let name = self.build_dbname(dbname);

        let db = Database::new(name.clone(), self.clone());

        let path = self.create_path(name, None)?;

        let put_response = self
            ._client
            .put(&path)
            .headers(construct_json_headers(None))
            .send()
            .await?;

        let status = put_response.status();
        let s: CouchResponse = put_response.json().await?;

        match s.ok {
            Some(true) => Ok(db),
            _ => {
                let err = s.error.unwrap_or_else(|| s!("unspecified error"));
                Err(CouchError::new(err, status))
            }
        }
    }

    /// Destroy the database with the given name
    pub async fn destroy_db(&self, dbname: &str) -> CouchResult<bool> {
        let path = self.create_path(self.build_dbname(dbname), None)?;
        let response = self
            ._client
            .delete(&path)
            .headers(construct_json_headers(None))
            .send()
            .await?;

        let s: CouchResponse = response.json().await?;

        Ok(s.ok.unwrap_or(false))
    }

    /// Checks if a database exists
    ///
    /// Usage:
    /// ```
    /// use couch_rs::error::CouchResult;
    ///
    /// const TEST_DB: &str = "test_db";
    ///
    /// #[tokio::main]
    /// async fn main() -> CouchResult<()> {
    ///     let client = couch_rs::Client::new_local_test()?;
    ///     let db = client.db(TEST_DB).await?;
    ///
    ///     if db.exists(TEST_DB).await {
    ///         println!("The database exists");
    ///     }
    ///
    ///     return Ok(());
    /// }
    /// ```
    pub async fn exists(&self, dbname: &str) -> CouchResult<bool> {
        let path = self.create_path(self.build_dbname(dbname), None)?;
        let result = self.head(path, None)?.send().await;
        Ok(result.is_ok())
    }

    /// Gets information about the specified database.
    /// See [common](https://docs.couchdb.org/en/stable/api/database/common.html) for more details.
    pub async fn get_info(&self, dbname: &str) -> CouchResult<DbInfo> {
        let path = self.create_path(self.build_dbname(dbname), None)?;
        let response = self.get(path, None)?.send().await?.error_for_status()?;
        let info = response.json().await?;
        Ok(info)
    }

    /// Returns meta information about the instance. The response contains information about the server,
    /// including a welcome message and the version of the server.
    /// See [common](https://docs.couchdb.org/en/stable/api/server/common.html) for more details.
    pub async fn check_status(&self) -> CouchResult<CouchStatus> {
        let response = self
            ._client
            .get(&self.uri)
            .headers(construct_json_headers(None))
            .send()
            .await?;

        let status = response.json().await?;
        Ok(status)
    }

    fn create_path(&self, path: String, args: Option<HashMap<String, String>>) -> CouchResult<String> {
        let mut uri = Url::parse(&self.uri)?.join(&path)?;

        if let Some(ref map) = args {
            let mut qp = uri.query_pairs_mut();
            for (k, v) in map {
                qp.append_pair(k, v);
            }
        }

        Ok(uri.into_string())
    }

    pub fn req(
        &self,
        method: Method,
        path: String,
        opts: Option<HashMap<String, String>>,
    ) -> CouchResult<RequestBuilder> {
        let uri = self.create_path(path, opts)?;
        let req = self
            ._client
            .request(method, &uri)
            .headers(construct_json_headers(Some(&uri)));

        // req.header(reqwest::header::Referer::new(uri.clone()));

        Ok(req)
    }

    pub(crate) fn get(&self, path: String, args: Option<HashMap<String, String>>) -> CouchResult<RequestBuilder> {
        Ok(self.req(Method::GET, path, args)?)
    }

    pub(crate) fn post(&self, path: String, body: String) -> CouchResult<RequestBuilder> {
        let req = self.req(Method::POST, path, None)?.body(body);
        Ok(req)
    }

    pub(crate) fn put(&self, path: String, body: String) -> CouchResult<RequestBuilder> {
        let req = self.req(Method::PUT, path, None)?.body(body);
        Ok(req)
    }

    pub(crate) fn head(&self, path: String, args: Option<HashMap<String, String>>) -> CouchResult<RequestBuilder> {
        Ok(self.req(Method::HEAD, path, args)?)
    }

    pub(crate) fn delete(&self, path: String, args: Option<HashMap<String, String>>) -> CouchResult<RequestBuilder> {
        Ok(self.req(Method::DELETE, path, args)?)
    }
}