All Downloads are FREE. Search and download functionalities are using the official Maven repository.

global.maplink.http.Response Maven / Gradle / Ivy

The newest version!
package global.maplink.http;

import global.maplink.http.exceptions.MapLinkHttpException;
import global.maplink.json.JsonMapper;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
public class Response {

    @Getter
    private final int statusCode;

    private final String contentType;

    private final byte[] body;

    public  T parseBodyObject(JsonMapper mapper, Class type) {
        return parseBodyObject(mapper, type, true);
    }

    public  T parseBodyObject(JsonMapper mapper, Class type, boolean throwOnError) {
        if (throwOnError) throwIfIsError();
        return mapper.fromJson(body, type);
    }

    public  List parseBodyArray(JsonMapper mapper, Class type) {
        return parseBodyArray(mapper, type, true);
    }

    public  List parseBodyArray(JsonMapper mapper, Class type, boolean throwOnError) {
        if (throwOnError) throwIfIsError();
        return mapper.fromJsonList(body, type);
    }

    public boolean isOk() {
        return statusCode >= 200 && statusCode < 300;
    }

    public boolean isError() {
        return !isOk();
    }

    public boolean isClientError() {
        return statusCode >= 400 && statusCode < 500;
    }

    public boolean isServerError() {
        return statusCode >= 500 && statusCode < 600;
    }

    public void throwIfIsError() {
        if (isError()) {
            throw new MapLinkHttpException(statusCode, contentType, body);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy