cn.idea360.commons.http.Request Maven / Gradle / Ivy
package cn.idea360.commons.http;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author cuishiying
*/
@Data
public class Request implements Serializable {
/**
* 请求地址
*/
private String url;
/**
* 请求头
*/
private Map headers;
/**
* 表单参数
*/
private Map params;
/**
* 请求体(支持POST、PUT、PATCH)
*/
private String body;
Request(String url, Map headers, Map params, String body) {
this.url = url;
this.headers = headers;
this.params = params;
this.body = body;
}
public static Request.RequestBuilder builder() {
return new Request.RequestBuilder();
}
public static class RequestBuilder {
private String url;
private Map headers;
private Map params;
private String body;
RequestBuilder() {
}
public Request.RequestBuilder url(String url) {
this.url = url;
return this;
}
public Request.RequestBuilder headers(Map headers) {
this.headers = headers;
return this;
}
public Request.RequestBuilder params(Map params) {
this.params = params;
return this;
}
public Request.RequestBuilder body(String body) {
this.body = body;
return this;
}
public Request.RequestBuilder param(String key, Object value) {
if (Objects.isNull(params)) {
this.params = new HashMap<>();
}
this.params.put(key, value);
return this;
}
public Request.RequestBuilder header(String key, String value) {
if (Objects.isNull(headers)) {
this.headers = new HashMap<>();
}
this.headers.put(key, value);
return this;
}
public Request build() {
return new Request(this.url, this.headers, this.params, this.body);
}
}
}