/
ListCreatives.java
142 lines (129 loc) · 5.33 KB
/
ListCreatives.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.services.samples.authorizedbuyers.realtimebidding.v1.bidders.creatives;
import com.google.api.services.realtimebidding.v1.RealTimeBidding;
import com.google.api.services.realtimebidding.v1.model.Creative;
import com.google.api.services.realtimebidding.v1.model.ListCreativesResponse;
import com.google.api.services.samples.authorizedbuyers.realtimebidding.Utils;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
/**
* This sample illustrates how to list Creatives for a given bidder account ID.
*
* <p>Note that unless filtered, this will include creatives from all buyer accounts under the
* bidder.
*/
public class ListCreatives {
public static void execute(RealTimeBidding client, Namespace parsedArgs) throws IOException {
Long accountId = parsedArgs.getLong("account_id");
Integer pageSize = parsedArgs.getInt("page_size");
String parentBuyerName = String.format("bidders/%s", accountId);
String pageToken = null;
System.out.printf("Found Creatives for bidder Account ID '%d':\n", accountId);
do {
List<Creative> creatives = null;
ListCreativesResponse response =
client
.bidders()
.creatives()
.list(parentBuyerName)
.setFilter(parsedArgs.getString("filter"))
.setView(parsedArgs.getString("view"))
.setPageSize(pageSize)
.setPageToken(pageToken)
.execute();
creatives = response.getCreatives();
pageToken = response.getNextPageToken();
if (creatives == null) {
System.out.println("No creatives found.");
} else {
for (Creative creative : creatives) {
Utils.printCreative(creative);
}
}
} while (pageToken != null);
}
public static void main(String[] args) {
ArgumentParser parser =
ArgumentParsers.newFor("ListCreatives")
.build()
.defaultHelp(true)
.description(("Lists creatives for the given bidder account."));
parser
.addArgument("-a", "--account_id")
.help(
"The resource ID of the bidders resource under which the creatives were created by"
+ " buyers. This will be used to construct the parent used as a path parameter for"
+ " the creatives.list request.")
.required(true)
.type(Long.class);
parser
.addArgument("-p", "--page_size")
.help(
"The resource ID of the buyers resource under which the user lists were created. "
+ "This will be used to construct the parent used as a path parameter for the "
+ "userLists.list request.")
.setDefault(Utils.getMaximumPageSize())
.type(Integer.class);
parser
.addArgument("-f", "--filter")
.help(
"Query string to filter creatives. If no filter is specified, all active creatives will"
+ " be returned. To demonstrate usage, the default behavior of this sample is to"
+ " filter such that only approved HTML snippet creatives are returned.")
.setDefault(
"creativeServingDecision.networkPolicyCompliance.status=APPROVED "
+ "AND creativeFormat=HTML");
parser
.addArgument("-v", "--view")
.help(
"Controls the amount of information included in the response. By default, the"
+ " creatives.list method only includes creativeServingDecision. This sample"
+ " configures the view to return the full contents of the creatives by setting"
+ " this to 'FULL'.")
.choices("FULL", "SERVING_DECISION_ONLY")
.setDefault("FULL");
Namespace parsedArgs = null;
try {
parsedArgs = parser.parseArgs(args);
} catch (ArgumentParserException ex) {
parser.handleError(ex);
System.exit(1);
}
RealTimeBidding client = null;
try {
client = Utils.getRealTimeBiddingClient();
} catch (IOException ex) {
System.out.printf("Unable to create RealTimeBidding API service:\n%s", ex);
System.out.println("Did you specify a valid path to a service account key file?");
System.exit(1);
} catch (GeneralSecurityException ex) {
System.out.printf("Unable to establish secure HttpTransport:\n%s", ex);
System.exit(1);
}
try {
execute(client, parsedArgs);
} catch (IOException ex) {
System.out.printf("RealTimeBidding API returned error response:\n%s", ex);
System.exit(1);
}
}
}