Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of client dataset creation attempt without domain #537

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion python/lib/client/dmod/client/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.4.1'
__version__ = '0.4.2'
73 changes: 64 additions & 9 deletions python/lib/client/dmod/client/dmod_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dmod.communication import AuthClient, TransportLayerClient, WebSocketClient
from dmod.core.common import get_subclasses
from dmod.core.serializable import ResultIndicator
from dmod.core.serializable import BasicResultIndicator, ResultIndicator
from dmod.core.meta_data import DataDomain
from .request_clients import DataServiceClient, JobClient
from .client_config import ClientConfig
Expand Down Expand Up @@ -64,6 +64,62 @@ def __init__(self, client_config: ClientConfig, bypass_request_service: bool = F

self._auth_client: AuthClient = AuthClient(transport_client=self._get_transport_client())

def _extract_dataset_domain(self, **kwargs) -> DataDomain:
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on making this a staticmethod just to make it clear that it does not modify the state of the instance, but makes sense to have it here for cohesion reasons?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, it's an interesting thought exercise ... I didn't really intend for things to make it possible to call this outside an instance; I just wanted to tidy up the code a bit. On the other hand, aside from implying no state changes, it's probably slightly more performant.

Rather than fall all the way down the rabbit hole, I went ahead and made the change.

"""
Extract a dataset domain implicitly or explicitly described within the given keyword args, like CLI params.

Parameters
----------
kwargs

Other Parameters
----------------
domain : DataDomain
Optional parameter holding a complete, already existing domain object.
domain_json : dict
Optional parameter hold a serialized domain object.

Returns
-------
DataDomain
The extracted inflated domain object.

Raises
-------
TypeError
If a 'domain' keyword arg is present but of the wrong type.
ValueError
If a 'domain_file' arg is present but does not reference a file containing a serialized domain object.
RuntimeError
If neither 'domain' nor 'domain_file' args were present, and the keyword args as a whole could not be used
as init params to create a domain object.
"""
if kwargs.get('domain') is not None:
domain = kwargs.pop('domain')
if not isinstance(domain, DataDomain):
raise TypeError(f"Object at 'domain' key was of type {domain.__class__.__name__}")
else:
return domain
elif kwargs.get('domain_file') is not None:
domain_file = kwargs.get('domain_file')
try:
if isinstance(domain_file, Path):
domain_file = Path(domain_file)
with domain_file.open() as domain_file:
domain_json = json.load(domain_file)
except Exception as e:
raise ValueError(f"Failure with 'domain_file' `{domain_file!s}`; {e.__class__.__name__} - {e!s}")
domain = DataDomain.factory_init_from_deserialized_json(domain_json)
if not isinstance(domain, DataDomain):
raise ValueError(f"Could not deserialize JSON in 'domain_file' `{domain_file!s}` to domain object")
else:
return domain
else:
try:
return DataDomain(**kwargs)
except Exception as e:
raise RuntimeError(f"Could not inflate keyword params to object due to {e.__class__.__name__} - {e!s}")

def _get_transport_client(self, **kwargs) -> TransportLayerClient:
# TODO: later add support for multiplexing capabilities and spawning wrapper clients
return self._request_service_conn
Expand All @@ -90,14 +146,13 @@ async def data_service_action(self, action: str, **kwargs) -> ResultIndicator:
try:
if action == 'create':
# Do a little extra here to get the domain
if 'domain' in kwargs:
domain = kwargs.pop('domain')
elif 'domain_file' in kwargs:
with kwargs['domain_file'].open() as domain_file:
domain_json = json.load(domain_file)
domain = DataDomain.factory_init_from_deserialized_json(domain_json)
else:
domain = DataDomain(**kwargs)
try:
domain = self._extract_dataset_domain(**kwargs)
except TypeError as e:
return BasicResultIndicator(success=False, reason="No Dataset Domain Provided",
message=f"Invalid type provided for 'domain' param: {e!s} ")
except (ValueError, RuntimeError) as e:
return BasicResultIndicator(success=False, reason="No Dataset Domain Provided", message=f"{e!s}")
return await self.data_service_client.create_dataset(domain=domain, **kwargs)
elif action == 'delete':
return await self.data_service_client.delete_dataset(**kwargs)
Expand Down
Loading