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

com.qingxun.javasdkapi.clients.ImageTransOpenApiClient 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.IImageTransOpenApiClient;
import com.qingxun.javasdkapi.common.ApiCode;
import com.qingxun.javasdkapi.common.UrlConstansts;
import com.qingxun.javasdkapi.request.DownloadImageRequest;
import com.qingxun.javasdkapi.request.QueryTransImageProgressRequest;
import com.qingxun.javasdkapi.request.UploadTransImageRequest;
import com.qingxun.javasdkapi.response.ApiResponse;
import com.qingxun.javasdkapi.response.DownloadImageResponse;
import com.qingxun.javasdkapi.response.QueryTransImageProgressResponse;
import com.qingxun.javasdkapi.response.UploadTransImageResponse;
import com.qingxun.javasdkapi.utils.HttpClientUtil;
import com.qingxun.javasdkapi.utils.IHttpUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

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

/**
 * 图片翻译
 */
public class ImageTransOpenApiClient extends CommonOpenApiClient implements IImageTransOpenApiClient {


    private IHttpUtil httpUtil = HttpClientUtil.DEFAULT;

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

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

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


    @Override
    public UploadTransImageResponse excute(UploadTransImageRequest uploadTransImageRequest) {
        String urlResult = "";
        if (uploadTransImageRequest.getFile() !=null ){
            urlResult = excuteFile(uploadTransImageRequest);
        }else{
            urlResult = excuteBytes(uploadTransImageRequest);
        }
        UploadTransImageResponse translateImageResponse = new UploadTransImageResponse();
        try {
            ApiResponse> baseResponse = objectMapper.readValue(urlResult, ApiResponse.class);
            ApiCode apiCode = ApiCode.getByCode(baseResponse.getCode());
            translateImageResponse.setApiCode(baseResponse.getCode(), baseResponse.getMsg());
            if(apiCode != ApiCode.OK && apiCode != ApiCode.OK2){
                return translateImageResponse;
            }
            if ( baseResponse.getData() != null && baseResponse.getData().containsKey("tid")) {
                translateImageResponse.setTid(baseResponse.getData().get("tid"));
            }else if ( baseResponse.getData() != null && baseResponse.getData().containsKey("taskId")) {
                translateImageResponse.setTid(baseResponse.getData().get("taskId"));
            }
            return translateImageResponse;
        } catch (IOException e) {
            e.printStackTrace();
        }
        translateImageResponse.setApiCode(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMessage());
        return translateImageResponse;
    }

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

    private String excuteBytes(UploadTransImageRequest uploadTransImageRequest){
        TreeMap treeMap = uploadTransImageRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        String urlResult="";
        try {
            urlResult = httpUtil.post(UrlConstansts.IMAGE_UPLOAD_URL,treeMap,uploadTransImageRequest.getFileBytes(),uploadTransImageRequest.getFileName());
            log.info("url: {}\n参数:{}\n结果{}", UrlConstansts.IMAGE_UPLOAD_URL, objectMapper.writeValueAsString(treeMap), urlResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlResult;
    }



    @Override
    public QueryTransImageProgressResponse excute(QueryTransImageProgressRequest queryTransImageProgressRequest) {
        TreeMap treeMap = queryTransImageProgressRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        QueryTransImageProgressResponse queryTransImageProgressResponse = new QueryTransImageProgressResponse();
        String urlResult = httpUtil.post(UrlConstansts.IMAGE_TRANS_PROGRES_URL, treeMap);
        try {
            log.info("url: {}\n参数:{}\n结果{}", UrlConstansts.IMAGE_TRANS_PROGRES_URL, objectMapper.writeValueAsString(treeMap), urlResult);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        ApiResponse> baseResponse = null;
        try {
            baseResponse = objectMapper.readValue(urlResult, ApiResponse.class);
            ApiCode apiCode = ApiCode.getByCode(baseResponse.getCode());
            queryTransImageProgressResponse.setApiCode(baseResponse.getCode(), baseResponse.getMsg());
            if (apiCode != ApiCode.OK && apiCode != ApiCode.OK2) {
                return queryTransImageProgressResponse;
            }
            Map data = baseResponse.getData();
            if (data == null || data.size() == 0) {
                return queryTransImageProgressResponse;
            }
            BeanUtils.populate(queryTransImageProgressResponse, data);
            return queryTransImageProgressResponse;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        queryTransImageProgressResponse.setApiCode(ApiCode.SYSTEM_ERROR.getCode(), baseResponse.getMsg());
        return queryTransImageProgressResponse;
    }

    @Override
    public DownloadImageResponse excute(DownloadImageRequest downloadImageRequest) {
        TreeMap treeMap = downloadImageRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        IHttpUtil.HttpFileResponse res = httpUtil.formResponseStream(UrlConstansts.IMAGE_DOWNDLOAD__URL,treeMap);
        DownloadImageResponse downloadImageResponse = new DownloadImageResponse();
        if (res.getBytes() !=null && res.getBytes().length > 0 ) {
                downloadImageResponse.setBytes(res.getBytes());
                downloadImageResponse.setApiCode(ApiCode.OK.getCode(), ApiCode.OK.getMessage());
                return downloadImageResponse;
        }
        ApiResponse baseResponse = null;
        try {
            baseResponse = objectMapper.readValue(res.getBody(), ApiResponse.class);
            downloadImageResponse.setApiCode(baseResponse.getCode(), baseResponse.getMsg());
            return downloadImageResponse;
        } catch (IOException e) {
            e.printStackTrace();
        }
        downloadImageResponse.setApiCode(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMessage());
        return downloadImageResponse;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy