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();
}
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();
}
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);
}
}
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);
}
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);
}
}
Aggregations