1
zj
2024-07-29 f6f3df18ea57ea4128fcccf3282e1520e867c631
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.example.common;
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
 
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
public class MexcMarketDataClient {
 
    private static final String REQUEST_HOST = "https://api.mexc.com";
    private static final OkHttpClient OK_HTTP_CLIENT = createOkHttpClient();
 
    private static OkHttpClient createOkHttpClient() {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .connectTimeout(45, TimeUnit.SECONDS)
                .readTimeout(45, TimeUnit.SECONDS)
                .writeTimeout(45, TimeUnit.SECONDS)
                //.addInterceptor(httpLoggingInterceptor)
                .build();
    }
 
 
    public static <T> T get(String uri, Map<String, String> params, TypeReference<T> ref) {
        try {
            Request.Builder builder = new Request.Builder().url(REQUEST_HOST + uri + "?" + SignatureUtil.toQueryString(params)).get();
            Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute();
            Gson gson = new Gson();
            assert response.body() != null;
            String content = response.body().string();
            return gson.fromJson(content, ref.getType());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}