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
//! Tools for communicating with Fauna.

mod response;

#[cfg(feature = "sync_client")]
mod sync;

pub use response::*;

#[cfg(feature = "sync_client")]
pub use sync::*;

use crate::{
    error::{Error, FaunaErrors},
    expr::Expr,
};
use futures::{future, stream::Stream, Future};
use http::header::{AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE};
use hyper::{client::HttpConnector, Body, StatusCode, Uri};
use hyper_tls::HttpsConnector;
use serde_json;
use std::{borrow::Cow, time::Duration};
use tokio_timer::Timeout;

type Transport = hyper::Client<HttpsConnector<HttpConnector>>;

/// For building a new Fauna client.
pub struct ClientBuilder<'a> {
    uri: Cow<'a, str>,
    secret: Cow<'a, str>,
    timeout: Duration,
}

impl<'a> ClientBuilder<'a> {
    /// Change the uri if using dedicated Fauna servers. Default:
    /// `https://db.fauna.com`.
    pub fn uri(&mut self, uri: impl Into<Cow<'a, str>>) -> &mut Self {
        self.uri = uri.into();
        self
    }

    /// Request timeout. Default: `60 seconds`.
    pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
        self.timeout = timeout;
        self
    }

    /// Creates the client.
    pub fn build(self) -> crate::Result<Client> {
        let mut builder = hyper::Client::builder();
        builder.keep_alive(true);

        let secret_b64 = base64::encode(&format!("{}:", self.secret));

        Ok(Client {
            transport: builder.build(HttpsConnector::new(1)?),
            uri: self.uri.parse()?,
            timeout: self.timeout,
            authorization: format!("Basic {}", secret_b64),
        })
    }

    #[cfg(feature = "sync_client")]
    pub fn build_sync(self) -> crate::Result<SyncClient> {
        Ok(SyncClient::new(self.build()?)?)
    }
}

/// The client for Fauna. Should be created using the
/// [ClientBuilder](struct.ClientBuilder.html).
///
/// Do not create new clients for every request to prevent
/// spamming Fauna servers with new connections.
pub struct Client {
    transport: Transport,
    uri: Uri,
    timeout: Duration,
    authorization: String,
}

impl Client {
    /// Create a new client builder. Secret can be generated in [Fauna Cloud
    /// Console](https://dashboard.fauna.com/keys-new/@db/).
    pub fn builder<'a>(secret: impl Into<Cow<'a, str>>) -> ClientBuilder<'a> {
        ClientBuilder {
            uri: Cow::from("https://db.fauna.com"),
            secret: secret.into(),
            timeout: Duration::new(60, 0),
        }
    }

    /// Send a query to Fauna servers and parsing the response.
    pub fn query<'a, Q>(&self, query: Q) -> FutureResponse<Response>
    where
        Q: Into<Expr<'a>>,
    {
        let query = query.into();
        let payload_json = serde_json::to_string(&query).unwrap();

        trace!("Querying with: {:?}", &payload_json);

        self.request(self.build_request(payload_json), |body| {
            serde_json::from_str(&body).unwrap()
        })
    }

    fn request<F, T>(&self, request: hyper::Request<Body>, f: F) -> FutureResponse<T>
    where
        T: Send + Sync + 'static,
        F: FnOnce(String) -> T + Send + Sync + 'static,
    {
        let send_request = self
            .transport
            .request(request)
            .map_err(|e| Error::ConnectionError(e.into()));

        let requesting = send_request.and_then(move |response| {
            trace!("Client::call got response status {}", response.status());

            let status = response.status();

            let get_body = response
                .into_body()
                .map_err(|e| Error::ConnectionError(e.into()))
                .concat2();

            get_body.and_then(move |body_chunk| {
                if let Ok(body) = String::from_utf8(body_chunk.to_vec()) {
                    trace!("Got response: {:?}", &body);

                    match status {
                        s if s.is_success() => future::ok(f(body)),
                        StatusCode::UNAUTHORIZED => future::err(Error::Unauthorized),
                        StatusCode::BAD_REQUEST => {
                            let errors: FaunaErrors = serde_json::from_str(&body).unwrap();
                            future::err(Error::BadRequest(errors))
                        }
                        StatusCode::NOT_FOUND => {
                            let errors: FaunaErrors = serde_json::from_str(&body).unwrap();
                            future::err(Error::NotFound(errors))
                        }
                        _ => future::err(Error::DatabaseError(body)),
                    }
                } else {
                    future::err(Error::EmptyResponse)
                }
            })
        });

        let with_timeout = Timeout::new(requesting, self.timeout).map_err(|e| {
            if e.is_timer() {
                Error::TimeoutError
            } else {
                match e.into_inner() {
                    Some(error) => error,
                    None => Error::Other,
                }
            }
        });

        FutureResponse(Box::new(with_timeout))
    }

    fn build_request(&self, payload: String) -> hyper::Request<Body> {
        let mut builder = hyper::Request::builder();

        builder.uri(&self.uri);
        builder.method("POST");

        builder.header(CONTENT_LENGTH, format!("{}", payload.len()).as_bytes());
        builder.header(CONTENT_TYPE, "application/json");
        builder.header(AUTHORIZATION, self.authorization.as_bytes());
        builder.header("X-FaunaDB-API-Version", "2.1");

        builder.body(Body::from(payload)).unwrap()
    }
}