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 get(String url, Class clazz) { HttpResponse response = doGet(url); return CheckUtil.checkAndGetObject(response,clazz); } public static List getResultArray(String url, Class 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 post(String url, Object obj, Class clazz) { HttpResponse response = doPost(url, obj); return CheckUtil.checkAndGetObject(response,clazz); } public static List postResultArray(String url, Object obj, Class 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 doPut(String url, Object obj, Class clazz) { HttpResponse response = doPut(url, obj); return CheckUtil.checkAndGetObject(response,clazz); } public static List putResultArray(String url, Object obj, Class 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 delete(String url, Class clazz) { HttpResponse response = doDelete(url); return CheckUtil.checkAndGetObject(response,clazz); } public static List deleteResultArray(String url, Class 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; } }