com.hileone.restretrofit.response.RestResponse.java Source code

Java tutorial

Introduction

Here is the source code for com.hileone.restretrofit.response.RestResponse.java

Source

/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hileone.restretrofit.response;

import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Response;

/**
 * An HTTP response.
 */
public final class RestResponse<T> {

    /**
     * Create a successful response from {@code rawResponse} with {@code body} as the deserialized
     * body.
     */
    public static <T> RestResponse<T> success(T body, com.squareup.okhttp.Response rawResponse) {
        if (rawResponse == null) {
            throw new NullPointerException("rawResponse == null");
        }
        if (!rawResponse.isSuccessful()) {
            throw new IllegalArgumentException("rawResponse must be successful response");
        }
        return new RestResponse<>(rawResponse, body, null);
    }

    /**
     * Something Wrong
     */
    public static <T> RestResponse<T> error(ErrorResult error, Response rawResponse) {
        if (error == null) {
            throw new NullPointerException("body == null");
        }
        if (rawResponse == null) {
            throw new NullPointerException("rawResponse == null");
        }
        return new RestResponse<>(rawResponse, null, error);
    }

    private final Response rawResponse;
    private final T body;
    private final ErrorResult errorBody;

    private RestResponse(Response rawResponse, T body, ErrorResult errorBody) {
        this.rawResponse = rawResponse;
        this.body = body;
        this.errorBody = errorBody;
    }

    /**
     * The raw response from the HTTP client.
     */
    public Response raw() {
        return rawResponse;
    }

    /**
     * HTTP status code.
     */
    public int code() {
        return rawResponse.code();
    }

    /**
     * HTTP status message or null if unknown.
     */
    public String message() {
        return rawResponse.message();
    }

    /**
     * HTTP headers.
     */
    public Headers headers() {
        return rawResponse.headers();
    }

    /**
     * {@code true} if {@link #code()} is in the range [200..300).
     */
    public boolean isSuccess() {
        return rawResponse.code() >= 200 && rawResponse.code() < 300;
    }

    /**
     * The deserialized response body of a {@linkplain #isSuccess() successful} response.
     */
    public T body() {
        return body;
    }

    /**
     * The raw response body of an {@linkplain #isSuccess() unsuccessful} response.
     */
    public ErrorResult errorBody() {
        return errorBody;
    }
}