Generic usage of Google Ads API using reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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