package com.cool.store.sdk.ec;

import com.alibaba.fastjson.JSON;
import com.cool.store.sdk.ec.core.EcRequest;
import com.cool.store.sdk.ec.core.EcResponse;
import com.cool.store.utils.Get;
import com.cool.store.utils.MapUtil;
import com.cool.store.utils.Post;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;

public class EcClient {




    @SneakyThrows
    public <T extends EcResponse> T exec(String baseUrl,EcRequest<T> request) {
        String apiUrl = request.getApiUrl();
//        if (accessToken != null) {
//            apiUrl += "?token=" + accessToken;
//        }
        String method = request.getMethod();
        String result = "";
        if (method.equals("GET")) {
            result = Get.send(baseUrl+apiUrl, MapUtil.entityToUrlParam(request.getParameter()));
        } else {
            HashMap<String,String> headers = new HashMap<>(1);
            headers.put("Content-Type", "application/json");
            result = Post.send(baseUrl+apiUrl, JSON.toJSONString(request.getParameter()), headers);
        }

        return JSON.parseObject(result, request.getResponseClass());
    }
}
