Skip to content

Commit 2948aff

Browse files
committed
A bit of cleanup on API node endpoints wrapping
1 parent 148d500 commit 2948aff

File tree

7 files changed

+63
-72
lines changed

7 files changed

+63
-72
lines changed

dataikuapi/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from dssclient import DSSClient
1+
from .dssclient import DSSClient
22

3-
from apinode_client import APINodeClient
4-
from apinode_admin_client import APINodeAdminClient
3+
from .apinode_client import APINodeClient
4+
from .apinode_admin_client import APINodeAdminClient
55

6-
from dss.recipe import GroupingRecipeCreator, JoinRecipeCreator, StackRecipeCreator, WindowRecipeCreator, SyncRecipeCreator, SamplingRecipeCreator, SQLQueryRecipeCreator, CodeRecipeCreator, SplitRecipeCreator, SortRecipeCreator, TopNRecipeCreator, DistinctRecipeCreator, DownloadRecipeCreator
6+
from .dss.recipe import GroupingRecipeCreator, JoinRecipeCreator, StackRecipeCreator, WindowRecipeCreator, SyncRecipeCreator, SamplingRecipeCreator, SQLQueryRecipeCreator, CodeRecipeCreator, SplitRecipeCreator, SortRecipeCreator, TopNRecipeCreator, DistinctRecipeCreator, DownloadRecipeCreator
77

8-
from dss.admin import DSSUserImpersonationRule, DSSGroupImpersonationRule
8+
from .dss.admin import DSSUserImpersonationRule, DSSGroupImpersonationRule

dataikuapi/apinode_admin_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from apinode_admin.service import APINodeService
2-
from apinode_admin.auth import APINodeAuth
1+
from .apinode_admin.service import APINodeService
2+
from .apinode_admin.auth import APINodeAuth
33
from .utils import DataikuException
44
from .base_client import DSSBaseClient
55

dataikuapi/apinode_client.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,53 +67,21 @@ def predict_records(self, endpoint_id, records, forced_generation=None, dispatch
6767

6868
return self._perform_json("POST", "%s/predict-multi" % endpoint_id, body = obj)
6969

70-
def evaluate_function(self, endpoint_id, **kwargs):
70+
def sql_query(self, endpoint_id, parameters):
7171
"""
72-
Applies a DSS API node function endpoint (Python or R) to the given parameters
72+
Queries a "SQL query" endpoint on a DSS API node
7373
7474
:param str endpoint_id: Identifier of the endpoint to query
75-
:param **kwargs: parameters to the functon
76-
77-
:return: a Python dict of the API answer. The answer is the result
78-
"""
79-
return self.apply_on_record(endpoint_id, kwargs)
80-
81-
def apply_on_record(self, endpoint_id, features):
82-
"""
83-
Applies a function on a single record on a DSS API node endpoint (Python or R function)
84-
85-
:param str endpoint_id: Identifier of the endpoint to query
86-
:param features: Python dictionary of features of the record
87-
88-
:return: a Python dict of the API answer. The answer is the result
89-
"""
90-
return self._perform_json("POST", "%s/run" % endpoint_id, body = features)
91-
92-
def run_query(self, endpoint_id, **kwargs):
93-
"""
94-
Run a DSS API node SQL endpoint with the given parameters
95-
96-
:param str endpoint_id: Identifier of the endpoint to query
97-
:param **kwargs: parameters to the functon
75+
:param parameters: Python dictionary of the named parameters for the SQL query endpoint
9876
9977
:return: a Python dict of the API answer. The answer is the a dict with a columns field and a rows field (list of rows as list of strings)
10078
"""
101-
return self.query_on_record(endpoint_id, kwargs)
79+
return self._perform_json("POST", "%s/query" % endpoint_id, body = parameters)
10280

103-
def query_on_record(self, endpoint_id, features):
104-
"""
105-
Run a record on a DSS API node SQL endpoint
106-
107-
:param str endpoint_id: Identifier of the endpoint to query
108-
:param features: Python dictionary of features of the record
109-
110-
:return: a Python dict of the API answer. The answer is the a dict with a columns field and a rows field (list of rows as list of strings)
111-
"""
112-
return self._perform_json("POST", "%s/query" % endpoint_id, body = features)
11381

11482
def lookup_record(self, endpoint_id, record, context=None):
11583
"""
116-
Lookup a single record on a DSS API node endpoint
84+
Lookup a single record on a DSS API node endpoint of "dataset lookup" type
11785
11886
:param str endpoint_id: Identifier of the endpoint to query
11987
:param record: Python dictionary of features of the record
@@ -129,15 +97,16 @@ def lookup_record(self, endpoint_id, record, context=None):
12997

13098
return self._perform_json("POST", "%s/lookup" % endpoint_id, body = obj).get("results", [])[0]
13199

132-
def lookup_records(self, endpoint_id, records, context=None):
100+
def lookup_records(self, endpoint_id, records):
133101
"""
134-
Lookup a single record on a DSS API node endpoint
102+
Lookups a batch of records on a DSS API node endpoint of "dataset lookup" type
135103
136104
:param str endpoint_id: Identifier of the endpoint to query
137-
:param records: Python list of records. Each record must be a Python dict. Each record must contain a "data" dict (see predict_record) and optionally a "context" dict.
105+
:param records: Python list of records. Each record must be a Python dict, containing at least one entry called "data": a dict containing the input columns
138106
139-
:return: a Python dict of the API answer. The answer contains a "results" key (which is an array of result objects)
107+
:return: a Python dict of the API answer. The answer contains a "results" key, which is an array of result objects. Each result contains a "data" dict which is the output
140108
"""
109+
141110
for record in records:
142111
if not "data" in record:
143112
raise ValueError("Each record must contain a 'data' dict")
@@ -148,3 +117,16 @@ def lookup_records(self, endpoint_id, records, context=None):
148117

149118
return self._perform_json("POST", "%s/lookup-multi" % endpoint_id, body = obj)
150119

120+
def run_function(self, endpoint_id, **kwargs):
121+
"""
122+
Calls a "Run function" endpoint on a DSS API node
123+
124+
:param str endpoint_id: Identifier of the endpoint to query
125+
:param kwargs: Arguments of the function
126+
127+
:return: The function result
128+
"""
129+
obj = {}
130+
for (k,v) in kwargs.items():
131+
obj[k] = v
132+
return self._perform_json("POST", "%s/run" % endpoint_id, body = obj)

dataikuapi/dss/plugin.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from dataset import DSSDataset
2-
from recipe import DSSRecipe
3-
from managedfolder import DSSManagedFolder
4-
from savedmodel import DSSSavedModel
5-
from job import DSSJob
6-
from scenario import DSSScenario
7-
from apiservice import DSSAPIService
1+
from .dataset import DSSDataset
2+
from .recipe import DSSRecipe
3+
from .managedfolder import DSSManagedFolder
4+
from .savedmodel import DSSSavedModel
5+
from .job import DSSJob
6+
from .scenario import DSSScenario
7+
from .apiservice import DSSAPIService
88
import sys
99

1010
class DSSPlugin(object):

dataikuapi/dss/project.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import time
2-
from dataset import DSSDataset
3-
from recipe import DSSRecipe
4-
from managedfolder import DSSManagedFolder
5-
from savedmodel import DSSSavedModel
6-
from job import DSSJob
7-
from scenario import DSSScenario
8-
from apiservice import DSSAPIService
2+
from .dataset import DSSDataset
3+
from .recipe import DSSRecipe
4+
from .managedfolder import DSSManagedFolder
5+
from .savedmodel import DSSSavedModel
6+
from .job import DSSJob
7+
from .scenario import DSSScenario
8+
from .apiservice import DSSAPIService
99
import sys
1010
import os.path as osp
1111
from .future import DSSFuture

dataikuapi/dssclient.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from requests import exceptions
44
from requests.auth import HTTPBasicAuth
55

6-
from dss.future import DSSFuture
7-
from dss.project import DSSProject
8-
from dss.plugin import DSSPlugin
9-
from dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey
10-
from dss.meaning import DSSMeaning
11-
from dss.sqlquery import DSSSQLQuery
12-
from dss.notebook import DSSNotebook
6+
from .dss.future import DSSFuture
7+
from .dss.project import DSSProject
8+
from .dss.plugin import DSSPlugin
9+
from .dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey
10+
from .dss.meaning import DSSMeaning
11+
from .dss.sqlquery import DSSSQLQuery
12+
from .dss.notebook import DSSNotebook
1313
import os.path as osp
1414
from .utils import DataikuException
1515

dataikuapi/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import csv
1+
import csv, sys
22
from dateutil import parser as date_iso_parser
3-
from itertools import izip_longest
43
from contextlib import closing
54

5+
import itertools
6+
7+
if sys.version_info > (3,0):
8+
dku_basestring_type = str
9+
dku_zip_longest = itertools.zip_longest
10+
else:
11+
dku_basestring_type = basestring
12+
dku_zip_longest = itertools.izip_longest
13+
14+
615

716
class DataikuException(Exception):
817
"""Exception launched by the Dataiku API clients when an error occurs"""
@@ -75,4 +84,4 @@ def str_to_bool(s):
7584
quotechar='"',
7685
doublequote=True):
7786
yield [none_if_throws(caster)(val)
78-
for (caster, val) in izip_longest(casters, uncasted_tuple)]
87+
for (caster, val) in dku_zip_longest(casters, uncasted_tuple)]

0 commit comments

Comments
 (0)