All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qingxun.javasdkapi.clients.AudioTransOpenApiClient Maven / Gradle / Ivy

Go to download

openapi SDK for Java Copyright © 2023 杭州轻寻科技 All rights reserved. 版权所有 (C)杭州轻寻科技 http://open.qingxun.com

The newest version!
package com.qingxun.javasdkapi.clients;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.qingxun.javasdkapi.clients.interfaces.CommonOpenApiClient;
import com.qingxun.javasdkapi.clients.interfaces.IAudioTransOpenApiClient;
import com.qingxun.javasdkapi.common.ApiCode;
import com.qingxun.javasdkapi.common.UrlConstansts;
import com.qingxun.javasdkapi.request.AudioGetDownloadUrlRequest;
import com.qingxun.javasdkapi.request.AudioQueryProgressRequest;
import com.qingxun.javasdkapi.request.AudioUploadTransRequest;
import com.qingxun.javasdkapi.response.*;
import com.qingxun.javasdkapi.utils.HttpClientUtil;
import com.qingxun.javasdkapi.utils.IHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import static com.qingxun.javasdkapi.common.JsonUtil.objectMapper;

/**
 * 音频识别翻译
 */
public class AudioTransOpenApiClient extends CommonOpenApiClient implements IAudioTransOpenApiClient {



    private IHttpUtil httpUtil = HttpClientUtil.DEFAULT;

    private Logger log = LoggerFactory.getLogger(AudioTransOpenApiClient.class);

    public AudioTransOpenApiClient(String appId, String privateKey) {
        super(appId, privateKey);
    }

    public AudioTransOpenApiClient(String appId, String privateKey, IHttpUtil iHttpUtil) {
        super(appId, privateKey);
        this.httpUtil = iHttpUtil;
    }


    @Override
    public AudioUploadTransResponse excute(AudioUploadTransRequest audioUploadTransRequest) {
        String urlResult = "";
        if(audioUploadTransRequest.getFile() != null){
            urlResult = excuteFile( audioUploadTransRequest);
        }else{
            urlResult = excuteBytes(audioUploadTransRequest);
        }

        AudioUploadTransResponse uploadResponse = new AudioUploadTransResponse();
        try {
            ApiResponse baseResponse = objectMapper.readValue(urlResult, ApiResponse.class);
            uploadResponse.setApiCode(baseResponse.getCode(), baseResponse.getMsg());
            ApiCode apiCode = ApiCode.getByCode(baseResponse.getCode());
            if (apiCode == ApiCode.OK && null != baseResponse.getData()) {
                uploadResponse.setRecordId(baseResponse.getData());
            }
            return uploadResponse;
        } catch (IOException e) {
            e.printStackTrace();
        }
        uploadResponse.setApiCode(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMessage());
        return uploadResponse;
    }

    private String excuteFile(AudioUploadTransRequest audioUploadTransRequest){
        TreeMap treeMap = audioUploadTransRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        String urlResult = httpUtil.post(UrlConstansts.AUDIO_UPLOAD_URL,treeMap,audioUploadTransRequest.getFile());
        treeMap.remove("file");
        try {
            log.info("url: {}\n参数:{}\n结果{}", UrlConstansts.AUDIO_UPLOAD_URL, objectMapper.writeValueAsString(treeMap), urlResult);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return urlResult;
    }

    private String excuteBytes(AudioUploadTransRequest audioUploadTransRequest){
        TreeMap treeMap = audioUploadTransRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        String result = "";
        try {
            result = httpUtil.post(UrlConstansts.AUDIO_UPLOAD_URL,treeMap, audioUploadTransRequest.getFileBytes(), audioUploadTransRequest.getFileName());
            try {
                log.info("url: {}\n参数:{}\n结果{}", UrlConstansts.AUDIO_UPLOAD_URL, objectMapper.writeValueAsString(treeMap), result);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public AudioQueryProcessResponse excute(AudioQueryProgressRequest audioQueryProgressRequest) {
        TreeMap treeMap = audioQueryProgressRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        AudioQueryProcessResponse transProgressResponse = new AudioQueryProcessResponse();
        String urlResult = httpUtil.post(UrlConstansts.AUDIO_QUERY_PROCESS_URL, treeMap);
        try {
            log.info("url: {}\n参数: {}\n 结果{}", UrlConstansts.AUDIO_QUERY_PROCESS_URL, objectMapper.writeValueAsString(treeMap), urlResult);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        ApiResponse> baseResponse = null;
        try {
            baseResponse = objectMapper.readValue(urlResult, ApiResponse.class);
            transProgressResponse.setApiCode(baseResponse.getCode(), baseResponse.getMsg());
            ApiCode apiCode = ApiCode.getByCode(baseResponse.getCode());
            if (apiCode != ApiCode.OK && apiCode != ApiCode.OK2) {
                return transProgressResponse;
            }
            Map data = baseResponse.getData();
            if (data == null || data.size() == 0) {
                return transProgressResponse;
            }
            if (data.containsKey("percent")) {
                transProgressResponse.setPercent(data.get("percent").toString());
            }
            if (data.containsKey("status")) {
                transProgressResponse.setStatus(data.get("status").toString());
            }
            return transProgressResponse;
        } catch (IOException e) {
            e.printStackTrace();
        }
        transProgressResponse.setApiCode(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMessage());
        return transProgressResponse;
    }

    @Override
    public AudioGetDownloadUrlResponse excute(AudioGetDownloadUrlRequest audioGetDownloadUrlRequest) {
        TreeMap treeMap = audioGetDownloadUrlRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        AudioGetDownloadUrlResponse transProgressResponse = new AudioGetDownloadUrlResponse();
        String urlResult = httpUtil.post(UrlConstansts.AUDIO_GET_DOWNLOAD_URL, treeMap);
        try {
            log.info("url: {}\n参数: {}\n 结果{}", UrlConstansts.AUDIO_GET_DOWNLOAD_URL, objectMapper.writeValueAsString(treeMap), urlResult);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        ApiResponse baseResponse = null;
        try {
            baseResponse = objectMapper.readValue(urlResult, ApiResponse.class);
            transProgressResponse.setApiCode(baseResponse.getCode(), baseResponse.getMsg());
            ApiCode apiCode = ApiCode.getByCode(baseResponse.getCode());
            if (apiCode != ApiCode.OK && apiCode != ApiCode.OK2) {
                return transProgressResponse;
            }
            transProgressResponse.setUrl(baseResponse.getData());
            return transProgressResponse;
        } catch (IOException e) {
            e.printStackTrace();
        }
        transProgressResponse.setApiCode(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMessage());
        return transProgressResponse;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy