package com.njzscloud.common.http.support;

import java.util.List;
import java.util.Map;

public final class ResponseResult<T> {
    /**
     * 是否成功, HTTP 状态码 200..299
     */
    public final boolean success;

    /**
     * HTTP 状态码
     */
    public final int code;

    /**
     * HTTP 状态信息
     */
    public final String status;

    /**
     * HTTP 响应头
     */
    public final Map<String, List<String>> headers;

    /**
     * HTTP 响应体
     */
    public final T body;

    /**
     * 错误信息, 在请求发生异常时有值
     */
    public final Exception e;

    private ResponseResult(boolean success, int code, String status, Map<String, List<String>> headers, T body, Exception e) {
        this.success = success;
        this.code = code;
        this.status = status;
        this.headers = headers;
        this.body = body;
        this.e = e;
    }

    public static <T> ResponseResultBuilder<T> builder() {
        return new ResponseResultBuilder<T>();
    }

    public static class ResponseResultBuilder<T> {
        private int code;
        private String status;
        private Map<String, List<String>> headers;
        private T body;
        private Exception e;

        private ResponseResultBuilder() {
        }

        public ResponseResultBuilder<T> code(int code) {
            this.code = code;
            return this;
        }

        public ResponseResultBuilder<T> status(String status) {
            this.status = status;
            return this;
        }

        public ResponseResultBuilder<T> headers(Map<String, List<String>> headers) {
            this.headers = headers;
            return this;
        }

        public ResponseResultBuilder<T> body(T body) {
            this.body = body;
            return this;
        }

        public ResponseResultBuilder<T> e(Exception e) {
            this.e = e;
            return this;
        }

        public ResponseResult<T> build() {
            return new ResponseResult<>(
                    code >= 200 && code <= 299,
                    code,
                    status,
                    headers,
                    body,
                    e
            );
        }

    }
}
