package foundation.icall.callout;
|
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.nio.charset.Charset;
|
import java.util.Set;
|
|
import foundation.dao.bizlogic.DocDescription;
|
import foundation.dao.bizlogic.IRequest;
|
import foundation.util.ContentBuilder;
|
import foundation.util.MapList;
|
import okhttp3.FormBody;
|
import okhttp3.MediaType;
|
import okhttp3.Request.Builder;
|
import okhttp3.RequestBody;
|
|
public class ICallRequest implements IRequest {
|
|
private String url;
|
private MapList<String, String> params;
|
private MapList<String, String> headers;
|
private MapList<String, String> formBody;
|
private JSONResponse jsonBody;
|
private DocDescription docDescription;
|
|
public ICallRequest(String url) {
|
this.url = url;
|
params = new MapList<String, String>(false);
|
headers = new MapList<String, String>(false);
|
formBody = new MapList<String, String>(false);
|
}
|
|
public String paramsToURL() throws UnsupportedEncodingException {
|
if (params.isEmpty()) {
|
return "";
|
}
|
|
ContentBuilder builder = new ContentBuilder("&");
|
Set<String> keySet = params.getKeySet();
|
|
for (String key: keySet) {
|
String name = URLEncoder.encode(key, "utf-8");
|
|
String value = params.get(key);
|
if (value == null) {
|
continue;
|
}
|
|
value = URLEncoder.encode(value, "utf-8");
|
builder.append(name, value);
|
}
|
|
return builder.toString();
|
}
|
|
public void applyHeaders(Builder builder) {
|
Set<String> keySet = headers.getKeySet();
|
|
for (String key: keySet) {
|
String value = headers.get(key);
|
|
if (value == null) {
|
continue;
|
}
|
|
builder.addHeader(key, value);
|
}
|
}
|
|
public void applyJSONBody(Builder builder) {
|
MediaType contentType = MediaType.parse("application/json;charset=utf-8");
|
RequestBody body = RequestBody.create(contentType, jsonBody.toString());
|
builder.post(body);
|
}
|
|
public void applyFormBody(Builder builder) throws UnsupportedEncodingException {
|
Charset charset = Charset.forName("UTF-8");
|
FormBody.Builder formBuilder = new FormBody.Builder(charset);
|
|
Set<String> keySet = formBody.getKeySet();
|
|
for (String key: keySet) {
|
String value = formBody.get(key);
|
formBuilder.add(key, value);
|
}
|
|
FormBody result = formBuilder.build();
|
builder.post(result);
|
}
|
|
public void addHeader(String name, String value) {
|
headers.add(name, value);
|
}
|
|
public void addFormData(String name, String value) {
|
formBody.add(name, value);
|
}
|
|
@Override
|
public String getURL() {
|
return url;
|
}
|
|
public void setURL(String url) {
|
this.url = url;
|
}
|
|
@Override
|
public DocDescription getDocDescription() {
|
return docDescription;
|
}
|
|
@Override
|
public void setDocDescription(DocDescription docDesciption) {
|
this.docDescription = docDesciption;
|
}
|
|
public boolean existsHeaders() {
|
return !headers.isEmpty();
|
}
|
|
public boolean existsJSONBody() {
|
return jsonBody != null;
|
}
|
|
public boolean existsFormBody() {
|
return formBody != null;
|
}
|
|
public void addOneParam(String key, String value) {
|
params.add(key, value);
|
}
|
|
@Override
|
public String getRequestHeader() {
|
return headers.mapToString(",");
|
}
|
|
@Override
|
public String getRequestBody() {
|
return formBody.mapToString(",");
|
}
|
|
@Override
|
public String getBody() {
|
ContentBuilder json = new ContentBuilder(",");
|
json.append("\"url\":\"" + url + "\"");
|
|
json.append("\"params\":" + params.mapToString(","));
|
json.append("\"headers\":" + headers.mapToString(","));
|
json.append("\"formBody\":" + formBody.mapToString(","));
|
|
return json.toString();
|
}
|
}
|