Skip to content

Commit a487e9d

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

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
def main(
28+
operating_account_type: datamanager_v1.ProductAccount.AccountType,
29+
operating_account_id: str,
30+
login_account_type: Optional[
31+
datamanager_v1.ProductAccount.AccountType
32+
] = None,
33+
login_account_id: Optional[str] = None,
34+
linked_account_type: Optional[
35+
datamanager_v1.ProductAccount.AccountType
36+
] = None,
37+
linked_account_id: Optional[str] = None,
38+
) -> None:
39+
"""Runs the sample.
40+
41+
Args:
42+
operating_account_type: the account type of the operating account.
43+
operating_account_id: the ID of the operating account.
44+
login_account_type: the account type of the login account.
45+
login_account_id: the ID of the login account.
46+
linked_account_type: the account type of the linked account.
47+
linked_account_id: the ID of the linked account.
48+
"""
49+
50+
# Validates parameter pairs.
51+
if bool(login_account_type) != bool(login_account_id):
52+
raise ValueError(
53+
"Must specify either both or neither of login account type and login account ID"
54+
)
55+
if bool(linked_account_type) != bool(linked_account_id):
56+
raise ValueError(
57+
"Must specify either both or neither of linked account type and linked account ID"
58+
)
59+
60+
# Creates a dictionary to pass header metadata.
61+
headers = []
62+
63+
if login_account_id:
64+
headers.append(
65+
(
66+
"login-account",
67+
f"accountTypes/{login_account_type}/accounts/{login_account_id}",
68+
)
69+
)
70+
71+
if linked_account_id:
72+
headers.append(
73+
(
74+
"linked-account",
75+
f"accountTypes/{linked_account_type}/accounts/{linked_account_id}",
76+
)
77+
)
78+
79+
call_kwargs = {"metadata": headers}
80+
81+
# Creates a client for UserListService.
82+
user_list_client = datamanager_v1.UserListServiceClient()
83+
84+
# Sends the request. The client library returns an iterator that automatically fetches all
85+
# response pages.
86+
response_pager: iter = user_list_client.list_user_lists(
87+
parent=f"accountTypes/{operating_account_type}/accounts/{operating_account_id}",
88+
**call_kwargs,
89+
)
90+
91+
user_list_num = 0
92+
for user_list in response_pager:
93+
user_list_num += 1
94+
_logger.info("User list #%d:\n%s\n", user_list_num, user_list)
95+
96+
_logger.info("Total count of user list resources: %d", user_list_num)
97+
98+
99+
if __name__ == "__main__":
100+
# Configures logging.
101+
logging.basicConfig(level=logging.INFO)
102+
103+
parser = argparse.ArgumentParser(
104+
description=("Retrieves all users lists from an operating account."),
105+
fromfile_prefix_chars="@",
106+
)
107+
# The following argument(s) should be provided to run the example.
108+
parser.add_argument(
109+
"--operating_account_type",
110+
type=str,
111+
required=True,
112+
help="The account type of the operating account.",
113+
)
114+
parser.add_argument(
115+
"--operating_account_id",
116+
type=str,
117+
required=True,
118+
help="The ID of the operating account.",
119+
)
120+
parser.add_argument(
121+
"--login_account_type",
122+
type=str,
123+
required=False,
124+
help="The account type of the login account.",
125+
)
126+
parser.add_argument(
127+
"--login_account_id",
128+
type=str,
129+
required=False,
130+
help="The ID of the login account.",
131+
)
132+
parser.add_argument(
133+
"--linked_account_type",
134+
type=str,
135+
required=False,
136+
help="The account type of the linked account.",
137+
)
138+
parser.add_argument(
139+
"--linked_account_id",
140+
type=str,
141+
required=False,
142+
help="The ID of the linked account.",
143+
)
144+
args = parser.parse_args()
145+
146+
main(
147+
args.operating_account_type,
148+
args.operating_account_id,
149+
args.login_account_type,
150+
args.login_account_id,
151+
args.linked_account_type,
152+
args.linked_account_id,
153+
)

0 commit comments

Comments
 (0)