use futures::{Future, Poll};
use error::*;
use client::{AsyncSender, Client, Sender, SyncSender};
use client::requests::{empty_body, DefaultBody, RequestBuilder};
use client::requests::params::Index;
use client::requests::endpoints::IndicesCreateRequest;
use client::requests::raw::RawRequestInner;
use client::responses::CommandResponse;
pub type IndexCreateRequestBuilder<TSender, TBody> = RequestBuilder<TSender, IndexCreateRequestInner<TBody>>;
#[doc(hidden)]
pub struct IndexCreateRequestInner<TBody> {
index: Index<'static>,
body: TBody,
}
impl<TSender> Client<TSender>
where
TSender: Sender,
{
pub fn index_create(&self, index: Index<'static>) -> IndexCreateRequestBuilder<TSender, DefaultBody> {
RequestBuilder::new(
self.clone(),
None,
IndexCreateRequestInner {
index: index,
body: empty_body(),
},
)
}
}
impl<TBody> IndexCreateRequestInner<TBody> {
fn into_request(self) -> IndicesCreateRequest<'static, TBody> {
IndicesCreateRequest::for_index(self.index, self.body)
}
}
impl<TSender, TBody> IndexCreateRequestBuilder<TSender, TBody>
where
TSender: Sender,
TBody: Into<TSender::Body>,
{
pub fn body<TNewBody>(self, body: TNewBody) -> IndexCreateRequestBuilder<TSender, TNewBody>
where
TNewBody: Into<TSender::Body>,
{
RequestBuilder::new(
self.client,
self.params,
IndexCreateRequestInner {
index: self.inner.index,
body: body,
},
)
}
}
impl<TBody> IndexCreateRequestBuilder<SyncSender, TBody>
where
TBody: Into<<SyncSender as Sender>::Body>,
{
pub fn send(self) -> Result<CommandResponse> {
let req = self.inner.into_request();
RequestBuilder::new(self.client, self.params, RawRequestInner::new(req))
.send()?
.into_response()
}
}
impl<TBody> IndexCreateRequestBuilder<AsyncSender, TBody>
where
TBody: Into<<AsyncSender as Sender>::Body>,
{
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)
}
}
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_create(index("testindex")).inner.into_request();
assert_eq!("/testindex", req.url.as_ref());
}
#[test]
fn specify_body() {
let client = SyncClientBuilder::new().build().unwrap();
let req = client
.index_create(index("testindex"))
.body("{}")
.inner
.into_request();
assert_eq!("{}", req.body);
}
}