doum
7 小时以前 36f691267e45ca2861bed663fdcf5f2efcefdfce
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
package com.doumee.core.jiandaoyun.api.jdy;
 
import  com.doumee.core.jiandaoyun.model.file.UploadTokenParam;
import  com.doumee.core.jiandaoyun.model.http.ApiClient;
import  com.doumee.core.jiandaoyun.model.http.HttpRequestParam;
import org.apache.commons.lang3.StringUtils;
 
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import static com.doumee.core.jiandaoyun.constants.HttpConstant.FORM_BASE_PATH;
 
/**
 * 文件相关接口
 */
public class FileApiClient extends ApiClient {
 
    private static final String DEFAULT_VERSION = "v5";
    private static final List<String> VALID_VERSION_LIST = Collections.singletonList("v5");
 
    public FileApiClient(String apiKey, String host) {
        super(apiKey, host);
        this.setDefaultVersion(DEFAULT_VERSION);
        this.setValidVersionList(VALID_VERSION_LIST);
    }
 
    @Override
    public String generatePath(String version, String path) {
        return super.getValidVersion(version) + FORM_BASE_PATH + path;
    }
 
    /**
     * 获取文件上传凭证和上传地址接口
     *
     * @param uploadTokenParam - 文件对应的信息
     * @return token和url信息
     */
    public Map<String, Object> uploadToken(UploadTokenParam uploadTokenParam, String version) throws Exception {
        if (uploadTokenParam == null || !uploadTokenParam.isValid()) {
            throw new RuntimeException("param lack!");
        }
        String path = this.generatePath(version, "file/get_upload_token");
        // 请求参数
        Map<String, Object> data = new HashMap<>();
        data.put("transaction_id", uploadTokenParam.getTransactionId());
        data.put("app_id", uploadTokenParam.getAppId());
        data.put("entry_id", uploadTokenParam.getEntryId());
        HttpRequestParam param = new HttpRequestParam(path, data);
        return this.sendPostRequest(param);
    }
 
    public Map<String, Object> uploadFile(String url, String token, File file) throws Exception {
        if (StringUtils.isBlank(url) || StringUtils.isBlank(token) || file == null) {
            throw new RuntimeException("param lack!");
        }
        return this.httpPostFile(url, token, file);
    }
}