Search in sources :

Example 1 with ApiException

use of com.hyq0719.mktapi.common.exception.ApiException in project marketing-api-java-sdks by Hyq0719.

the class VivoAuthorizer method authorize.

@Override
public Result<AuthToken> authorize(String advertiserId, String authCode) {
    VivoProperties.VivoOAuth2Config config = vivoProperties.getConfigs().get(advertiserId);
    if (config == null) {
        return Result.ofFail();
    }
    if (isDebugMode()) {
        return Util.authorizeForTest(advertiserId, authCode, channel());
    }
    Oauth2TokenRequest request = new Oauth2TokenRequest();
    request.clientId(Integer.valueOf(config.getClientId())).clientSecret(config.getSecret()).grantType("code").code(authCode);
    try {
        VivoResponse<Oauth2TokenResponseData> response = vivoSdkService.getTokenApi().oauth2Token().execute(request);
        if (!response.isSuccessful()) {
            return Result.ofFail();
        }
        Oauth2TokenResponseData data = response.getData();
        AuthToken authToken = new AuthToken();
        authToken.setChannel(channel());
        authToken.setAdvertiserId(advertiserId);
        authToken.setAccessToken(data.getAccessToken());
        authToken.setRefreshToken(data.getRefreshToken());
        long now = System.currentTimeMillis();
        authToken.setCreateTime(now);
        authToken.setRefreshTime(now);
        return Result.ofSuccessful(authToken);
    } catch (ApiException e) {
        e.printStackTrace();
    }
    return Result.ofFail();
}
Also used : Oauth2TokenResponseData(com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenResponseData) Oauth2TokenRequest(com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenRequest) AuthToken(test.auth.bean.AuthToken) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Example 2 with ApiException

use of com.hyq0719.mktapi.common.exception.ApiException in project marketing-api-java-sdks by Hyq0719.

the class OceanAuthorizer method refresh.

@Override
public Result<AuthToken> refresh(AuthToken authToken) {
    OceanProperties.OceanOAuth2Config config = oceanProperties.getConfigs().get(authToken.getAdvertiserId());
    if (config == null) {
        return Result.ofFail();
    }
    if (isDebugMode()) {
        return Util.refreshForTest(authToken);
    }
    RefreshTokenRequest request = new RefreshTokenRequest();
    request.appId(Long.valueOf(config.getClientId())).secret(config.getSecret()).refreshToken(authToken.getRefreshToken()).grantType("auth_code");
    try {
        OceanResponse<RefreshTokenResponseData> response = oceanSdkService.getTokenApi().refreshToken().execute(request);
        if (!response.isSuccessful()) {
            return Result.ofFail();
        }
        RefreshTokenResponseData data = response.getData();
        AuthToken newAuthToken = authToken.newToken(data.getAccessToken(), data.getRefreshToken());
        return Result.ofSuccessful(newAuthToken);
    } catch (ApiException e) {
        e.printStackTrace();
    }
    return Result.ofFail();
}
Also used : RefreshTokenResponseData(com.hyq0719.mktapi.oceanengine.bean.token.RefreshTokenResponseData) RefreshTokenRequest(com.hyq0719.mktapi.oceanengine.bean.token.RefreshTokenRequest) AuthToken(test.auth.bean.AuthToken) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Example 3 with ApiException

use of com.hyq0719.mktapi.common.exception.ApiException in project marketing-api-java-sdks by Hyq0719.

the class OkhttpHttpHandler method downloadFileFromResponse.

public File downloadFileFromResponse(Response response) throws ApiException {
    try {
        File file = prepareDownloadFile(response);
        BufferedSink sink = Okio.buffer(Okio.sink(file));
        sink.writeAll(response.body().source());
        sink.close();
        return file;
    } catch (IOException e) {
        throw new ApiException(e);
    }
}
Also used : BufferedSink(okio.BufferedSink) IOException(java.io.IOException) File(java.io.File) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Example 4 with ApiException

use of com.hyq0719.mktapi.common.exception.ApiException in project marketing-api-java-sdks by Hyq0719.

the class ApiRequest method executeWithHttp.

protected ApiResponse<R> executeWithHttp(T t, String token) throws ApiException {
    RequestParam param = constructParameters(t, token);
    paramValidate(t);
    updateParamsForAuth(param);
    return getApiClient().execute(param, localVarReturnType);
}
Also used : RequestParam(com.hyq0719.mktapi.common.executor.parameter.RequestParam)

Example 5 with ApiException

use of com.hyq0719.mktapi.common.exception.ApiException in project marketing-api-java-sdks by Hyq0719.

the class ApacheHttpHandler method deserialize.

/**
 * Deserialize response body to Java object, according to the return type and the Content-Type
 * response header.
 *
 * @param <T>        Type
 * @param response   HTTP response
 * @param returnType The type of the Java object
 * @return The deserialized Java object
 * @throws ApiException If fail to deserialize response body, i.e. cannot read response body or
 *                      the Content-Type of the response is not supported.
 */
public <T> T deserialize(HttpResponse response, Type returnType) throws ApiException {
    if (response == null || returnType == null) {
        return null;
    }
    String respBody;
    try {
        if (response.getEntity() != null) {
            respBody = EntityUtils.toString(response.getEntity(), DEFAULT_ENCODER);
        } else {
            respBody = null;
        }
    } catch (IOException e) {
        throw new ApiException(e);
    }
    if (respBody == null || "".equals(respBody)) {
        return null;
    }
    String contentType = getHeaderValue(response.getAllHeaders(), "Content-Type");
    if (contentType == null) {
        // ensuring a default content type
        contentType = "application/json";
    }
    if (isJsonMime(contentType)) {
        return getJSON().deserialize(respBody, returnType);
    } else if (returnType.equals(String.class)) {
        // Expecting string, return the raw response body.
        return (T) respBody;
    } else {
        throw new ApiException("Content type \"" + contentType + "\" is not supported for type: " + returnType, response.getStatusLine().getStatusCode(), toMultimap(response.getAllHeaders()), respBody);
    }
}
Also used : IOException(java.io.IOException) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Aggregations

ApiException (com.hyq0719.mktapi.common.exception.ApiException)12 IOException (java.io.IOException)6 AuthToken (test.auth.bean.AuthToken)6 File (java.io.File)3 ApiResponse (com.hyq0719.mktapi.common.ApiResponse)2 OauthTokenRequest (com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenRequest)2 OauthTokenResponseData (com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenResponseData)2 RequestParam (com.hyq0719.mktapi.common.executor.parameter.RequestParam)1 FileImageAdRequest (com.hyq0719.mktapi.oceanengine.bean.material.FileImageAdRequest)1 AccessTokenRequest (com.hyq0719.mktapi.oceanengine.bean.token.AccessTokenRequest)1 AccessTokenResponseData (com.hyq0719.mktapi.oceanengine.bean.token.AccessTokenResponseData)1 RefreshTokenRequest (com.hyq0719.mktapi.oceanengine.bean.token.RefreshTokenRequest)1 RefreshTokenResponseData (com.hyq0719.mktapi.oceanengine.bean.token.RefreshTokenResponseData)1 Oauth2RefreshTokenRequest (com.hyq0719.mktapi.vivo.bean.token.Oauth2RefreshTokenRequest)1 Oauth2RefreshTokenResponseData (com.hyq0719.mktapi.vivo.bean.token.Oauth2RefreshTokenResponseData)1 Oauth2TokenRequest (com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenRequest)1 Oauth2TokenResponseData (com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenResponseData)1 BufferedSink (okio.BufferedSink)1 BasicHeader (org.apache.http.message.BasicHeader)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1