| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.core.jiandaoyun.model.http; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.core.jiandaoyun.util.LimitUtil; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.Charsets; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.http.Header; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.HttpClient; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.client.methods.HttpRequestBase; |
| | | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| | | import org.apache.http.entity.ContentType; |
| | | import org.apache.http.entity.StringEntity; |
| | | import org.apache.http.entity.mime.MultipartEntityBuilder; |
| | | import org.apache.http.entity.mime.content.StringBody; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.impl.io.EmptyInputStream; |
| | | import org.apache.http.message.BasicHeader; |
| | | import org.apache.http.ssl.SSLContextBuilder; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import java.io.File; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | public abstract class ApiClient { |
| | | |
| | | /** |
| | | * apiKey |
| | | */ |
| | | private String apiKey; |
| | | |
| | | /** |
| | | * å°å |
| | | */ |
| | | private String host; |
| | | |
| | | /** |
| | | * é»è®¤çæ¬ |
| | | */ |
| | | private String defaultVersion; |
| | | |
| | | /** |
| | | * åæ³çæ¬ |
| | | */ |
| | | private List<String> validVersionList; |
| | | |
| | | public ApiClient(String apiKey, String host) { |
| | | this.apiKey = apiKey; |
| | | this.host = host; |
| | | } |
| | | |
| | | public String getApiKey() { |
| | | return apiKey; |
| | | } |
| | | |
| | | public void setApiKey(String apiKey) { |
| | | this.apiKey = apiKey; |
| | | } |
| | | |
| | | public String getHost() { |
| | | return host; |
| | | } |
| | | |
| | | public void setHost(String host) { |
| | | this.host = host; |
| | | } |
| | | |
| | | public String getDefaultVersion() { |
| | | return defaultVersion; |
| | | } |
| | | |
| | | public void setDefaultVersion(String defaultVersion) { |
| | | this.defaultVersion = defaultVersion; |
| | | } |
| | | |
| | | public List<String> getValidVersionList() { |
| | | return validVersionList; |
| | | } |
| | | |
| | | public void setValidVersionList(List<String> validVersionList) { |
| | | this.validVersionList = validVersionList; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * çæ path |
| | | * |
| | | * @param version - çæ¬å· |
| | | * @param path - è·¯å¾ |
| | | * @return æ¥å£çè·¯å¾ |
| | | */ |
| | | public abstract String generatePath(String version, String path); |
| | | |
| | | /** |
| | | * è·å¾åæ³ççæ¬å· |
| | | * |
| | | * @param version - çæ¬å· |
| | | * @return åæ³ççæ¬å· |
| | | */ |
| | | public String getValidVersion(String version) { |
| | | if (this.getValidVersionList() != null && this.getValidVersionList().contains(version)) { |
| | | return version; |
| | | } |
| | | return this.getDefaultVersion(); |
| | | } |
| | | |
| | | /** |
| | | * åéPOSTè¯·æ± |
| | | * |
| | | * @param param - 请æ±åæ° |
| | | * @return æ¥å£è¿ååæ° |
| | | */ |
| | | public Map<String, Object> sendPostRequest(HttpRequestParam param) throws Exception { |
| | | if (param == null || StringUtils.isBlank(param.getPath())) { |
| | | throw new Exception("ç¼ºå¤±åæ°ï¼"); |
| | | } |
| | | HttpClient client = getSSLHttpClient(); |
| | | Header[] headers = getHttpHeaders(this.getApiKey()); |
| | | String url = this.host + param.getPath(); |
| | | log.error("===ç®éäºæ¥å£urlï¼"+url); |
| | | HttpRequestBase request = new HttpPost(url); |
| | | |
| | | // 请æ±åæ° |
| | | if (param.getData() != null) { |
| | | ObjectMapper queryMap = new ObjectMapper(); |
| | | HttpEntity entity = new StringEntity(queryMap.writeValueAsString(param.getData()), Charsets.UTF_8); |
| | | ((HttpPost) request).setEntity(entity); |
| | | } |
| | | // 设置请æ±å¤´ |
| | | request.setHeaders(headers); |
| | | // éæµé»å¡ |
| | | LimitUtil.tryBeforeRun(); |
| | | // åé请æ±å¹¶è·åè¿åç»æ |
| | | HttpResponse response = client.execute(request); |
| | | // è¿åç¶æç |
| | | int statusCode = response.getStatusLine().getStatusCode(); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | // æé¨åæ¥å£ç´æ¥è¿å æ²¡ææ°æ® |
| | | // fixï¼ä¸è½ç¨content-length大äº0夿ï¼response header为gzipç¼ç æ¹å¼çæ
åµä¸ä¸º-1 |
| | | if (!(response.getEntity().getContent() instanceof EmptyInputStream)) { |
| | | result = (Map<String, Object>) mapper.readValue(response.getEntity().getContent(), Object.class); |
| | | } |
| | | if (statusCode >= 400) { |
| | | log.error("===ç®éäºæ¥å£ï¼è¯·æ±é误ï¼statusCode:" + statusCode + ",Error Code: " + result.get("code") + ", Error Msg: " + result.get("msg")); |
| | | throw new Exception("请æ±é误ï¼statusCode:" + statusCode + ",Error Code: " + result.get("code") + ", Error Msg: " + result.get("msg")); |
| | | } else { |
| | | // å¤çè¿åç»æ |
| | | log.error("===ç®éäºæ¥å£ï¼è¯·æ±æåresult:" + JSONObject.toJSONString(result)); |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | private static HttpClient getSSLHttpClient() throws Exception { |
| | | //ä¿¡ä»»ææ |
| | | SSLContext sslContext = |
| | | new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build(); |
| | | SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); |
| | | return HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
| | | } |
| | | |
| | | /** |
| | | * è·å请æ±å¤´ä¿¡æ¯ |
| | | * |
| | | * @return 请æ±å¤´ä¿¡æ¯ |
| | | */ |
| | | private Header[] getHttpHeaders(String apiKey) { |
| | | List<Header> headerList = new ArrayList<>(); |
| | | headerList.add(new BasicHeader("Authorization", "Bearer " + apiKey)); |
| | | headerList.add(new BasicHeader("Content-Type", "application/json;charset=utf-8")); |
| | | return headerList.toArray(new Header[headerList.size()]); |
| | | } |
| | | |
| | | public Map<String, Object> httpPostFile(String url, String token, File file) throws Exception { |
| | | HttpClient client = getSSLHttpClient(); |
| | | HttpPost httpPost = new HttpPost(url); |
| | | MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| | | httpPost.addHeader("token", token); |
| | | builder.addBinaryBody("file", file, ContentType.MULTIPART_FORM_DATA, file.getName()); |
| | | // ä¼ é token |
| | | builder.addTextBody("token", token); |
| | | StringBody tokenBody = new StringBody(token, ContentType.MULTIPART_FORM_DATA); |
| | | builder.addPart("token", tokenBody); |
| | | HttpEntity entity = builder.build(); |
| | | httpPost.setEntity(entity); |
| | | // éæµé»å¡ |
| | | LimitUtil.tryBeforeRun(); |
| | | // åé请æ±å¹¶è·åè¿åç»æ |
| | | HttpResponse response = client.execute(httpPost); |
| | | // è¿åç¶æç |
| | | int statusCode = response.getStatusLine().getStatusCode(); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | // æé¨åæ¥å£ç´æ¥è¿å æ²¡ææ°æ® |
| | | // fixï¼ä¸è½ç¨content-length大äº0夿ï¼response header为gzipç¼ç æ¹å¼çæ
åµä¸ä¸º-1 |
| | | if (!(response.getEntity().getContent() instanceof EmptyInputStream)) { |
| | | result = (Map<String, Object>) mapper.readValue(response.getEntity().getContent(), Object.class); |
| | | } |
| | | if (statusCode >= 400) { |
| | | throw new RuntimeException("请æ±é误ï¼statusCode:" + statusCode + ",Error Code: " + result.get("code") + ", Error Msg: " + result.get("msg")); |
| | | } else { |
| | | // å¤çè¿åç»æ |
| | | return result; |
| | | } |
| | | } |
| | | } |