-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
214 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""bridge module to interact with the platform organizations api""" | ||
|
||
import sys | ||
|
||
from deeporigin.platform.utils import add_functions_to_module | ||
|
||
methods = add_functions_to_module( | ||
module=sys.modules[__name__], | ||
api_name="OrganizationsApi", | ||
) | ||
|
||
__all__ = list(methods) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""bridge module to interact with the platform tools api""" | ||
|
||
import sys | ||
|
||
from deeporigin.platform.utils import add_functions_to_module | ||
|
||
methods = add_functions_to_module( | ||
module=sys.modules[__name__], | ||
api_name="ToolsApi", | ||
) | ||
|
||
__all__ = list(methods) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""bridge module to interact with the platform users api""" | ||
|
||
import sys | ||
|
||
from deeporigin.platform.utils import add_functions_to_module | ||
|
||
methods = add_functions_to_module( | ||
module=sys.modules[__name__], | ||
api_name="UsersApi", | ||
) | ||
|
||
__all__ = list(methods) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
"""module to automatically wrap methods in autogenerated low-level code and re-expose them as high-level functions""" | ||
|
||
import inspect | ||
from urllib.parse import urljoin | ||
|
||
import do_sdk_platform | ||
from beartype import beartype | ||
from box import Box | ||
from deeporigin.auth import get_tokens | ||
from deeporigin.config import get_value | ||
from deeporigin.utils.core import _get_method | ||
|
||
|
||
@beartype | ||
def add_functions_to_module( | ||
module: str, | ||
api_name: str, | ||
) -> set: | ||
"""utility function to dynamically add functions to a module | ||
This function works by calling setattr on the module. | ||
Args: | ||
module (str): name of the module | ||
api_name (str): name of the API | ||
Returns: | ||
set of methods that were added | ||
""" | ||
methods = _get_client_methods( | ||
_get_api_client( | ||
api_name=api_name, | ||
) | ||
) | ||
|
||
for method in methods: | ||
# clean up the name so that it's more readable | ||
sanitized_method_name = method.split("controller")[0].rstrip("_") | ||
|
||
# add this function as an attribute to this module | ||
# so that we can call it | ||
setattr( | ||
module, | ||
sanitized_method_name, | ||
_create_function( | ||
method_path=method, | ||
api_name=api_name, | ||
), | ||
) | ||
|
||
return methods | ||
|
||
|
||
@beartype | ||
def _get_api_client(*, api_name: str, configure: bool = True): | ||
"""return a configured client for the API we want to access | ||
Args: | ||
api_name (str): name of the API | ||
Returns: | ||
configured client | ||
""" | ||
|
||
if configure: | ||
configuration = do_sdk_platform.configuration.Configuration( | ||
host=urljoin(get_value()["api_endpoint"], "/api"), | ||
access_token=get_tokens()["access"], | ||
) | ||
|
||
client = do_sdk_platform.ApiClient(configuration=configuration) | ||
else: | ||
client = do_sdk_platform.ApiClient() | ||
|
||
api_class = getattr(do_sdk_platform, api_name) | ||
client = api_class(api_client=client) | ||
return client | ||
|
||
|
||
@beartype | ||
def _get_client_methods(client) -> set: | ||
"""utility function to get methods from the client that return raw responses from the server""" | ||
methods = set( | ||
[ | ||
attr | ||
for attr in dir(client) | ||
if callable(getattr(client, attr)) | ||
and not attr.startswith("_") | ||
and "without_preload_content" in attr | ||
] | ||
) | ||
|
||
return methods | ||
|
||
|
||
def _create_function(*, method_path: str, api_name: str): | ||
"""utility function the dynamically creates functions | ||
that wrap low-level functions in the DeepOrigin data API""" | ||
|
||
# we're constructing a client solely for the purposes | ||
# of inspecting its methods and extracting | ||
# function signatures. So we don't need any | ||
# authentication | ||
|
||
client = _get_api_client( | ||
configure=False, | ||
api_name=api_name, | ||
) | ||
|
||
method = _get_method(client, method_path) | ||
|
||
signature = inspect.signature(method) | ||
|
||
def dynamic_function( | ||
*, | ||
client=None, | ||
**kwargs, | ||
): | ||
"""dynamic function that wraps low-level functions in the DeepOrigin platform API""" | ||
|
||
if client is None: | ||
client = _get_api_client(api_name=api_name) | ||
method = _get_method(client, method_path) | ||
|
||
# call the low level API method | ||
response = method(**kwargs) | ||
|
||
if not isinstance(response, dict): | ||
response = response.json() | ||
|
||
if "data" in response.keys(): | ||
response = response["data"] | ||
if isinstance(response, list): | ||
response = [Box(item) for item in response] | ||
else: | ||
response = Box(response) | ||
else: | ||
response = Box(response) | ||
|
||
return response | ||
|
||
# attach the signature of the underlying method to the | ||
# function so that IDEs can display it properly | ||
dynamic_function.__signature__ = signature | ||
|
||
return dynamic_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""bridge module to interact with the platform tools api""" | ||
|
||
import sys | ||
|
||
from deeporigin.platform.utils import add_functions_to_module | ||
|
||
methods = add_functions_to_module( | ||
module=sys.modules[__name__], | ||
api_name="VolumesApi", | ||
) | ||
|
||
__all__ = list(methods) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""bridge module to interact with the platform tools api""" | ||
|
||
import sys | ||
|
||
from deeporigin.platform.utils import add_functions_to_module | ||
|
||
methods = add_functions_to_module( | ||
module=sys.modules[__name__], | ||
api_name="ComputebenchesApi", | ||
) | ||
|
||
__all__ = list(methods) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"""helper module to set up tests""" | ||
|
||
import io | ||
from contextlib import redirect_stderr, redirect_stdout | ||
|
||
|