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
/*!
Request types for the Elasticsearch REST API.

This module contains implementation details that are useful if you want to customise the request process, but aren't generally important for sending requests.
*/

use fluent_builder::FluentBuilder;
use std::sync::Arc;
use tokio_threadpool::ThreadPool;

use client::sender::{
    AsyncSender,
    RequestParams,
    Sender,
};
use client::Client;

pub use elastic_requests::endpoints;
pub use elastic_requests::params;
pub use elastic_requests::{
    empty_body,
    DefaultBody,
    Endpoint,
    UrlPath,
};

pub use self::endpoints::*;
pub use self::params::*;

pub mod raw;
pub use self::raw::RawRequestBuilder;

// Search requests
pub mod search;
pub use self::search::SearchRequestBuilder;

// Document requests
pub mod document_delete;
pub mod document_get;
pub mod document_index;
pub mod document_put_mapping;
pub mod document_update;
pub use self::document_delete::DeleteRequestBuilder;
pub use self::document_get::GetRequestBuilder;
pub use self::document_index::IndexRequestBuilder;
pub use self::document_put_mapping::PutMappingRequestBuilder;
pub use self::document_update::UpdateRequestBuilder;

// Index requests
pub mod index_close;
pub mod index_create;
pub mod index_delete;
pub mod index_exists;
pub mod index_open;
pub use self::index_close::IndexCloseRequestBuilder;
pub use self::index_create::IndexCreateRequestBuilder;
pub use self::index_delete::IndexDeleteRequestBuilder;
pub use self::index_exists::IndexExistsRequestBuilder;
pub use self::index_open::IndexOpenRequestBuilder;

// Misc requests
pub mod bulk;
pub mod ping;
pub use self::bulk::BulkRequestBuilder;
pub use self::ping::PingRequestBuilder;

pub mod common;

/**
A builder for a request.

This structure wraps up a concrete REST API request type and lets you adjust parameters before sending it.
The `RequestBuilder` has two generic parameters:

- `TSender`: the kind of request sender. This can be either synchronous or asynchronous
- `TRequest`: the inner request type, for example `SearchRequestBuilder`.

`RequestBuilder` contains methods that are common to all request builders.
*/
pub struct RequestBuilder<TSender, TRequest>
where
    TSender: Sender,
{
    client: Client<TSender>,
    params_builder: FluentBuilder<RequestParams>,
    inner: TRequest,
}

/**
# Methods for any request builder

The following methods can be called on any request builder, whether it's synchronous or asynchronous.
*/
impl<TSender, TRequest> RequestBuilder<TSender, TRequest>
where
    TSender: Sender,
{
    fn initial(client: Client<TSender>, req: TRequest) -> Self {
        RequestBuilder {
            client: client,
            params_builder: FluentBuilder::new(),
            inner: req,
        }
    }

    fn new(client: Client<TSender>, builder: FluentBuilder<RequestParams>, req: TRequest) -> Self {
        RequestBuilder {
            client: client,
            params_builder: builder,
            inner: req,
        }
    }

    /**
    Override the parameters for this request.

    This method will box the given closure and use it to mutate the request parameters.
    It will be called after a node address has been chosen so `params` can be used to override the url a request will be sent to.
    Each call to `params` will be chained so it can be called multiple times but it's recommended to only call once.

    # Examples

    Add a url param to force an index refresh:

    ```no_run
    # extern crate elastic;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let client = SyncClientBuilder::new().build()?;
    # fn get_req() -> PingRequest<'static> { PingRequest::new() }
    let builder = client.request(get_req())
                        .params_fluent(|p| p.url_param("refresh", true));
    # Ok(())
    # }
    ```

    Force the request to be sent to `http://different-host:9200`:

    ```no_run
    # extern crate elastic;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let client = SyncClientBuilder::new().build()?;
    # fn get_req() -> PingRequest<'static> { PingRequest::new() }
    let builder = client.request(get_req())
                        .params_fluent(|p| p.base_url("http://different-host:9200"));
    # Ok(())
    # }
    ```
    */
    pub fn params_fluent(
        mut self,
        builder: impl Fn(RequestParams) -> RequestParams + 'static,
    ) -> Self {
        self.params_builder = self.params_builder.fluent(builder).boxed();

        self
    }

    /**
    Specify default request parameters.

    This method differs from `params_fluent` by not taking any default parameters into account.
    The `RequestParams` passed in are exactly the `RequestParams` used to build the request.

    # Examples

    Add a url param to force an index refresh and send the request to `http://different-host:9200`:

    ```no_run
    # extern crate elastic;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let client = SyncClientBuilder::new().build()?;
    # fn get_req() -> PingRequest<'static> { PingRequest::new() }
    let builder = client.request(get_req())
                        .params(RequestParams::new("http://different-hos:9200").url_param("refresh", true));
    # Ok(())
    # }
    ```
    */
    pub fn params(mut self, params: impl Into<RequestParams>) -> Self {
        self.params_builder = self.params_builder.value(params.into());

        self
    }
}

/**
# Methods for asynchronous request builders

The following methods can be called on any asynchronous request builder.
*/
impl<TRequest> RequestBuilder<AsyncSender, TRequest> {
    /**
    Override the thread pool used for deserialisation for this request.

    # Examples

    Use the given thread pool to deserialise the response:

    ```no_run
    # extern crate tokio;
    # extern crate tokio_threadpool;
    # extern crate elastic;
    # use std::sync::Arc;
    # use tokio_threadpool::ThreadPool;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let client = AsyncClientBuilder::new().build()?;
    # fn get_req() -> PingRequest<'static> { PingRequest::new() }
    let pool = ThreadPool::new();
    let builder = client.request(get_req())
                        .serde_pool(Arc::new(pool));
    # Ok(())
    # }
    ```

    Never deserialise the response on a thread pool:

    ```no_run
    # extern crate tokio;
    # extern crate tokio_threadpool;
    # extern crate elastic;
    # use tokio_threadpool::ThreadPool;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let client = AsyncClientBuilder::new().build()?;
    # fn get_req() -> PingRequest<'static> { PingRequest::new() }
    let builder = client.request(get_req())
                        .serde_pool(None);
    # Ok(())
    # }
    ```
    */
    pub fn serde_pool(mut self, pool: impl Into<Option<Arc<ThreadPool>>>) -> Self {
        self.client.sender.serde_pool = pool.into();

        self
    }
}

pub mod prelude {
    /*! A glob import for convenience. */

    pub use super::endpoints::*;
    pub use super::params::*;

    pub use super::bulk::{
        bulk,
        bulk_raw,
        BulkOperation,
    };

    pub use super::{
        empty_body,
        DefaultBody,
        DeleteRequestBuilder,
        GetRequestBuilder,
        IndexCloseRequestBuilder,
        IndexCreateRequestBuilder,
        IndexDeleteRequestBuilder,
        IndexOpenRequestBuilder,
        IndexRequestBuilder,
        PingRequestBuilder,
        PutMappingRequestBuilder,
        RawRequestBuilder,
        SearchRequestBuilder,
        UpdateRequestBuilder,
    };
}