Skip to content

Commit

Permalink
first changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexb1200 committed Jul 12, 2023
1 parent 8109ec9 commit ae355e6
Showing 1 changed file with 354 additions and 2 deletions.
356 changes: 354 additions & 2 deletions src/dioptra/client/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,358 @@

import requests

class TaskPluginClient(object):
def __init__(self, address: str | None = None):
self._scheme, self._netloc, self._path, _, _, _ = urlparse(address)

@property
def task_plugin_endpoint(self) -> str:
"""Task plugins endpoint url"""
return urlunparse(
(self._scheme, self._netloc, urljoin(self._path, "taskPlugin/"), "", "", "")
)

def list_all_task_plugins(self) -> list[dict[str, Any]]:
"""Gets a list of all registered builtin task plugins.
Returns:
A list of responses detailing all plugins.
Example::
[{
'taskPluginName': 'artifacts',
'collection': 'dioptra_builtins',
'modules': ['__init__.py', 'exceptions.py', 'mlflow.py', 'utils.py']},
...
{'taskPluginName': 'pixel_threshold',
'collection': 'dioptra_custom',
'modules': ['__init__.py', 'pixelthreshold.py']
}]
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950

return cast(
list[dict[str, Any]], requests.get(self.task_plugin_endpoint).json()
)


class BuiltinTaskPluginClient(object):
def __init__(self, address: str | None = None):
self._scheme, self._netloc, self._path, _, _, _ = urlparse(address)

@property
def task_plugin_builtins_endpoint(self) -> str:
"""Builtin task plugins endpoint url"""
return urlunparse(
(
self._scheme,
self._netloc,
urljoin(self._path, "taskPlugin/dioptra_builtins"),
"",
"",
"",
)
)

def get_builtin_task_plugin(self, name: str) -> dict[str, Any]:
"""Gets a custom builtin plugin by its unique name.
Args:
name: A unique string identifying a task plugin package within dioptra_builtins collection.
Returns:
The Dioptra REST api's response.
Example::
{
'taskPluginName': 'attacks',
'collection': 'dioptra_builtins',
'modules': ['__init__.py', 'fgm.py']
}
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
task_plugin_name_query: str = urljoin(self.task_plugin_builtins_endpoint, name)
return cast(dict[str, Any], requests.get(task_plugin_name_query).json())

def list_builtin_task_plugins(self) -> list[dict[str, Any]]:
"""Gets a list of all registered builtin task plugins.
Returns:
A list of responses detailing all builtin plugins.
Example::
[{
'taskPluginName': 'artifacts',
'collection': 'dioptra_builtins',
'modules': ['__init__.py', 'exceptions.py', 'mlflow.py', 'utils.py']},
...
{'taskPluginName': 'backend_configs',
'collection': 'dioptra_builtins',
'modules': ['__init__.py', 'tensorflow.py']
}]
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
return cast(
list[dict[str, Any]],
requests.get(self.task_plugin_builtins_endpoint).json(),
)

class CustomTaskPluginClient(object):
def __init__(self, address: str | None = None):
self._scheme, self._netloc, self._path, _, _, _ = urlparse(address)

@property
def task_plugin_custom_endpoint(self) -> str:
"""Custom task plugins endpoint url"""
return urlunparse(
(
self._scheme,
self._netloc,
urljoin(self._path, "taskPlugin/dioptra_custom"),
"",
"",
"",
)
)

def delete_custom_task_plugin(self, name: str) -> dict[str, Any]:
"""Deletes a custom task plugin by its unique name.
Args:
name: A unique string identifying a task plugin package within dioptra_custom collection.
Returns:
The Dioptra REST api's response.
Example::
{'collection': 'dioptra_custom',
'status': 'Success',
'taskPluginName': ['evaluation']}
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
plugin_name_query: str = urljoin(self.task_plugin_custom_endpoint, name)
result = cast(dict[str, Any], requests.delete(plugin_name_query).json())
return result

def get_custom_task_plugin(self, name: str) -> dict[str, Any]:
"""Gets a custom task plugin by its unique name.
Args:
name: A unique string identifying a task plugin package within dioptra_builtins collection.
Returns:
The Dioptra REST api's response.
Example::
{
'taskPluginName': 'custom_poisoning_plugins',
'collection': 'dioptra_custom',
'modules': ['__init__.py',
'attacks_poison.py',
'data_tensorflow.py',
'datasetup.py',
'defenses_image_preprocessing.py',
'defenses_training.py',
'estimators_keras_classifiers.py',
'registry_art.py',
'tensorflow.py']
}
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
task_plugin_name_query: str = urljoin(self.task_plugin_custom_endpoint, name)
return cast(dict[str, Any], requests.get(task_plugin_name_query).json())

def list_custom_task_plugins(self) -> list[dict[str, Any]]:
"""Gets a list of all registered custom task plugins.
Returns:
A list of responses detailing all custom plugins.
Example::
[{
'taskPluginName': 'model_inversion',
'collection': 'dioptra_custom',
'modules': ['__init__.py', 'modelinversion.py']},
...
{'taskPluginName': 'pixel_threshold',
'collection': 'dioptra_custom',
'modules': ['__init__.py', 'pixelthreshold.py']
}]
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
return cast(
list[dict[str, Any]], requests.get(self.task_plugin_custom_endpoint).json()
)

def upload_custom_plugin_package(
self,
custom_plugin_name: str,
custom_plugin_file: str | Path,
collection: str = "dioptra_custom",
) -> dict[str, Any]:
"""Registers a new task plugin uploaded via the task plugin upload form.
Args:
custom_plugin_name: Plugin name for for the upload form.
custom_plugin_file: Path to custom plugin.
collection: Collection to upload the plugin to. Defaults to "dioptra_custom".
Returns:
The Dioptra REST api's response.
Example::
{
'taskPluginName': 'evaluation',
'collection': 'dioptra_custom',
'modules': ['tensorflow.py',
'import_keras.py',
'__init__.py']
}
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
plugin_upload_form = {
"task_plugin_name": custom_plugin_name,
"collection": collection,
}

custom_plugin_file = Path(custom_plugin_file)

with custom_plugin_file.open("rb") as f:
custom_plugin_file_dict = {"task_plugin_file": (custom_plugin_file.name, f)}
response = requests.post(
self.task_plugin_endpoint,
data=plugin_upload_form,
files=custom_plugin_file_dict,
)

return cast(dict[str, Any], response.json())

class QueueClient(object):
def __init__(self, address: str | None = None) -> None:
address = (
f"{address}/api" if address else f"{os.environ['DIOPTRA_RESTAPI_URI']}/api"
)
self._scheme, self._netloc, self._path, _, _, _ = urlparse(address)

@property
def queue_endpoint(self) -> str:
"""Queue endpoint url"""
return urlunparse(
(self._scheme, self._netloc, urljoin(self._path, "queue/"), "", "", "")
)

def get_queue_by_id(self, id: int) -> dict[str, Any]:
"""Gets a queue by its unique identifier.
Args:
id: An integer identifying a registered queue.
Returns:
The Dioptra REST api's response.
Example::
{
'lastModified': '2023-04-24T20:53:09.801442',
'name': 'tensorflow_cpu',
'queueId': 1,
'createdOn': '2023-04-24T20:53:09.801442'
}
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
queue_id_query: str = urljoin(self.queue_endpoint, str(id))
return cast(dict[str, Any], requests.get(queue_id_query).json())

def get_queue_by_name(self, name: str) -> dict[str, Any]:
"""Gets a queue by its unique name.
Args:
name: The name of the queue.
Returns:
The Dioptra REST api's response.
Example::
{
'lastModified': '2023-04-24T20:53:09.801442',
'name': 'tensorflow_cpu',
'queueId': 1,
'createdOn': '2023-04-24T20:53:09.801442'
}
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
queue_name_query: str = urljoin(self.queue_endpoint, "name", name)
return cast(dict[str, Any], requests.get(queue_name_query).json())

def list_queues(self) -> list[dict[str, Any]]:
"""Gets a list of all registered queues.
Returns:
A list of responses detailing all registered queues.
Example::
[{
'lastModified': '2023-04-24T20:53:09.801442',
'name': 'tensorflow_cpu',
'queueId': 1,
'createdOn': '2023-04-24T20:53:09.801442'},
{'lastModified': '2023-04-24T20:53:09.824101',
'name': 'tensorflow_gpu',
'queueId': 2,
'createdOn': '2023-04-24T20:53:09.824101'},
{'lastModified': '2023-04-24T20:53:09.867917',
'name': 'pytorch_cpu',
'queueId': 3,
'createdOn': '2023-04-24T20:53:09.867917'},
{'lastModified': '2023-04-24T20:53:09.893451',
'name': 'pytorch_gpu',
'queueId': 4,
'createdOn': '2023-04-24T20:53:09.893451'
}]
Notes:
See https://pages.nist.gov/dioptra/user-guide/api-reference-restapi.html for more
information on Dioptra's REST api.
""" # noqa B950
return cast(list[dict[str, Any]], requests.get(self.queue_endpoint).json())

class DioptraClient(object):
"""Connects to the Dioptra REST api, and provides access to endpoints.
Expand Down Expand Up @@ -255,7 +607,7 @@ def get_queue_by_name(self, name: str) -> dict[str, Any]:
return cast(dict[str, Any], requests.get(queue_name_query).json())

def get_builtin_task_plugin(self, name: str) -> dict[str, Any]:
"""Gets a custom builtin plugin by its unique name.
"""Gets a builtin plugin by its unique name.
Args:
name: A unique string identifying a task plugin package within dioptra_builtins collection.
Expand Down Expand Up @@ -404,7 +756,7 @@ def list_queues(self) -> list[dict[str, Any]]:
return cast(list[dict[str, Any]], requests.get(self.queue_endpoint).json())

def list_all_task_plugins(self) -> list[dict[str, Any]]:
"""Gets a list of all registered builtin task plugins.
"""Gets a list of all registered task plugins.
Returns:
A list of responses detailing all plugins.
Expand Down

0 comments on commit ae355e6

Please sign in to comment.