com.qingxun.javasdkapi.clients.AudioTransOpenApiClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk-api Show documentation
Show all versions of java-sdk-api Show documentation
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy