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

io.github.biezhi.wechat.api.request.ApiRequest Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version

package io.github.biezhi.wechat.api.request;

import io.github.biezhi.wechat.api.constant.Constant;
import io.github.biezhi.wechat.api.response.ApiResponse;
import lombok.Getter;
import okhttp3.Headers;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础请求
 *
 * @author biezhi
 */
@Getter
public abstract class ApiRequest {

    protected int timeout = 10;
    protected boolean noRedirect;
    protected boolean jsonBody;
    protected boolean multipart;

    protected String url;
    protected String method = "GET";
    protected String fileName;
    protected String contentType = "application/x-www-form-urlencoded";
    protected Headers headers;
    @SuppressWarnings("unchecked")
    protected final T thisAsT = (T) this;
    private final Class  responseClass;
    private final Map parameters;

    public ApiRequest(String url, Class responseClass) {
        this.url = url;
        this.responseClass = responseClass;
        this.parameters = new HashMap();
        this.headers = Headers.of("User-Agent", Constant.USER_AGENT, "Content-Type", this.contentType);
    }

    public T header(String name, String value) {
        this.headers = this.headers.newBuilder().set(name, value).build();
        return thisAsT;
    }

    public T add(String name, Object val) {
        parameters.put(name, val);
        return thisAsT;
    }

    public T noRedirect() {
        this.noRedirect = true;
        return thisAsT;
    }

    public T multipart() {
        this.multipart = true;
        return thisAsT;
    }

    public Type getResponseType() {
        return responseClass;
    }

    public T url(String url) {
        this.url = url;
        return thisAsT;
    }

    public T timeout(int seconds) {
        this.timeout = seconds;
        return thisAsT;
    }

    public T fileName(String fileName) {
        this.fileName = fileName;
        return thisAsT;
    }

    public T post() {
        this.method = "POST";
        return thisAsT;
    }

    public T jsonBody() {
        this.jsonBody = true;
        this.contentType = "application/json; charset=UTF-8";
        this.header("Content-Type", this.contentType);
        return thisAsT;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy