|
|
|
@ -3,10 +3,13 @@ package com.gitee.sop.sdk.client; |
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.gitee.sop.sdk.common.OpenConfig; |
|
|
|
|
import com.gitee.sop.sdk.common.RequestForm; |
|
|
|
|
import com.gitee.sop.sdk.common.RequestMethod; |
|
|
|
|
import com.gitee.sop.sdk.common.UploadFile; |
|
|
|
|
import com.gitee.sop.sdk.response.BaseResponse; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
import java.net.URLEncoder; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
@ -26,23 +29,45 @@ public class OpenRequest { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public String request(String url, RequestForm requestForm, Map<String, String> header) { |
|
|
|
|
return this.doPost(url, requestForm, header); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected String doPost(String url, RequestForm requestForm, Map<String, String> header) { |
|
|
|
|
try { |
|
|
|
|
Map<String, String> form = requestForm.getForm(); |
|
|
|
|
List<UploadFile> files = requestForm.getFiles(); |
|
|
|
|
if (files != null && files.size() > 0) { |
|
|
|
|
return openHttp.postFile(url, form, header, files); |
|
|
|
|
return openHttp.requestFile(url, form, header, files); |
|
|
|
|
} else { |
|
|
|
|
return openHttp.postFormBody(url, form, header); |
|
|
|
|
RequestMethod requestMethod = requestForm.getRequestMethod(); |
|
|
|
|
if (requestMethod == RequestMethod.GET) { |
|
|
|
|
String query = this.buildGetQueryString(form, requestForm.getCharset()); |
|
|
|
|
if (query != null && query.length() > 0) { |
|
|
|
|
url = url + "?" + query; |
|
|
|
|
} |
|
|
|
|
return openHttp.get(url, header); |
|
|
|
|
} else { |
|
|
|
|
return openHttp.requestFormBody(url, form, header, requestMethod.name()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
return this.causeException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected String buildGetQueryString(Map<String, String> params, String charset) throws UnsupportedEncodingException { |
|
|
|
|
if (params == null || params.size() == 0) { |
|
|
|
|
return ""; |
|
|
|
|
} |
|
|
|
|
StringBuilder query = new StringBuilder(); |
|
|
|
|
int i = 0; |
|
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
|
|
|
|
String name = entry.getKey(); |
|
|
|
|
String value = entry.getValue(); |
|
|
|
|
if (i++ > 0) { |
|
|
|
|
query.append("&"); |
|
|
|
|
} |
|
|
|
|
query.append(name).append("=").append(URLEncoder.encode(value, charset)); |
|
|
|
|
} |
|
|
|
|
return query.toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected String causeException(Exception e) { |
|
|
|
|
ErrorResponse result = new ErrorResponse(); |
|
|
|
|
result.setCode(HTTP_ERROR_CODE); |
|
|
|
|