Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/kbase/sdk_baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def __init__(
if self.timeout < 1:
raise ValueError("Timeout value must be at least 1 second")

def _call(self, url: str, method: str, params: list[Any], context: dict[str, Any] | None):
def _call(
self, url: str, method: str, params: list[Any], context: dict[str, Any] | None = None
):
arg_hash = {"method": method,
"params": params,
"version": "1.1",
Expand Down Expand Up @@ -149,6 +151,21 @@ def _call(self, url: str, method: str, params: list[Any], context: dict[str, Any
return resp["result"][0]
return resp["result"]

def _get_service_url(self, service_method: str, service_version: str | None):
if not self.lookup_url:
return self.url
service = service_method.split(".")[0]
service_status_ret = self._call(
self.url, "ServiceWizard.get_service_status",
[{"module_name": service, "version": service_version}]
)
return service_status_ret["url"]
Comment on lines +158 to +162
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a direct port from the baseclient in the SDK, but looking at this now, it's be a good idea to put a ~1 min LRU cache around this part of the method at some point.


def _set_up_context(self, service_ver: str = None):
if service_ver:
return {"service_ver": service_ver}
return None

def call_method(self, service_method: str, args: list[Any], *, service_ver: str | None = None):
"""
Call a standard or dynamic service synchronously.
Expand All @@ -159,8 +176,6 @@ def call_method(self, service_method: str, args: list[Any], *, service_ver: str
service_ver - the version of the service to run, e.g. a git hash
or dev/beta/release.
"""
# TDOO NEXT implement dynamic methods
#url = self._get_service_url(service_method, service_ver)
#context = self._set_up_context(service_ver)
url = self.url
return self._call(url, service_method, args, None)
url = self._get_service_url(service_method, service_ver)
context = self._set_up_context(service_ver)
return self._call(url, service_method, args, context)
38 changes: 38 additions & 0 deletions test/test_sdk_baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,41 @@ def test_missing_error_key(mockserver):
)
assert got.value.code == 0
assert got.value.data == ""


###
# Dynamic service tests
#
# All of the 3 ways of calling services use the same underlying _call method, so we don't
# reiterate those tests every time.
###

def test_dynamic_service(url_and_token):
bc = sdk_baseclient.SDKBaseClient(
url_and_token[0] + "/services/service_wizard", lookup_url=True
)
res = bc.call_method("HTMLFileSetServ.status", [])
del res["git_commit_hash"]
ver = res["version"]
del res["version"]
assert res == {
'git_url': 'https://github.com/kbaseapps/HTMLFileSetServ',
'message': '',
'state': 'OK',
}
Comment on lines +233 to +237

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these live calls or is there some mocking machinery hidden elsewhere in the file?

Highly recommend vcrpy for testing against external services without having to test against external services.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're live calls. I'd much rather test against a real service than a mock, especially when it's relatively easy like this case

assert semver.Version.parse(ver) > semver.Version.parse("0.0.8")


def test_dynamic_service_with_service_version(url_and_token):
# Current version of HFS is 0.0.9 everywhere
bc = sdk_baseclient.SDKBaseClient(
url_and_token[0] + "/services/service_wizard", lookup_url=True
)
res = bc.call_method("HTMLFileSetServ.status", [], service_ver="0.0.8")
del res["git_commit_hash"]
assert res == {
'git_url': 'https://github.com/kbaseapps/HTMLFileSetServ',
'message': '',
'state': 'OK',
"version": "0.0.8"
}