办学质量监测教学评价系统
ageer
2025-04-08 5a5a48e153c4f161a4e01c8ec4b4b0ec8f5fa1d2
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.ruoyi.system.util;
 
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.ruoyi.common.core.service.ConfigService;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.concurrent.TimeUnit;
 
/**
 * @author WangLe
 */
@RequiredArgsConstructor
@Component
@Slf4j
public class AudioOkHttpUtil {
 
    private final ConfigService configService;
 
    private static final String AUTHORIZATION = "Authorization";
 
    private final OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(300, TimeUnit.SECONDS)
        .writeTimeout(300, TimeUnit.SECONDS)
        .readTimeout(300, TimeUnit.SECONDS)
        .build();
 
    public String executeRequest(Request request) {
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            return response.body() != null ? response.body().string() : null;
        } catch (IOException e) {
            // 这里应根据实际情况使用适当的日志记录方式
            log.error("请求失败: {}",e.getMessage());
            return null;
        }
    }
 
    public Request createPostRequest(String url, String json) {
        MediaType JSON = MediaType.get("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(json, JSON);
        return new Request.Builder()
            .url(getKey("apiHost") + url)
            .post(body)
            .header("Content-Type", "application/json")
            .header(AUTHORIZATION,  "Bearer "+getKey("apiKey"))
            .build();
    }
 
 
    public Request createGetRequest(String url) {
        return new Request.Builder()
            .url(getKey("apiHost") + url)
            .header(AUTHORIZATION, "Bearer "+getKey("apiKey"))
            .build();
    }
 
 
    public String getKey(String key) {
        return configService.getConfigValue("audio", key);
    }
}