2424
2525import argparse
2626import sys
27+ from typing import Optional , List , Dict
2728
2829from google .ads .googleads .client import GoogleAdsClient
2930from google .ads .googleads .errors import GoogleAdsException
30-
31-
32- def main (client , login_customer_id = None ):
31+ from google .ads .googleads .v19 .services .services .google_ads_service .client import GoogleAdsServiceClient
32+ from google .ads .googleads .v19 .services .services .customer_service .client import CustomerServiceClient
33+ from google .ads .googleads .v19 .resources .types .customer_client import CustomerClient
34+ from google .ads .googleads .v19 .services .types .google_ads_service import SearchPagedResponse , GoogleAdsRow
35+ # ListAccessibleCustomersResponse is not directly used for a variable type,
36+ # but its attribute .resource_names is used, which is List[str].
37+
38+ def main (client : GoogleAdsClient , login_customer_id : Optional [str ] = None ) -> None :
3339 """Gets the account hierarchy of the given MCC and login customer ID.
3440
3541 Args:
@@ -40,11 +46,11 @@ def main(client, login_customer_id=None):
4046 """
4147
4248 # Gets instances of the GoogleAdsService and CustomerService clients.
43- googleads_service = client .get_service ("GoogleAdsService" )
44- customer_service = client .get_service ("CustomerService" )
49+ googleads_service : GoogleAdsServiceClient = client .get_service ("GoogleAdsService" )
50+ customer_service : CustomerServiceClient = client .get_service ("CustomerService" )
4551
4652 # A collection of customer IDs to handle.
47- seed_customer_ids = []
53+ seed_customer_ids : List [ str ] = []
4854
4955 # Creates a query that retrieves all child accounts of the manager
5056 # specified in search calls below.
@@ -64,69 +70,74 @@ def main(client, login_customer_id=None):
6470 # the only ID in the list. Otherwise, we will issue a request for all
6571 # customers accessible by this authenticated Google account.
6672 if login_customer_id is not None :
73+ # Ensure login_customer_id is treated as a string for this list.
6774 seed_customer_ids = [login_customer_id ]
6875 else :
6976 print (
7077 "No manager ID is specified. The example will print the "
7178 "hierarchies of all accessible customer IDs."
7279 )
7380
74- customer_resource_names = (
81+ customer_resource_names : List [ str ] = (
7582 customer_service .list_accessible_customers ().resource_names
7683 )
7784
7885 for customer_resource_name in customer_resource_names :
79- customer_id = googleads_service .parse_customer_path (
86+ # customer_id here is a string as returned by parse_customer_path
87+ customer_id_from_parse : str = googleads_service .parse_customer_path (
8088 customer_resource_name
8189 )["customer_id" ]
82- print (customer_id )
83- seed_customer_ids .append (customer_id )
90+ print (customer_id_from_parse )
91+ seed_customer_ids .append (customer_id_from_parse )
8492
85- for seed_customer_id in seed_customer_ids :
93+ for seed_customer_id_str in seed_customer_ids : # seed_customer_id_str is a string
8694 # Performs a breadth-first search to build a Dictionary that maps
8795 # managers to their child accounts (customerIdsToChildAccounts).
88- unprocessed_customer_ids = [seed_customer_id ]
89- customer_ids_to_child_accounts = dict ()
90- root_customer_client = None
96+ # unprocessed_customer_ids should store integers.
97+ unprocessed_customer_ids : List [int ] = [int (seed_customer_id_str )]
98+ customer_ids_to_child_accounts : Dict [int , List [CustomerClient ]] = dict ()
99+ root_customer_client : CustomerClient | None = None
91100
92101 while unprocessed_customer_ids :
93- customer_id = int (unprocessed_customer_ids .pop (0 ))
94- response = googleads_service .search (
95- customer_id = str (customer_id ), query = query
102+ customer_id_loop : int = unprocessed_customer_ids .pop (0 ) # customer_id_loop is an int
103+ # The search method expects customer_id to be a string.
104+ response : SearchPagedResponse = googleads_service .search (
105+ customer_id = str (customer_id_loop ), query = query
96106 )
97107
98108 # Iterates over all rows in all pages to get all customer
99109 # clients under the specified customer's hierarchy.
100- for googleads_row in response :
101- customer_client = googleads_row .customer_client
110+ for googleads_row : GoogleAdsRow in response :
111+ customer_client_loop_var : CustomerClient = googleads_row .customer_client
102112
103113 # The customer client that with level 0 is the specified
104114 # customer.
105- if customer_client .level == 0 :
115+ if customer_client_loop_var .level == 0 :
106116 if root_customer_client is None :
107- root_customer_client = customer_client
117+ root_customer_client = customer_client_loop_var
108118 continue
109119
110120 # For all level-1 (direct child) accounts that are a
111121 # manager account, the above query will be run against them
112122 # to create a Dictionary of managers mapped to their child
113123 # accounts for printing the hierarchy afterwards.
114- if customer_id not in customer_ids_to_child_accounts :
115- customer_ids_to_child_accounts [customer_id ] = []
124+ if customer_id_loop not in customer_ids_to_child_accounts :
125+ customer_ids_to_child_accounts [customer_id_loop ] = []
116126
117- customer_ids_to_child_accounts [customer_id ].append (
118- customer_client
127+ customer_ids_to_child_accounts [customer_id_loop ].append (
128+ customer_client_loop_var
119129 )
120130
121- if customer_client .manager :
131+ if customer_client_loop_var .manager :
122132 # A customer can be managed by multiple managers, so to
123133 # prevent visiting the same customer many times, we
124134 # need to check if it's already in the Dictionary.
135+ # Assuming customer_client_loop_var.id is an int
125136 if (
126- customer_client .id not in customer_ids_to_child_accounts
127- and customer_client .level == 1
137+ customer_client_loop_var .id not in customer_ids_to_child_accounts
138+ and customer_client_loop_var .level == 1
128139 ):
129- unprocessed_customer_ids .append (customer_client .id )
140+ unprocessed_customer_ids .append (customer_client_loop_var .id )
130141
131142 if root_customer_client is not None :
132143 print (
@@ -145,8 +156,8 @@ def main(client, login_customer_id=None):
145156
146157
147158def print_account_hierarchy (
148- customer_client , customer_ids_to_child_accounts , depth
149- ):
159+ customer_client : CustomerClient , customer_ids_to_child_accounts : Dict [ int , List [ CustomerClient ]], depth : int
160+ ) -> None :
150161 """Prints the specified account's hierarchy using recursion.
151162
152163 Args:
@@ -160,17 +171,18 @@ def print_account_hierarchy(
160171 if depth == 0 :
161172 print ("Customer ID (Descriptive Name, Currency Code, Time Zone)" )
162173
163- customer_id = customer_client .id
174+ # Assuming customer_client.id is an int based on previous analysis for keys in customer_ids_to_child_accounts
175+ customer_id_print : int = customer_client .id
164176 print ("-" * (depth * 2 ), end = "" )
165177 print (
166- f"{ customer_id } ({ customer_client .descriptive_name } , "
178+ f"{ customer_id_print } ({ customer_client .descriptive_name } , "
167179 f"{ customer_client .currency_code } , "
168180 f"{ customer_client .time_zone } )"
169181 )
170182
171183 # Recursively call this function for all child accounts of customer_client.
172- if customer_id in customer_ids_to_child_accounts :
173- for child_account in customer_ids_to_child_accounts [customer_id ]:
184+ if customer_id_print in customer_ids_to_child_accounts :
185+ for child_account : CustomerClient in customer_ids_to_child_accounts [customer_id_print ]:
174186 print_account_hierarchy (
175187 child_account , customer_ids_to_child_accounts , depth + 1
176188 )
0 commit comments