Trait elastic_responses::parsing::IsOk
[−]
[src]
pub trait IsOk { fn is_ok<B: ResponseBody>(
head: HttpResponseHead,
unbuffered: Unbuffered<B>
) -> Result<MaybeOkResponse<B>, ParseResponseError>; }
Convert a response message into a either a success or failure result.
This is the main trait that drives response parsing by inspecting the http status and potentially
buffering the response to determine whether or not it represents a successful operation.
This trait doesn't do the actual deserialisation, it just passes on a MaybeOkResponse
.
Some endpoints may not map success or error responses directly to a status code.
In this case, the Unbuffered
body can be buffered into an anonymous json object and inspected
for an error node.
The Unbuffered
type takes care of response bodies that can only be buffered once.
Any type that implements IsOk
can be deserialised by parse
.
Implementations should behave in the following way:
- If the response is successful, this trait should return
Ok(MaybeOkResponse::ok)
. - If the response is an error, this trait should return
Ok(MaybeOkResponse::err)
. - If the response isn't recognised or is otherwise invalid, this trait should return
Err
.
Examples
Implement IsOk
for a custom response type, where a http 404
might still contain a valid response:
impl IsOk for MyResponse { fn is_ok<B: ResponseBody>(head: HttpResponseHead, unbuffered: Unbuffered<B>) -> Result<MaybeOkResponse<B>, ParseResponseError> { match head.status() { 200...299 => Ok(MaybeOkResponse::ok(unbuffered)), 404 => { // If we get a 404, it could be an IndexNotFound error or ok // Check if the response contains a root 'error' node let (body, buffered) = unbuffered.body()?; let is_ok = body.as_object() .and_then(|body| body.get("error")) .is_none(); Ok(MaybeOkResponse::new(is_ok, buffered)) } _ => Ok(MaybeOkResponse::err(unbuffered)), } } }
Required Methods
fn is_ok<B: ResponseBody>(
head: HttpResponseHead,
unbuffered: Unbuffered<B>
) -> Result<MaybeOkResponse<B>, ParseResponseError>
head: HttpResponseHead,
unbuffered: Unbuffered<B>
) -> Result<MaybeOkResponse<B>, ParseResponseError>
Inspect the http response to determine whether or not it succeeded.
Implementors
impl IsOk for Value
impl IsOk for CommandResponse
impl IsOk for PingResponse
impl<T: DeserializeOwned> IsOk for GetResponse<T>
impl IsOk for DeleteResponse
impl IsOk for UpdateResponse
impl<T: DeserializeOwned> IsOk for SearchResponse<T>
impl<TIndex, TType, TId> IsOk for BulkResponse<TIndex, TType, TId>
impl<TIndex, TType, TId> IsOk for BulkErrorsResponse<TIndex, TType, TId>
impl IsOk for IndexResponse
impl IsOk for IndicesExistsResponse