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

eleme.openapi.sdk.oauth.impl.DefaultIOAuthClient Maven / Gradle / Ivy

There is a newer version: 1.30.71
Show newest version
package eleme.openapi.sdk.oauth.impl;


import eleme.openapi.sdk.config.Constants;
import eleme.openapi.sdk.config.Config;
import eleme.openapi.sdk.oauth.IOAuthClient;
import eleme.openapi.sdk.oauth.OAuthRequest;
import eleme.openapi.sdk.oauth.parser.OAuthParser;
import eleme.openapi.sdk.oauth.parser.ObjectJsonParser;
import eleme.openapi.sdk.oauth.response.ErrorResponse;
import eleme.openapi.sdk.utils.JacksonUtils;
import eleme.openapi.sdk.utils.WebUtils;

import java.io.IOException;

/**
 * 客户端模式获取Token
 */
public class DefaultIOAuthClient implements IOAuthClient {
    private int connectTimeout = 15000; // 默认连接超时时间为15秒
    private int readTimeout = 30000; // 默认响应超时时间为30秒
    private String serverHostUrl;
    private Config context;

    public DefaultIOAuthClient(Config context, String suffix) {
        this.context = context;
        this.serverHostUrl = context.getServerUrl() + suffix;
    }

    public  T execute(OAuthRequest request) {
        try {
            String respJson = WebUtils.doPost(context, serverHostUrl,
                    request.getBodyMap(),
                    Constants.CHARSET_UTF8,
                    WebUtils.getReqID(),
                    connectTimeout,
                    readTimeout,
                    request.getHeaderMap()
            );
            // 构建响应解释器
            return JacksonUtils.json2pojo(respJson, request.getResponseClass());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy