Skip to content

Commit f6c814e

Browse files
committed
feat(sample): add list user lists sample
Change-Id: I3a7dd56a7f5fdefd2210cd859dc29a383c7411ce
1 parent 42dd158 commit f6c814e

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/env python
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""Sample of retrieving all user lists from an operating account."""
16+
17+
18+
import argparse
19+
import logging
20+
from typing import Dict, List, Optional
21+
22+
from google.ads import datamanager_v1
23+
24+
_logger = logging.getLogger(__name__)
25+
26+
27+
# [START list-user-lists]
28+
def main(
29+
operating_account_type: datamanager_v1.ProductAccount.AccountType,
30+
operating_account_id: str,
31+
login_account_type: Optional[
32+
datamanager_v1.ProductAccount.AccountType
33+
] = None,
34+
login_account_id: Optional[str] = None,
35+
linked_account_type: Optional[
36+
datamanager_v1.ProductAccount.AccountType
37+
] = None,
38+
linked_account_id: Optional[str] = None,
39+
) -> None:
40+
"""Runs the sample.
41+
42+
Args:
43+
operating_account_type: the account type of the operating account.
44+
operating_account_id: the ID of the operating account.
45+
login_account_type: the account type of the login account.
46+
login_account_id: the ID of the login account.
47+
linked_account_type: the account type of the linked account.
48+
linked_account_id: the ID of the linked account.
49+
"""
50+
51+
# Validates parameter pairs.
52+
if bool(login_account_type) != bool(login_account_id):
53+
raise ValueError(
54+
"Must specify either both or neither of login account type and login account ID"
55+
)
56+
if bool(linked_account_type) != bool(linked_account_id):
57+
raise ValueError(
58+
"Must specify either both or neither of linked account type and linked account ID"
59+
)
60+
61+
# Creates a dictionary to pass header metadata.
62+
headers = []
63+
64+
if login_account_id:
65+
headers.append(
66+
(
67+
"login-account",
68+
f"accountTypes/{login_account_type}/accounts/{login_account_id}",
69+
)
70+
)
71+
72+
if linked_account_id:
73+
headers.append(
74+
(
75+
"linked-account",
76+
f"accountTypes/{linked_account_type}/accounts/{linked_account_id}",
77+
)
78+
)
79+
80+
call_kwargs = {"metadata": headers}
81+
82+
# Creates a client for UserListService.
83+
user_list_client = datamanager_v1.UserListServiceClient()
84+
85+
# Sends the request. The client library returns an iterator that automatically fetches all
86+
# response pages.
87+
response_pager: iter = user_list_client.list_user_lists(
88+
parent=f"accountTypes/{operating_account_type}/accounts/{operating_account_id}",
89+
**call_kwargs,
90+
)
91+
92+
user_list_num = 0
93+
for user_list in response_pager:
94+
user_list_num += 1
95+
_logger.info("User list #%d:\n%s\n", user_list_num, user_list)
96+
97+
_logger.info("Total count of user list resources: %d", user_list_num)
98+
# [END list-user-lists]
99+
100+
101+
if __name__ == "__main__":
102+
# Configures logging.
103+
logging.basicConfig(level=logging.INFO)
104+
105+
parser = argparse.ArgumentParser(
106+
description=("Retrieves all users lists from an operating account."),
107+
fromfile_prefix_chars="@",
108+
)
109+
# The following argument(s) should be provided to run the example.
110+
parser.add_argument(
111+
"--operating_account_type",
112+
type=str,
113+
required=True,
114+
help="The account type of the operating account.",
115+
)
116+
parser.add_argument(
117+
"--operating_account_id",
118+
type=str,
119+
required=True,
120+
help="The ID of the operating account.",
121+
)
122+
parser.add_argument(
123+
"--login_account_type",
124+
type=str,
125+
required=False,
126+
help="The account type of the login account.",
127+
)
128+
parser.add_argument(
129+
"--login_account_id",
130+
type=str,
131+
required=False,
132+
help="The ID of the login account.",
133+
)
134+
parser.add_argument(
135+
"--linked_account_type",
136+
type=str,
137+
required=False,
138+
help="The account type of the linked account.",
139+
)
140+
parser.add_argument(
141+
"--linked_account_id",
142+
type=str,
143+
required=False,
144+
help="The ID of the linked account.",
145+
)
146+
args = parser.parse_args()
147+
148+
main(
149+
args.operating_account_type,
150+
args.operating_account_id,
151+
args.login_account_type,
152+
args.login_account_id,
153+
args.linked_account_type,
154+
args.linked_account_id,
155+
)

0 commit comments

Comments
 (0)