Skip to content

Commit 805e14d

Browse files
authored
Merge pull request #16 from dataiku/bug/sc-76640-next-page-pagination-with-extractionn-path
Fix Next page pagination x extraction path
2 parents bd4953c + a973c70 commit 805e14d

6 files changed

Lines changed: 29 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22

33

4+
## [Version 1.0.4](https://github.com/dataiku/dss-plugin-api-connect/releases/tag/v1.0.4) - Bugfix release - 2021-12-14
5+
6+
- Fix use of "Next page pagination" in combination with extraction path
7+
- Extraction path added to recipe
8+
49
## [Version 1.0.3](https://github.com/dataiku/dss-plugin-api-connect/releases/tag/v1.0.3) - Bugfix and feature release - 2021-11-23
510

611
- Fixes error raised on HTTP 204 status codes

custom-recipes/api-connect/recipe.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"icon": "icon-rocket"
66
},
77
"kind": "PYTHON",
8+
"selectableFromDataset": "input_A_role",
89
"inputRoles": [
910
{
1011
"name": "input_A_role",

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "api-connect",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"meta": {
55
"label": "API Connect",
66
"description": "Retrieve data from any REST API",

python-lib/dku_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ def parse_keys_for_json(items):
4242
return ret
4343

4444

45-
def get_value_from_path(dictionary, path):
45+
def get_value_from_path(dictionary, path, default=None, can_raise=True):
4646
ret = copy.deepcopy(dictionary)
4747
for key in path:
4848
if key in ret and isinstance(ret, dict):
4949
ret = ret.get(key)
5050
else:
51-
raise ValueError("The extraction path {} was not found in the incoming data".format(path))
51+
error_message = "The extraction path {} was not found in the incoming data".format(path)
52+
if can_raise:
53+
raise ValueError(error_message)
54+
elif default:
55+
return default # [{"error": error_message}]
56+
else:
57+
return None
5258
return ret

python-lib/pagination.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from safe_logger import SafeLogger
2+
from dku_utils import get_value_from_path
23

34

45
logger = SafeLogger("api-connect plugin Pagination")
@@ -66,8 +67,11 @@ def update_next_page(self, data, response_links=None):
6667
self.is_last_batch_empty = True
6768
return
6869
elif self.counting_key:
69-
batch_size = len(data.get(self.counting_key, []))
70-
if batch_size == 0:
70+
extracted_data = get_value_from_path(data, self.counting_key.split("."), can_raise=False)
71+
if extracted_data:
72+
batch_size = len(extracted_data)
73+
else:
74+
batch_size = 0
7175
self.is_last_batch_empty = True
7276
else:
7377
batch_size = 1

python-lib/rest_api_recipe_session.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataikuapi.utils import DataikuException
22
from rest_api_client import RestAPIClient
33
from safe_logger import SafeLogger
4-
from dku_utils import parse_keys_for_json
4+
from dku_utils import parse_keys_for_json, get_value_from_path
55
from dku_constants import DKUConstants
66
import copy
77
import json
@@ -23,6 +23,7 @@ def __init__(self, custom_key_values, credential_parameters, endpoint_parameters
2323
self.display_metadata = display_metadata
2424
self.maximum_number_rows = maximum_number_rows
2525
self.is_row_limit = (self.maximum_number_rows > 0)
26+
self.can_raise = False
2627

2728
@staticmethod
2829
def get_column_to_parameter_dict(parameter_columns, parameter_renamings):
@@ -68,9 +69,13 @@ def retrieve_next_page(self, is_raw_output):
6869
metadata = self.client.get_metadata() if self.display_metadata else {}
6970
is_api_returning_dict = True
7071
if self.extraction_key:
71-
data_rows = json_response.get(self.extraction_key, [json_response])
72+
data_rows = get_value_from_path(json_response, self.extraction_key.split("."), can_raise=False)
7273
if data_rows is None:
73-
raise DataikuException("Extraction key '{}' was not found in the incoming data".format(self.extraction_key))
74+
error_message = "Extraction key '{}' was not found in the incoming data".format(self.extraction_key)
75+
if self.can_raise:
76+
raise DataikuException(error_message)
77+
else:
78+
return [{"error": error_message}]
7479
page_rows.extend(self.format_page_rows(data_rows, is_raw_output, metadata))
7580
else:
7681
# Todo: check api_response key is free and add something overwise

0 commit comments

Comments
 (0)