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

com.qingxun.javasdkapi.clients.TextTransOpenApiClient 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.ITextTransOpenApiClient;
import com.qingxun.javasdkapi.common.ApiCode;
import com.qingxun.javasdkapi.common.UrlConstansts;
import com.qingxun.javasdkapi.request.TextTransRequest;
import com.qingxun.javasdkapi.response.ApiResponse;
import com.qingxun.javasdkapi.response.TextTransResponse;
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.Map;
import java.util.TreeMap;

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


/**
 * 文字翻译
 */
public class TextTransOpenApiClient extends CommonOpenApiClient implements ITextTransOpenApiClient {



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

    private IHttpUtil httpUtil = HttpClientUtil.DEFAULT;

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

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


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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy