Google Ads Developer Blog: Using SelectorBuilder for the AdWords API Java lib...: As part of the latest Java client library, the SelectorBuilder utility class facilitates the creation of the generic selectors that are needed to query the AdWords API.
Because of the nature of the generic selectors, some boilerplate code
is needed in order to instantiate the selector objects properly. Also
the way that a selector is configured depends on the implementation that
is being used.
With the new SelectorBuilder, you have less boilerplate code and can still chain method calls and edit selectors without needing to know all the details of the generic Selector class. Let’s take a look at some examples comparing the current way of creating selectors, and the way it is with the utility class.
NOTE: Using the SelectorBuilder class to create selectors does not restrict the user from editing the selector after it has been built.
NOTE: To add a new field to the selection, you must call the method fields passing all fields that were used before, plus the one that you want to add:
All configurations available in the selector can be set using the SelectorBuilder utility. Looking at the GetKeywords example, there is sample code on how to add predicates to the selector:
SelectorBuilder makes available all possible predicate operators as utility methods:
If you have any questions about the client libraries, you can post them on our forums. Check out our Google+ page for client library and Ads APIs updates.
With the new SelectorBuilder, you have less boilerplate code and can still chain method calls and edit selectors without needing to know all the details of the generic Selector class. Let’s take a look at some examples comparing the current way of creating selectors, and the way it is with the utility class.
NOTE: Using the SelectorBuilder class to create selectors does not restrict the user from editing the selector after it has been built.
Some examples
The SelectorBuilder class is just a builder-like object that can be chained in order to simplify the code needed to create the predicates and fields used with the API. Let’s now take a look at how the selector is created in the GetCampaigns example (available on GitHub):You still need to pass all the field names as strings, but all the boilerplate code involved in setting everything on the selector is handled by the SelectorBuilder. More than that, the methods have the same signature for Axis and JAX-WS.SelectorBuilder builder = new SelectorBuilder().fields("Id", "Name").orderAscBy("Name").offset(0).limit(20).build();
NOTE: To add a new field to the selection, you must call the method fields passing all fields that were used before, plus the one that you want to add:
This will return the same Selector as previously, but with the field status added to the selection. (IMPORTANT: The previously created Selector DOES NOT change when making additional method calls to the builder).builder.fields("Id","Name",// new field to the selection"status");
All configurations available in the selector can be set using the SelectorBuilder utility. Looking at the GetKeywords example, there is sample code on how to add predicates to the selector:
More examples are available in the client library repository in GitHub.SelectorBuilder builder = new SelectorBuilder().fields("Id", "AdGroupId", "MatchType","KeywordText").orderAscBy("AdGroupId").offset(0).limit(20).in("AdGroupId", adGroupId.toString()).equals("CriteriaType", "KEYWORD").build();
Some additional considerations
It is important to remember that the builder returns a Selector, so all its accompanying features are still present, even if you use the builder to create it.SelectorBuilder makes available all possible predicate operators as utility methods:
builder.equals(“<field_name>”, value).notEquals(“<field_name>”, value).contains(“<field_name>”, value).containsIgnoreCase(“<field_name>”, value).doesNotcontain(“<field_name>”, value).doesNotContainIgnoreCase(“<field_name>”, value).in(“<field_name>”, collection).notIn(“<field_name>”, collection).greaterThan(“<field_name>”, value).greaterThanEquals(“<field_name>”, value).lessThan(“<field_name>”, value).lessThanEquals(“<field_name>”, value);
There are also additional ordering, paging and date range methods:
The start and end dates are Joda-Time dates, so it is easy to add and remove days from them. The builder will parse dates according to the API format.builder.orderAscBy(“<field_name>”).orderDescBy(“<field_name>”).offset(offset).limit(amount).forDateRange(start, end);
If you have any questions about the client libraries, you can post them on our forums. Check out our Google+ page for client library and Ads APIs updates.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.