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
/*! Builders for ping requests. */ use futures::{Future, Poll}; use error::{Error, Result}; use client::{AsyncSender, Client, Sender, SyncSender}; use client::requests::RequestBuilder; use client::requests::endpoints::PingRequest; use client::requests::raw::RawRequestInner; use client::responses::PingResponse; /** A ping request builder that can be configured before sending. Call [`Client.ping`][Client.ping] to get a `PingRequestBuilder`. The `send` method will either send the request [synchronously][send-sync] or [asynchronously][send-async], depending on the `Client` it was created from. [send-sync]: #send-synchronously [send-async]: #send-asynchronously [Client.ping]: ../../struct.Client.html#ping-request */ pub type PingRequestBuilder<TSender> = RequestBuilder<TSender, PingRequestInner>; #[doc(hidden)] pub struct PingRequestInner; /** # Ping request */ impl<TSender> Client<TSender> where TSender: Sender, { /** Create a [`PingRequestBuilder`][PingRequestBuilder] with this `Client` that can be configured before sending. For more details, see: - [send synchronously][send-sync] - [send asynchronously][send-async] # Examples Ping an Elasticsearch node. ```no_run # extern crate serde; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # #[macro_use] extern crate serde_json; # extern crate elastic; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { # #[derive(Debug, Serialize, Deserialize, ElasticType)] # struct MyType { } # let client = SyncClientBuilder::new().build()?; let response = client.ping().send()?; println!("node: {}", response.name()); # Ok(()) # } ``` [PingRequestBuilder]: requests/ping/type.PingRequestBuilder.html [send-sync]: requests/ping/type.PingRequestBuilder.html#send-synchronously [send-async]: requests/ping/type.PingRequestBuilder.html#send-asynchronously */ pub fn ping(&self) -> PingRequestBuilder<TSender> { RequestBuilder::new(self.clone(), None, PingRequestInner) } } impl PingRequestInner { fn into_request(self) -> PingRequest<'static> { PingRequest::new() } } /** # Send synchronously */ impl PingRequestBuilder<SyncSender> { /** Send a `PingRequestBuilder` synchronously using a [`SyncClient`][SyncClient]. This will block the current thread until a response arrives and is deserialised. # Examples Ping an Elasticsearch node: ```no_run # extern crate serde; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # #[macro_use] extern crate serde_json; # extern crate elastic; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { # #[derive(Debug, Serialize, Deserialize, ElasticType)] # struct MyType { } # let client = SyncClientBuilder::new().build()?; let response = client.ping().send()?; println!("node: {}", response.name()); # Ok(()) # } ``` [SyncClient]: ../../type.SyncClient.html */ pub fn send(self) -> Result<PingResponse> { let req = self.inner.into_request(); RequestBuilder::new(self.client, self.params, RawRequestInner::new(req)) .send()? .into_response() } } /** # Send asynchronously */ impl PingRequestBuilder<AsyncSender> { /** Send a `PingRequestBuilder` asynchronously using an [`AsyncClient`][AsyncClient]. This will return a future that will resolve to the deserialised ping response. # Examples Ping an Elasticsearch node: ```no_run # extern crate tokio_core; # extern crate futures; # extern crate serde; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # extern crate elastic; # use futures::Future; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { # #[derive(Debug, Serialize, Deserialize, ElasticType)] # struct MyType { } # let core = tokio_core::reactor::Core::new()?; # let client = AsyncClientBuilder::new().build(&core.handle())?; let future = client.ping().send(); future.and_then(|response| { println!("node: {}", response.name()); 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 = PingResponse, Error = Error>>, } impl Pending { fn new<F>(fut: F) -> Self where F: Future<Item = PingResponse, Error = Error> + 'static, { Pending { inner: Box::new(fut), } } } impl Future for Pending { type Item = PingResponse; 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.ping().inner.into_request(); assert_eq!("/", req.url.as_ref()); } }