Commit afe81d26 authored by jim's avatar jim

ip

parent e7c4e436
...@@ -26,12 +26,8 @@ import org.slf4j.Logger; ...@@ -26,12 +26,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
...@@ -83,15 +79,14 @@ public class BrowserTask { ...@@ -83,15 +79,14 @@ public class BrowserTask {
return headers; return headers;
} }
public HttpHeaders buildPostHeader() { public Map<String, String> buildPostHeader() {
HttpHeaders header = new HttpHeaders(); Map<String, String> headers = new HashMap<>();
header.setContentType(MediaType.APPLICATION_JSON); headers.put("Content-Type", "application/json");
if (profiles.equals("dev") || profiles.equals("staging")) { if (profiles.equals("dev") || profiles.equals("staging"))
header.setBearerAuth("oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm"); headers.put("Authorization", "Bearer oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
} else if (profiles.equals("prod")) { else if (profiles.equals("prod"))
header.setBearerAuth("tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO"); headers.put("Authorization", "Bearer tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
} return headers;
return header;
} }
private IpChargeRequestDto buildIpChargeRequestDto(IpResource request, int chargeType, int payMethod) { private IpChargeRequestDto buildIpChargeRequestDto(IpResource request, int chargeType, int payMethod) {
...@@ -117,8 +112,7 @@ public class BrowserTask { ...@@ -117,8 +112,7 @@ public class BrowserTask {
if (ipResourceRepository.lockTask(ipResource)) { if (ipResourceRepository.lockTask(ipResource)) {
try { try {
boolean result = false; boolean result = false;
RestTemplate restTemplate = new RestTemplate(); Map<String, String> header = buildPostHeader();
HttpHeaders header = buildPostHeader();
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("name", ipResource.getUsername()); map.put("name", ipResource.getUsername());
map.put("region", ipResource.getRegion()); map.put("region", ipResource.getRegion());
...@@ -130,10 +124,11 @@ public class BrowserTask { ...@@ -130,10 +124,11 @@ public class BrowserTask {
map.put("startscript", ""); map.put("startscript", "");
map.put("ipkeptperiod", 7); map.put("ipkeptperiod", 7);
map.put("persistSystemDiskOnTermination", false); map.put("persistSystemDiskOnTermination", false);
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header); // HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header);
IpBuyResultDto ipBuyResultDto = null; IpBuyResultDto ipBuyResultDto = null;
try { try {
ipBuyResultDto = restTemplate.postForObject(URL + "/intelligroup/ipresources?accountId=browser", httpEntity, IpBuyResultDto.class); String requestResult = HttpClientutils.doPost(URL + "/intelligroup/ipresources?accountId=browser", header, JSONObject.toJSONString(map));
ipBuyResultDto = JSONObject.parseObject(requestResult, IpBuyResultDto.class);
if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) { if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) {
log.error("fail to buy ip"); log.error("fail to buy ip");
log.error(ipBuyResultDto.getErrorCode()); log.error(ipBuyResultDto.getErrorCode());
...@@ -358,6 +353,20 @@ public class BrowserTask { ...@@ -358,6 +353,20 @@ public class BrowserTask {
return responseBody.string(); return responseBody.string();
} }
public static String doPost(String url, Map<String, String> headers, String body) throws IOException {
Headers httpHeaders = Headers.of(headers);
Request request = new Request.Builder()
.post(RequestBody.create(body, okhttp3.MediaType.get("json")))
.url(url)
.headers(httpHeaders)
.build();
Call call = client.newCall(request);
Response response = call.execute();
ResponseBody responseBody = response.body();
return responseBody.string();
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment