@@ -47,6 +47,22 @@ def validation_wrapper(*args, **kwargs):
4747 return validation_wrapper
4848
4949
50+ def _config_parser_decorator (func ):
51+ """A decorator used to easily parse config values.
52+
53+ Since configs can be loaded from different locations such as env vars or
54+ from YAML files it's possible that they may have inconsistent types that
55+ need to be parsed to a different type. Add this decorator to any method
56+ that returns the config as a dict.
57+ """
58+ @functools .wraps (func )
59+ def parser_wrapper (* args , ** kwargs ):
60+ config_dict = func (* args , ** kwargs )
61+ parsed_config = convert_login_customer_id_to_str (config_dict )
62+ return parsed_config
63+ return parser_wrapper
64+
65+
5066def validate_dict (config_data ):
5167 """Validates the given configuration dict.
5268
@@ -87,6 +103,7 @@ def validate_login_customer_id(login_customer_id):
87103
88104
89105@_config_validation_decorator
106+ @_config_parser_decorator
90107def load_from_yaml_file (path = None ):
91108 """Loads configuration data from a YAML file and returns it as a dict.
92109
@@ -114,6 +131,7 @@ def load_from_yaml_file(path=None):
114131
115132
116133@_config_validation_decorator
134+ @_config_parser_decorator
117135def parse_yaml_document_to_dict (yaml_doc ):
118136 """Parses a YAML document to a dict.
119137
@@ -131,6 +149,7 @@ def parse_yaml_document_to_dict(yaml_doc):
131149
132150
133151@_config_validation_decorator
152+ @_config_parser_decorator
134153def load_from_env ():
135154 """Loads configuration data from the environment and returns it as a dict.
136155
@@ -171,3 +190,24 @@ def get_oauth2_service_account_keys():
171190 A tuple containing the required keys as strs.
172191 """
173192 return _OAUTH2_SERVICE_ACCOUNT_KEYS
193+
194+
195+ def convert_login_customer_id_to_str (config_data ):
196+ """Parses a config dict's login_customer_id attr value to a str.
197+
198+ Like many values from YAML it's possible for login_customer_id to
199+ either be a str or an int. Since we actually run validations on this
200+ value before making requests it's important to parse it to a str.
201+
202+ Args:
203+ config_data: A config dict object.
204+
205+ Returns:
206+ The same config dict object with a mutated login_customer_id attr.
207+ """
208+ login_customer_id = config_data .get ('login_customer_id' )
209+
210+ if login_customer_id :
211+ config_data ['login_customer_id' ] = str (login_customer_id )
212+
213+ return config_data
0 commit comments