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
/*!
Builders for [delete index requests][docs-delete-index].

[docs-delete-index]: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
*/

use futures::{Future, Poll};

use error::*;
use client::{AsyncSender, Client, Sender, SyncSender};
use client::requests::RequestBuilder;
use client::requests::params::Index;
use client::requests::endpoints::IndicesDeleteRequest;
use client::requests::raw::RawRequestInner;
use client::responses::CommandResponse;

/** 
A [delete index request][docs-delete-index] builder that can be configured before sending. 

Call [`Client.index_delete`][Client.index_delete] to get an `IndexDeleteRequestBuilder`.
The `send` method will either send the request [synchronously][send-sync] or [asynchronously][send-async], depending on the `Client` it was deleted from.

[docs-delete-index]: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
[send-sync]: #send-synchronously
[send-async]: #send-asynchronously
[Client.index_delete]: ../../struct.Client.html#delete-index-request
*/
pub type IndexDeleteRequestBuilder<TSender> = RequestBuilder<TSender, IndexDeleteRequestInner>;

#[doc(hidden)]
pub struct IndexDeleteRequestInner {
    index: Index<'static>,
}

/**
# Delete index request
*/
impl<TSender> Client<TSender>
where
    TSender: Sender,
{
    /** 
    Delete a [`IndexDeleteRequestBuilder`][IndexDeleteRequestBuilder] with this `Client` that can be configured before sending.

    For more details, see:

    - [send synchronously][send-sync]
    - [send asynchronously][send-async]
    
    # Examples
    
    Delete an index called `myindex`:
    
    ```no_run
    # extern crate elastic;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let client = SyncClientBuilder::new().build()?;
    let response = client.index_delete(index("myindex")).send()?;

    assert!(response.acknowledged());
    # Ok(())
    # }
    ```

    [IndexDeleteRequestBuilder]: requests/index_delete/type.IndexDeleteRequestBuilder.html
    [builder-methods]: requests/index_delete/type.IndexDeleteRequestBuilder.html#builder-methods
    [send-sync]: requests/index_delete/type.IndexDeleteRequestBuilder.html#send-synchronously
    [send-async]: requests/index_delete/type.IndexDeleteRequestBuilder.html#send-asynchronously
    */
    pub fn index_delete(&self, index: Index<'static>) -> IndexDeleteRequestBuilder<TSender> {
        RequestBuilder::new(self.clone(), None, IndexDeleteRequestInner { index: index })
    }
}

impl IndexDeleteRequestInner {
    fn into_request(self) -> IndicesDeleteRequest<'static> {
        IndicesDeleteRequest::for_index(self.index)
    }
}

/**
# Send synchronously
*/
impl IndexDeleteRequestBuilder<SyncSender> {
    /**
    Send a `IndexDeleteRequestBuilder` synchronously using a [`SyncClient`][SyncClient].

    This will block the current thread until a response arrives and is deserialised.

    # Examples
    
    Delete an index called `myindex`:
    
    ```no_run
    # extern crate elastic;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let client = SyncClientBuilder::new().build()?;
    let response = client.index_delete(index("myindex")).send()?;

    assert!(response.acknowledged());
    # Ok(())
    # }
    ```

    [SyncClient]: ../../type.SyncClient.html
    */
    pub fn send(self) -> Result<CommandResponse> {
        let req = self.inner.into_request();

        RequestBuilder::new(self.client, self.params, RawRequestInner::new(req))
            .send()?
            .into_response()
    }
}

/**
# Send asynchronously
*/
impl IndexDeleteRequestBuilder<AsyncSender> {
    /**
    Send a `IndexDeleteRequestBuilder` asynchronously using an [`AsyncClient`][AsyncClient].
    
    This will return a future that will resolve to the deserialised command response.

    # Examples
    
    Delete an index called `myindex`:
    
    ```no_run
    # extern crate futures;
    # extern crate tokio_core;
    # extern crate elastic;
    # use futures::Future;
    # use elastic::prelude::*;
    # fn main() { run().unwrap() }
    # fn run() -> Result<(), Box<::std::error::Error>> {
    # let core = tokio_core::reactor::Core::new()?;
    # let client = AsyncClientBuilder::new().build(&core.handle())?;
    let future = client.index_delete(index("myindex")).send();

    future.and_then(|response| {
        assert!(response.acknowledged());

        Ok(())
    });
    # Ok(())
    # }
    ```

    [AsyncClient]: ../../type.AsyncClient.html
    */
    pub fn send(self) -> Pending {
        let req = self.inner.into_request();

        let res_future = RequestBuilder::new(self.client, self.params, RawRequestInner::new(req))
            .send()
            .and_then(|res| res.into_response());

        Pending::new(res_future)
    }
}

/** A future returned by calling `send`. */
pub struct Pending {
    inner: Box<Future<Item = CommandResponse, Error = Error>>,
}

impl Pending {
    fn new<F>(fut: F) -> Self
    where
        F: Future<Item = CommandResponse, Error = Error> + 'static,
    {
        Pending {
            inner: Box::new(fut),
        }
    }
}

impl Future for Pending {
    type Item = CommandResponse;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.inner.poll()
    }
}

#[cfg(test)]
mod tests {
    use prelude::*;

    #[test]
    fn default_request() {
        let client = SyncClientBuilder::new().build().unwrap();

        let req = client.index_delete(index("testindex")).inner.into_request();

        assert_eq!("/testindex", req.url.as_ref());
    }
}