Skip to content

Commit 9759305

Browse files
authored
Merge pull request #21 from dataiku/bug/sc-82448-templating-not-working-when-no-codeenv
Fix for SC 82448 : templating not working when no codeenv
2 parents 4e2de29 + 2a15bca commit 9759305

4 files changed

Lines changed: 64 additions & 23 deletions

File tree

python-lib/dku_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,30 @@ def get_value_from_path(dictionary, path, default=None, can_raise=True):
5757
else:
5858
return None
5959
return ret
60+
61+
62+
def template_dict(dictionnary, **kwargs):
63+
""" Recurses into dictionnary and replace template {{keys}} with the matching values present in the kwargs dictionnary"""
64+
ret = dict.copy(dictionnary)
65+
for key in ret:
66+
if isinstance(ret[key], dict):
67+
ret[key] = template_dict(ret[key], **kwargs)
68+
if is_string(ret[key]):
69+
ret[key] = format_template(ret[key], **kwargs)
70+
return ret
71+
72+
73+
def format_template(template, **kwargs):
74+
""" Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
75+
if template is None:
76+
return None
77+
formated = template
78+
for key in kwargs:
79+
replacement = kwargs.get(key, "")
80+
formated = formated.replace("{{{{{}}}}}".format(key), str(replacement))
81+
return formated
82+
83+
84+
def is_string(data):
85+
data_type = type(data).__name__
86+
return data_type in ["str", "unicode"]

python-lib/rest_api_client.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,13 @@
44
from pagination import Pagination
55
from safe_logger import SafeLogger
66
from loop_detector import LoopDetector
7-
from dku_utils import get_dku_key_values
7+
from dku_utils import get_dku_key_values, template_dict, format_template
88
from dku_constants import DKUConstants
99

1010

1111
logger = SafeLogger("api-connect plugin", forbiden_keys=["token", "password"])
1212

1313

14-
def template_dict(dictionnary, **kwargs):
15-
""" Recurses into dictionnary and replace template {{keys}} with the matching values present in the kwargs dictionnary"""
16-
ret = dict.copy(dictionnary)
17-
for key in ret:
18-
if isinstance(ret[key], dict):
19-
ret[key] = template_dict(ret[key], **kwargs)
20-
if isinstance(ret[key], str):
21-
ret[key] = format_template(ret[key], **kwargs)
22-
return ret
23-
24-
25-
def format_template(template, **kwargs):
26-
""" Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
27-
if template is None:
28-
return None
29-
formated = template
30-
for key in kwargs:
31-
replacement = kwargs.get(key, "")
32-
formated = formated.replace("{{{{{}}}}}".format(key), str(replacement))
33-
return formated
34-
35-
3614
class RestAPIClientError(ValueError):
3715
pass
3816

tests/python/unit/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest==6.2.1
2+
allure-pytest==2.8.29

tests/python/unit/test_common.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from dku_utils import template_dict
2+
import pytest
3+
4+
5+
class TestCommonMethods:
6+
def setup_class(self):
7+
self.template = {
8+
'url': 'https://api.spotify.com/v1/users/{{user_id}}/playlists',
9+
'headers': {
10+
'Content-Type': 'application/json',
11+
'Authorization': 'Bearer {{access_token}}'
12+
},
13+
'recipe_columns_parameter_names': ['user_id'],
14+
'key_to_next_page_url': 'next', 'items_key': 'items'
15+
}
16+
self.kwargs = {
17+
u'column': u'profiles id',
18+
'access_token': u'12341234secretcode-4321shhhhhh',
19+
'user_id': '1234abcde'
20+
}
21+
self.endpoint_ok = {
22+
'url': 'https://api.spotify.com/v1/users/1234abcde/playlists',
23+
'headers': {
24+
'Content-Type': 'application/json',
25+
'Authorization': 'Bearer 12341234secretcode-4321shhhhhh'
26+
},
27+
'key_to_next_page_url': 'next',
28+
'items_key': 'items',
29+
'recipe_columns_parameter_names': ['user_id']
30+
}
31+
32+
def test_template_dict(self):
33+
endpoint = template_dict(self.template, **self.kwargs)
34+
assert endpoint == self.endpoint_ok

0 commit comments

Comments
 (0)