Skip to content

Commit d33754b

Browse files
authored
Add ResourceNames utility. (#34)
1 parent 831f82c commit d33754b

5 files changed

Lines changed: 354 additions & 8 deletions

File tree

google-ads-examples/src/main/java/com/google/ads/googleads/examples/basicoperations/AddKeywords.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.ads.googleads.examples.utils.CodeSampleParams;
2020
import com.google.ads.googleads.lib.GoogleAdsClient;
2121
import com.google.ads.googleads.lib.GoogleAdsException;
22+
import com.google.ads.googleads.lib.utils.ResourceNames;
2223
import com.google.ads.googleads.v0.common.KeywordInfo;
2324
import com.google.ads.googleads.v0.enums.AdGroupCriterionStatusEnum.AdGroupCriterionStatus;
2425
import com.google.ads.googleads.v0.enums.KeywordMatchTypeEnum.KeywordMatchType;
@@ -118,8 +119,7 @@ private void runExample(
118119
.setMatchType(KeywordMatchType.EXACT)
119120
.build();
120121

121-
String adGroupResourceName =
122-
AdGroupName.format(Long.toString(customerId), Long.toString(adGroupId));
122+
String adGroupResourceName = ResourceNames.adGroup(customerId, adGroupId);
123123

124124
// Construct an ad group criterion using the keywordText configuration above.
125125
AdGroupCriterion criterion =

google-ads-examples/src/main/java/com/google/ads/googleads/examples/basicoperations/RemoveKeyword.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.ads.googleads.examples.utils.CodeSampleParams;
2020
import com.google.ads.googleads.lib.GoogleAdsClient;
2121
import com.google.ads.googleads.lib.GoogleAdsException;
22+
import com.google.ads.googleads.lib.utils.ResourceNames;
2223
import com.google.ads.googleads.v0.errors.GoogleAdsError;
2324
import com.google.ads.googleads.v0.resources.AdGroupCriteriaName;
2425
import com.google.ads.googleads.v0.services.AdGroupCriterionOperation;
@@ -101,10 +102,8 @@ private void runExample(
101102
GoogleAdsClient googleAdsClient, long customerId, long adGroupId, long criterionId) {
102103
try (AdGroupCriterionServiceClient adGroupCriterionServiceClient =
103104
googleAdsClient.getAdGroupCriterionServiceClient()) {
104-
String adGroupCriterionResourceName =
105-
AdGroupCriteriaName.format(
106-
Long.toString(customerId),
107-
String.format("%s_%s", Long.toString(adGroupId), Long.toString(criterionId)));
105+
String adGroupCriterionResourceName = ResourceNames.adGroupCriterion(customerId, adGroupId, criterionId);
106+
108107
// Construct an operation that will remove the keyword with the specified resource name.
109108
AdGroupCriterionOperation operation =
110109
AdGroupCriterionOperation.newBuilder().setRemove(adGroupCriterionResourceName).build();

google-ads-examples/src/main/java/com/google/ads/googleads/examples/recommendations/ApplyRecommendation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.ads.googleads.examples.utils.CodeSampleParams;
2020
import com.google.ads.googleads.lib.GoogleAdsClient;
2121
import com.google.ads.googleads.lib.GoogleAdsException;
22+
import com.google.ads.googleads.lib.utils.ResourceNames;
2223
import com.google.ads.googleads.v0.errors.GoogleAdsError;
2324
import com.google.ads.googleads.v0.resources.RecommendationName;
2425
import com.google.ads.googleads.v0.services.ApplyRecommendationOperation;
@@ -100,8 +101,7 @@ public static void main(String[] args) throws IOException {
100101
*/
101102
private void runExample(
102103
GoogleAdsClient googleAdsClient, long customerId, String recommendationId) {
103-
String recommendationResourceName =
104-
RecommendationName.format(Long.toString(customerId), recommendationId);
104+
String recommendationResourceName = ResourceNames.recommendation(customerId, recommendationId);
105105

106106
ApplyRecommendationOperation.Builder operationBuilder =
107107
ApplyRecommendationOperation.newBuilder().setResourceName(recommendationResourceName);
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the license.
14+
15+
package com.google.ads.googleads.lib.utils;
16+
17+
import com.google.ads.googleads.v0.resources.AccountBudgetProposalName;
18+
import com.google.ads.googleads.v0.resources.AdGroupAdName;
19+
import com.google.ads.googleads.v0.resources.AdGroupBidModifierName;
20+
import com.google.ads.googleads.v0.resources.AdGroupCriteriaName;
21+
import com.google.ads.googleads.v0.resources.AdGroupName;
22+
import com.google.ads.googleads.v0.resources.BiddingStrategyName;
23+
import com.google.ads.googleads.v0.resources.BillingSetupName;
24+
import com.google.ads.googleads.v0.resources.CampaignBidModifierName;
25+
import com.google.ads.googleads.v0.resources.CampaignBudgetName;
26+
import com.google.ads.googleads.v0.resources.CampaignCriteriaName;
27+
import com.google.ads.googleads.v0.resources.CampaignGroupName;
28+
import com.google.ads.googleads.v0.resources.CampaignName;
29+
import com.google.ads.googleads.v0.resources.CampaignSharedSetName;
30+
import com.google.ads.googleads.v0.resources.ChangeStatusName;
31+
import com.google.ads.googleads.v0.resources.ConversionActionName;
32+
import com.google.ads.googleads.v0.resources.CustomerName;
33+
import com.google.ads.googleads.v0.resources.GeoTargetConstantName;
34+
import com.google.ads.googleads.v0.resources.GoogleAdsFieldName;
35+
import com.google.ads.googleads.v0.resources.KeywordViewName;
36+
import com.google.ads.googleads.v0.resources.RecommendationName;
37+
import com.google.ads.googleads.v0.resources.SharedCriteriaName;
38+
import com.google.ads.googleads.v0.resources.SharedSetName;
39+
import com.google.ads.googleads.v0.resources.VideoName;
40+
import com.google.common.base.Joiner;
41+
import java.util.Arrays;
42+
43+
/**
44+
* Utilities for generating resource names. Offers several advantages over the utilities in the
45+
* various {@code *Name} classes.
46+
*
47+
* <ul>
48+
* <li>Method parameter types match the type of each identifier. For example, the {@code
49+
* campaignId} parameter of {@link #campaign(long, long)} is of type {@code long}. In
50+
* contrast, the {@code campaign} parameter of {@link CampaignName#format(String, String)} is
51+
* of type {@code String}.
52+
* <li>Method parameter lists contain each component identifier. For example, {@link
53+
* #adGroupCriterion(long, long, long)} expects a customer ID, an ad group ID, and a criterion
54+
* ID. In contrast, {@link AdGroupCriteriaName#format(String, String)} expects a customer ID
55+
* and an underscore-delimited combination of ad group ID and criterion ID.
56+
* </ul>
57+
*/
58+
public class ResourceNames {
59+
60+
/** Joiner for resource path components that consist of multiple identifiers. */
61+
private static final Joiner IDENTIFIER_JOINER = Joiner.on('_');
62+
63+
/**
64+
* Returns a string consisting of the specified identifiers, concatenated together using the
65+
* standard delimiter for composite identifiers.
66+
*/
67+
private static String concatLongs(long... components) {
68+
return IDENTIFIER_JOINER.join(Arrays.stream(components).mapToObj(Long::toString).iterator());
69+
}
70+
71+
/** Returns the account budget proposal resource name for the specified components. */
72+
public static String accountBudgetProposal(long customerId, long accountBudgetProposalId) {
73+
return AccountBudgetProposalName.format(
74+
Long.toString(customerId), Long.toString(accountBudgetProposalId));
75+
}
76+
77+
/** Returns the ad group ad resource name for the specified components. */
78+
public static String adGroupAd(long customerId, long adGroupId, long adId) {
79+
return AdGroupAdName.format(Long.toString(customerId), concatLongs(adGroupId, adId));
80+
}
81+
82+
/** Returns the ad group bid modifier resource name for the specified components. */
83+
public static String adGroupBidModifier(long customerId, long adGroupId, long criterionId) {
84+
return AdGroupBidModifierName.format(
85+
Long.toString(customerId), concatLongs(adGroupId, criterionId));
86+
}
87+
88+
/** Returns the ad group criterion resource name for the specified components. */
89+
public static String adGroupCriterion(long customerId, long adGroupId, long criterionId) {
90+
return AdGroupCriteriaName.format(
91+
Long.toString(customerId), concatLongs(adGroupId, criterionId));
92+
}
93+
94+
/** Returns the ad group resource name for the specified components. */
95+
public static String adGroup(long customerId, long adGroupId) {
96+
return AdGroupName.format(Long.toString(customerId), Long.toString(adGroupId));
97+
}
98+
99+
/** Returns the bidding strategy resource name for the specified components. */
100+
public static String biddingStrategy(long customerId, long biddingStrategyId) {
101+
return BiddingStrategyName.format(Long.toString(customerId), Long.toString(biddingStrategyId));
102+
}
103+
104+
/** Returns the billing setup resource name for the specified components. */
105+
public static String billingSetup(long customerId, long billingSetupId) {
106+
return BillingSetupName.format(Long.toString(customerId), Long.toString(billingSetupId));
107+
}
108+
109+
/** Returns the campaign bid modifier resource name for the specified components. */
110+
public static String campaignBidModifier(long customerId, long campaignId, long criterionId) {
111+
return CampaignBidModifierName.format(
112+
Long.toString(customerId), concatLongs(campaignId, criterionId));
113+
}
114+
115+
/** Returns the campaign budget resource name for the specified components. */
116+
public static String campaignBudget(long customerId, long campaignBudgetId) {
117+
return CampaignBudgetName.format(Long.toString(customerId), Long.toString(campaignBudgetId));
118+
}
119+
120+
/** Returns the campaign criterion resource name for the specified components. */
121+
public static String campaignCriterion(long customerId, long campaignId, long criterionId) {
122+
return CampaignCriteriaName.format(
123+
Long.toString(customerId), concatLongs(campaignId, criterionId));
124+
}
125+
126+
/** Returns the campaign group resource name for the specified components. */
127+
public static String campaignGroup(long customerId, long campaignGroupId) {
128+
return CampaignGroupName.format(Long.toString(customerId), Long.toString(campaignGroupId));
129+
}
130+
131+
/** Returns the campaign resource name for the specified components. */
132+
public static String campaign(long customerId, long campaignId) {
133+
return CampaignName.format(Long.toString(customerId), Long.toString(campaignId));
134+
}
135+
136+
/** Returns the campaign shared set resource name for the specified components. */
137+
public static String campaignSharedSet(long customerId, long campaignSharedSetId) {
138+
return CampaignSharedSetName.format(
139+
Long.toString(customerId), Long.toString(campaignSharedSetId));
140+
}
141+
142+
/** Returns the change status resource name for the specified components. */
143+
public static String changeStatus(long customerId, long changeStatusId) {
144+
return ChangeStatusName.format(Long.toString(customerId), Long.toString(changeStatusId));
145+
}
146+
147+
/** Returns the conversion action resource name for the specified components. */
148+
public static String conversionAction(long customerId, long conversionActionId) {
149+
return ConversionActionName.format(
150+
Long.toString(customerId), Long.toString(conversionActionId));
151+
}
152+
153+
/** Returns the customer resource name for the specified components. */
154+
public static String customer(long customerId) {
155+
return CustomerName.format(Long.toString(customerId));
156+
}
157+
158+
/** Returns the geo target constant resource name for the specified components. */
159+
public static String geoTargetConstant(long geoTargetConstantId) {
160+
return GeoTargetConstantName.format(Long.toString(geoTargetConstantId));
161+
}
162+
163+
/** Returns the google ads field resource name for the specified components. */
164+
public static String googleAdsField(String fieldName) {
165+
return GoogleAdsFieldName.format(fieldName);
166+
}
167+
168+
/** Returns the keyword view resource name for the specified components. */
169+
public static String keywordView(long customerId, long adGroupId, long criterionId) {
170+
return KeywordViewName.format(Long.toString(customerId), concatLongs(adGroupId, criterionId));
171+
}
172+
173+
/** Returns the recommendation resource name for the specified components. */
174+
public static String recommendation(long customerId, String recommendationId) {
175+
return RecommendationName.format(Long.toString(customerId), recommendationId);
176+
}
177+
178+
/** Returns the shared criterion resource name for the specified components. */
179+
public static String sharedCriterion(long customerId, long sharedCriterionId) {
180+
return SharedCriteriaName.format(Long.toString(customerId), Long.toString(sharedCriterionId));
181+
}
182+
183+
/** Returns the shared set resource name for the specified components. */
184+
public static String sharedSet(long customerId, long sharedSetId) {
185+
return SharedSetName.format(Long.toString(customerId), Long.toString(sharedSetId));
186+
}
187+
188+
/** Returns the video resource name for the specified components. */
189+
public static String video(long customerId, long videoId) {
190+
return VideoName.format(Long.toString(customerId), Long.toString(videoId));
191+
}
192+
}

0 commit comments

Comments
 (0)