Commit afe81d26 authored by jim's avatar jim

ip

parent e7c4e436
......@@ -26,12 +26,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.time.Instant;
......@@ -83,15 +79,14 @@ public class BrowserTask {
return headers;
}
public HttpHeaders buildPostHeader() {
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
if (profiles.equals("dev") || profiles.equals("staging")) {
header.setBearerAuth("oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
} else if (profiles.equals("prod")) {
header.setBearerAuth("tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
}
return header;
public Map<String, String> buildPostHeader() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
if (profiles.equals("dev") || profiles.equals("staging"))
headers.put("Authorization", "Bearer oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
else if (profiles.equals("prod"))
headers.put("Authorization", "Bearer tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
return headers;
}
private IpChargeRequestDto buildIpChargeRequestDto(IpResource request, int chargeType, int payMethod) {
......@@ -117,8 +112,7 @@ public class BrowserTask {
if (ipResourceRepository.lockTask(ipResource)) {
try {
boolean result = false;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = buildPostHeader();
Map<String, String> header = buildPostHeader();
HashMap<String, Object> map = new HashMap<>();
map.put("name", ipResource.getUsername());
map.put("region", ipResource.getRegion());
......@@ -130,10 +124,11 @@ public class BrowserTask {
map.put("startscript", "");
map.put("ipkeptperiod", 7);
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;
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())) {
log.error("fail to buy ip");
log.error(ipBuyResultDto.getErrorCode());
......@@ -358,6 +353,20 @@ public class BrowserTask {
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