use of com.google.api.ads.adwords.lib.utils.BatchJobException in project googleads-java-lib by googleads.
the class AddCompleteCampaignsUsingBatchJob method main.
public static void main(String[] args) {
AdWordsSession session;
try {
// Generate a refreshable OAuth2 credential.
Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS).fromFile().build().generateCredential();
// Construct an AdWordsSession.
session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
} catch (ConfigurationLoadException cle) {
System.err.printf("Failed to load configuration from the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, cle);
return;
} catch (ValidationException ve) {
System.err.printf("Invalid configuration in the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, ve);
return;
} catch (OAuthException oe) {
System.err.printf("Failed to create OAuth credentials. Check OAuth settings in the %s file. " + "Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, oe);
return;
}
AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
try {
runExample(adWordsServices, session);
} catch (ApiException apiException) {
// ApiException is the base class for most exceptions thrown by an API request. Instances
// of this exception have a message and a collection of ApiErrors that indicate the
// type and underlying cause of the exception. Every exception object in the adwords.axis
// packages will return a meaningful value from toString
//
// ApiException extends RemoteException, so this catch block must appear before the
// catch block for RemoteException.
System.err.println("Request failed due to ApiException. Underlying ApiErrors:");
if (apiException.getErrors() != null) {
int i = 0;
for (ApiError apiError : apiException.getErrors()) {
System.err.printf(" Error %d: %s%n", i++, apiError);
}
}
} catch (RemoteException re) {
System.err.printf("Request failed unexpectedly due to RemoteException: %s%n", re);
} catch (InterruptedException ie) {
System.err.printf("Thread was interrupted: %s%n", ie);
} catch (TimeoutException te) {
System.err.printf("Job did not complete after status was polled %s times: %s%n", MAX_POLL_ATTEMPTS, te);
} catch (BatchJobException be) {
System.err.printf("Example failed due to BatchJobException: %s%n", be);
}
}
use of com.google.api.ads.adwords.lib.utils.BatchJobException in project googleads-java-lib by googleads.
the class AddKeywordsUsingIncrementalBatchJob method main.
public static void main(String[] args) {
AdWordsSession session;
try {
// Generate a refreshable OAuth2 credential.
Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS).fromFile().build().generateCredential();
// Construct an AdWordsSession.
session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
} catch (ConfigurationLoadException cle) {
System.err.printf("Failed to load configuration from the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, cle);
return;
} catch (ValidationException ve) {
System.err.printf("Invalid configuration in the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, ve);
return;
} catch (OAuthException oe) {
System.err.printf("Failed to create OAuth credentials. Check OAuth settings in the %s file. " + "Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, oe);
return;
}
AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
AddKeywordsUsingIncrementalBatchJobParams params = new AddKeywordsUsingIncrementalBatchJobParams();
if (!params.parseArguments(args)) {
// Either pass the required parameters for this example on the command line, or insert them
// into the code here. See the parameter class definition above for descriptions.
params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
}
try {
runExample(adWordsServices, session, params.adGroupId);
} catch (ApiException apiException) {
// ApiException is the base class for most exceptions thrown by an API request. Instances
// of this exception have a message and a collection of ApiErrors that indicate the
// type and underlying cause of the exception. Every exception object in the adwords.axis
// packages will return a meaningful value from toString
//
// ApiException extends RemoteException, so this catch block must appear before the
// catch block for RemoteException.
System.err.println("Request failed due to ApiException. Underlying ApiErrors:");
if (apiException.getErrors() != null) {
int i = 0;
for (ApiError apiError : apiException.getErrors()) {
System.err.printf(" Error %d: %s%n", i++, apiError);
}
}
} catch (RemoteException re) {
System.err.printf("Request failed unexpectedly due to RemoteException: %s%n", re);
} catch (InterruptedException ie) {
System.err.printf("Thread was interrupted: %s%n", ie);
} catch (TimeoutException te) {
System.err.printf("Job did not complete after status was polled %s times: %s%n", MAX_POLL_ATTEMPTS, te);
} catch (BatchJobException be) {
System.err.printf("Example failed due to BatchJobException: %s%n", be);
}
}
use of com.google.api.ads.adwords.lib.utils.BatchJobException in project googleads-java-lib by googleads.
the class BatchJobHelperImpl method downloadBatchJobMutateResponse.
@Override
public BatchJobMutateResponse downloadBatchJobMutateResponse(String downloadUrl, int startIndex, int numberResults) throws BatchJobException {
/*
* Deserialize using the generated cm.MutateResult class instead of the batchjob.MutateResult
* class. The MutateResult and ErrorList types in the batchjob package have properties defined
* in the BatchJobMutateResultInterface and BatchJobErrorListInterface interfaces, respectively.
* On some Hotspot JVMs, if java.beans.Introspector (used by Axis) encounters a property
* defined via an implemented interface, it will return a PropertyDescriptor where
* getPropertType() returns the interface class instead of the type declared in the
* implementing class (e.g., BatchJobErrorListInterface instead of ErrorList). This causes
* problems during Axis deserialization because Axis relies on the presence of a static
* getTypeDesc method on each property it encounters.
*/
List<com.google.api.ads.adwords.axis.v201809.cm.MutateResult> cmMutateResults;
try {
cmMutateResults = deserializer.deserializeBatchJobMutateResults(new URL(downloadUrl), getServiceTypeMappings(), com.google.api.ads.adwords.axis.v201809.cm.MutateResult.class, resultQName, startIndex, numberResults);
} catch (Exception e) {
batchJobLogger.logDownload(downloadUrl, null, e);
throw new BatchJobException("Failed to download batch job mutate response from URL: " + downloadUrl, e);
}
// Translate the cm.MutateResults into batchjob.MutateResults.
BatchJobMutateResponse response = new BatchJobMutateResponse();
List<MutateResult> mutateResults = Lists.newArrayList();
for (com.google.api.ads.adwords.axis.v201809.cm.MutateResult cmMutateResult : cmMutateResults) {
MutateResult mutateResult = new MutateResult();
mutateResult.setIndex(cmMutateResult.getIndex());
mutateResult.setOperand(cmMutateResult.getResult());
if (cmMutateResult.getErrorList() != null) {
mutateResult.setErrorList(new ErrorList());
if (cmMutateResult.getErrorList().getErrors() != null) {
mutateResult.getErrorList().setErrors(cmMutateResult.getErrorList().getErrors());
}
}
mutateResults.add(mutateResult);
}
response.setMutateResults(mutateResults.toArray(new MutateResult[mutateResults.size()]));
batchJobLogger.logDownload(downloadUrl, response, null);
return response;
}
use of com.google.api.ads.adwords.lib.utils.BatchJobException in project googleads-java-lib by googleads.
the class BatchJobHelperImpl method downloadBatchJobMutateResponse.
@Override
public BatchJobMutateResponse downloadBatchJobMutateResponse(String downloadUrl, int startIndex, int numberResults) throws BatchJobException {
try {
BatchJobMutateResponse response = deserializer.deserializeBatchJobMutateResults(BatchJobMutateResponse.class, new URL(downloadUrl), startIndex, numberResults);
batchJobLogger.logDownload(downloadUrl, response, null);
return response;
} catch (Exception e) {
batchJobLogger.logDownload(downloadUrl, null, e);
throw new BatchJobException("Failed to download batch job mutate response from URL: " + downloadUrl, e);
}
}
Aggregations