Skip to content

Commit 78c1156

Browse files
akaashhazarikaBenRKarl
authored andcommitted
Added load_from_dict method to the GoogleAdsClient (googleads#152)
1 parent 81ff27f commit 78c1156

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

google/ads/google_ads/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ def load_from_string(cls, yaml_str):
9595
kwargs = cls._get_client_kwargs(config_data)
9696
return cls(**kwargs)
9797

98+
@classmethod
99+
def load_from_dict(cls, config_dict):
100+
"""Creates a GoogleAdsClient with data stored in the config_dict.
101+
102+
Args:
103+
config_dict: a dict consisting of configuration data used to
104+
initialize a GoogleAdsClient.
105+
106+
Returns:
107+
A GoogleAdsClient initialized with the values specified in the
108+
dict.
109+
110+
Raises:
111+
ValueError: If the configuration lacks a required field.
112+
"""
113+
config_data = config.load_from_dict(config_dict)
114+
kwargs = cls._get_client_kwargs(config_data)
115+
return cls(**kwargs)
116+
98117
@classmethod
99118
def load_from_storage(cls, path=None):
100119
"""Creates a GoogleAdsClient with data stored in the specified file.

google/ads/google_ads/config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ def load_from_yaml_file(path=None):
130130
return parse_yaml_document_to_dict(yaml_doc)
131131

132132

133+
@_config_validation_decorator
134+
@_config_parser_decorator
135+
def load_from_dict(config_dict):
136+
"""Check if the argument is dictionary or not. If successful it calls the parsing decorator,
137+
followed by validation decorator. This validates the keys used in the config_dict, before
138+
returning to its caller.
139+
140+
Args:
141+
config_dict: a dict containing client configuration.
142+
143+
Returns:
144+
The same input dictionary that is passed into the function.
145+
146+
Raises:
147+
A value error if the argument (config_dict) is not a dict.
148+
"""
149+
if isinstance(config_dict, dict):
150+
return config_dict
151+
else:
152+
raise ValueError("The configuration object passed to function load_from_dict must be of type dict.")
153+
154+
133155
@_config_validation_decorator
134156
@_config_parser_decorator
135157
def parse_yaml_document_to_dict(yaml_doc):

tests/client_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,32 @@ def test_get_client_kwargs_custom_endpoint(self):
200200
'logging_config': None
201201
})
202202

203+
def test_load_from_dict(self):
204+
config = {
205+
'developer_token': self.developer_token,
206+
'client_id': self.client_id,
207+
'client_secret': self.client_secret,
208+
'refresh_token': self.refresh_token}
209+
mock_credentials_instance = mock.Mock()
210+
211+
with mock.patch.object(
212+
Client.GoogleAdsClient,
213+
'__init__',
214+
return_value=None
215+
) as mock_client_init, mock.patch.object(
216+
Client.oauth2,
217+
'get_installed_app_credentials',
218+
return_value=mock_credentials_instance
219+
) as mock_credentials:
220+
Client.GoogleAdsClient.load_from_dict(config)
221+
222+
mock_client_init.assert_called_once_with(
223+
credentials=mock_credentials_instance,
224+
developer_token=self.developer_token,
225+
endpoint=None,
226+
login_customer_id=None,
227+
logging_config=None)
228+
203229
def test_load_from_storage(self):
204230
config = {
205231
'developer_token': self.developer_token,

tests/config_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ def test_parse_yaml_document_to_dict_missing_required_key(self):
120120
config.parse_yaml_document_to_dict,
121121
yaml_doc)
122122

123+
def test_load_from_dict(self):
124+
config_data = {
125+
'developer_token': self.developer_token,
126+
'client_id': self.client_id,
127+
'client_secret': self.client_secret,
128+
'refresh_token': self.refresh_token}
129+
self.assertEqual(config.load_from_dict(config_data), config_data)
130+
131+
def test_load_from_dict_error(self):
132+
config_data = 111
133+
self.assertRaises(ValueError, config.load_from_dict, config_data)
134+
123135
def test_load_from_env(self):
124136
environ = {
125137
'GOOGLE_ADS_DEVELOPER_TOKEN': self.developer_token,

0 commit comments

Comments
 (0)