Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debedb/3313498c2aa6c1e9dd15d4615f69e11f to your computer and use it in GitHub Desktop.
Save debedb/3313498c2aa6c1e9dd15d4615f69e11f to your computer and use it in GitHub Desktop.
Generic usage of Google Ads API using reflection
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.ads.googleads.v6.services.GoogleAdsVersion;
import com.google.api.gax.core.BackgroundResource;
import com.google.protobuf.AbstractMessage;
public class GoogleUtils {
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
private <GoogleEntity extends AbstractMessage> void executeGoogleOperation(
Class<? extends AbstractMessage> googleEntityType,
Map<String, Object> fields) throws Throwable {
final String entityName = googleEntityType.getSimpleName();
Method curMethod = null;
BackgroundResource svcCli = null;
Object instance = null;
try {
// Get the service client (e.g., CampaignServiceClient)
String svcCliCreator = "create" + entityName + "ServiceClient";
curMethod = googleClient.getClass().getMethod(svcCliCreator);
instance = googleClient;
AccessibleObject.setAccessible(new AccessibleObject[] {curMethod}, true);
svcCli = (BackgroundResource) curMethod.invoke(instance);
Class svcCliClass = svcCli.getClass();
// Get builder creation method (e.g., Campaign.newBuilder()
curMethod = googleEntityType.getMethod("newBuilder");
// Instance is builder for the googleEntity
instance = curMethod.invoke(null);
Class builderClass = instance.getClass();
// Build the instance of GoogleEntity
// Method [] builderMethods = builderClass.getMethods();
for (String field : fields.keySet()) {
String setterName = "set" + Character.toUpperCase(field.charAt(0)) + field.substring(1);
Object val = fields.get(field);
curMethod = builderClass.getMethod(setterName, val.getClass());
instance = curMethod.invoke(instance, val);
}
curMethod = builderClass.getMethod("build");
GoogleEntity googleEntity = (GoogleEntity) curMethod.invoke(instance);
String resourcePkg = googleEntityType.getPackage().getName();
String servicePkg = resourcePkg.substring(0, resourcePkg.lastIndexOf(".")) + ".services";
Class opClass = Class.forName(servicePkg + "." + entityName + "Operation");
// Ops builder
curMethod = opClass.getMethod("newBuilder");
// instance is builder for operation
instance = curMethod.invoke(instance);
builderClass = instance.getClass();
curMethod = builderClass.getMethod("setCreate", googleEntityType);
instance = curMethod.invoke(instance, googleEntity);
// get the builder method
curMethod = builderClass.getMethod("build");
// instance is the entity operation
instance = curMethod.invoke(instance);
List ops = new ArrayList();
ops.add(instance);
// get the mutator method
curMethod = svcCliClass.getMethod("mutate" + entityName + "s", String.class, List.class);
// invoke mutator
Object mutateResponse = curMethod.invoke(svcCli, customerId, ops);
// get getResultsList() method
curMethod = mutateResponse.getClass().getMethod("getResultsList");
List responses = (List) curMethod.invoke(mutateResponse);
String resourceName = (String) responses.get(0);
} finally {
if (svcCli != null) {
try {
svcCli.close();
} catch (Exception e) {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment