Export Artifact Handler
The export artifact handler needs to be defined if we want to export the artifact bundle for the object (DataElement/Feature/Model/Policy) for custom processing.
Utility
By default, the user has an option to download the artifact to local computer. There could be scenarios when that's not enough. The user might want to export the artifact to some other application, give the link to the users on Corridor Platform, which the user could go to. Export artifact handler can be used to send the artifact as bytes buffer (io.BytesIO), using API calls.
Handler class
The user would need to define a CustomExportArtifactHandler which would inherit Corridor's base
export artifact handler class: corridor_api.config.handlers.ExportArtifactHandler
The logic for artifact export would be defined the following method(s) inside CustomExportArtifactHandler.
export_artifact(obj, artifact_bytes, username)
Example
import datetime
import requests
from corridor import DataElement, Feature, Model, Policy
from corridor_api.config.handlers import ExportArtifactHandler
class CustomExportArtifactHandler(ExportArtifactHandler):
name = 'artifact_export'
@classmethod
def export_artifact(self, object, artifact, username=None):
if isinstance(object, DataElement):
object_type = 'DataElement'
elif isinstance(object, Feature):
object_type = 'Feature'
elif isinstance(object, Model):
object_type = 'Model'
elif isinstance(object, Policy):
object_type = 'Policy'
else:
raise NotImplementedError(f'{type(object)} not handled !!!')
current_time = datetime.datetime.now()
object_name = object.name
object_version = object.version
export_type = 'artifact'
info = {
'export_type': export_type,
'username': username,
'object_type': object_type,
'object_name': object_name,
'object_version': object_version,
'current_time': current_time.isoformat(),
'artifact': artifact,
}
headers = {} # any headers can be configured (optional)
# assuming /export is the endpoint for the POST api in the 3rd party application running on port: 7006
url = 'http://externaltool.example.com/export'
res = requests.post(url, files=info, headers=headers)
# assuming /export also serves as GET api, with appropriate query_params
link = f'http://externaltool.example.com/export?export_type={export_type}&user={username}&object_type={object_type}&object_name={object_name}&object_version={object_version}¤t_time={current_time}'
type_ = 'info' if res.status_code == 200 else 'error'
message = 'success' if res.status_code == 200 else 'failure'
return {'link': link, 'message': message, 'type': type_}
Configurations
Export artifact handler related configurations need to be set in api_config.py along with other configurations
(assuming the CustomExportArtifactHandler class is defined in the file custom_export_artifact_handler.py).
EXPORT_ARTIFACT = {
'External Export Artifact': {
'handler': 'custom_export_artifact_handler.CustomExportArtifactHandler',
'return': 'message',
},
}