package com.yami.shop.systemtest.util;
|
|
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpResponse;
|
import com.yami.shop.common.util.Json;
|
import com.yami.shop.systemtest.constants.Constant;
|
|
import java.util.List;
|
|
public class HttpUtil {
|
|
|
public static void get(String url) {
|
HttpResponse response = doGet(url);
|
CheckUtil.check(response);
|
}
|
|
public static <T> T get(String url, Class<T> clazz) {
|
HttpResponse response = doGet(url);
|
return CheckUtil.checkAndGetObject(response,clazz);
|
}
|
|
public static <T> List<T> getResultArray(String url, Class<T[]> clazz) {
|
HttpResponse response = doGet(url);
|
return CheckUtil.checkAndGetArray(response,clazz);
|
}
|
|
|
public static void post(String url, Object obj) {
|
HttpResponse response = doPost(url, obj);
|
CheckUtil.check(response);
|
}
|
|
public static <T> T post(String url, Object obj, Class<T> clazz) {
|
HttpResponse response = doPost(url, obj);
|
return CheckUtil.checkAndGetObject(response,clazz);
|
}
|
|
public static <T> List<T> postResultArray(String url, Object obj, Class<T[]> clazz) {
|
HttpResponse response = doPost(url, obj);
|
return CheckUtil.checkAndGetArray(response,clazz);
|
}
|
|
|
public static void put(String url, Object obj) {
|
HttpResponse response = doPut(url, obj);
|
CheckUtil.check(response);
|
}
|
|
public static <T> T doPut(String url, Object obj, Class<T> clazz) {
|
HttpResponse response = doPut(url, obj);
|
return CheckUtil.checkAndGetObject(response,clazz);
|
}
|
|
public static <T> List<T> putResultArray(String url, Object obj, Class<T[]> clazz) {
|
HttpResponse response = doPut(url, obj);
|
return CheckUtil.checkAndGetArray(response,clazz);
|
}
|
|
|
public static void delete(String url) {
|
HttpResponse response = doDelete(url);
|
CheckUtil.check(response);
|
}
|
|
public static <T> T delete(String url, Class<T> clazz) {
|
HttpResponse response = doDelete(url);
|
return CheckUtil.checkAndGetObject(response,clazz);
|
}
|
|
public static <T> List<T> deleteResultArray(String url, Class<T[]> clazz) {
|
HttpResponse response = doDelete(url);
|
return CheckUtil.checkAndGetArray(response,clazz);
|
}
|
|
|
|
private static HttpResponse doGet(String url) {
|
HttpRequest postRequest = HttpRequest.get(url);
|
setHeader(postRequest);
|
return postRequest.execute();
|
}
|
|
private static HttpResponse doPost(String url, Object obj) {
|
HttpRequest postRequest = HttpRequest.post(url);
|
setHeader(postRequest);
|
return postRequest
|
.body(Json.toJsonString(obj))
|
.execute();
|
}
|
|
private static HttpResponse doPut(String url, Object obj) {
|
HttpRequest postRequest = HttpRequest.put(url);
|
setHeader(postRequest);
|
return postRequest
|
.body(Json.toJsonString(obj))
|
.execute();
|
}
|
|
private static HttpResponse doDelete(String url) {
|
HttpRequest postRequest = HttpRequest.get(url);
|
setHeader(postRequest);
|
return postRequest.execute();
|
}
|
|
private static HttpRequest setHeader(HttpRequest httpRequest) {
|
String url = httpRequest.getUrl();
|
if (url.startsWith(Constant.API_URL)) {
|
httpRequest.header("authorization", Constant.API_TOKEN);
|
}
|
if (url.startsWith(Constant.SHOP_URL)) {
|
httpRequest.header("authorization", Constant.SHOP_TOKEN);
|
}
|
if (url.startsWith(Constant.PLATFORM_URL)) {
|
httpRequest.header("authorization", Constant.PLATFORM_TOKEN);
|
}
|
return httpRequest;
|
}
|
}
|