Module elastic::client::responses::parse
[−]
[src]
Utility types for response parsing.
Examples
Implement IsOk
for a deserialisable type that converts a http response into a concrete type.
This example defines a search response that, for whatever reason, only includes the took
field:
#[derive(Deserialize)] struct MyResponse { took: u64 } impl IsOk for MyResponse { fn is_ok<B>(head: HttpResponseHead, body: Unbuffered<B>) -> Result<MaybeOkResponse<B>, ParseResponseError> where B: ResponseBody { match head.status() { // If the status is 2xx then return the response with `ok: true` // The body will be parsed as a `MyResponse`. 200...299 => Ok(MaybeOkResponse::ok(body)), // Otherwise return the response with `ok: false` // The body will be parsed as an `ApiError`. _ => Ok(MaybeOkResponse::err(body)) } } }
The MyResponse
type can then be used for deserialising a concrete response:
let response = client.request(req) .send() .and_then(into_response::<MyResponse>); match response { Ok(response) => { println!("took: {}", response.took); }, Err(e) => { match *e.kind() { ErrorKind::Api(ref e) => { // handle a REST API error }, ref e => { // handle a HTTP or JSON error } } } }
You can also parse the response body into a temporary serde_json::Value
if the status code isn't enough to determine if it's ok.
This will consume the UnbufferedResponse
and return a BufferedResponse
instead that keeps the response body private for later handlers to use.
See the IsOk
trait for more details.
Structs
Buffered |
A response body that has been buffered. |
HttpResponseHead |
The non-body component of the HTTP response. |
MaybeOkResponse |
A response that might be successful or an |
Unbuffered |
A response body that hasn't been buffered yet. |
Enums
MaybeBufferedResponse |
A response body that may or may not have been buffered. |
ParseResponseError |
An error parsing a response stream. |
Traits
IsOk |
Convert a response message into a either a success or failure result. |
ResponseBody |
A http response body that can be buffered into a json value. |