Technology
Export Artifact Handler
Ask AI
How to define a custom handler that exports an object's artifact bundle to a 3rd party application.
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. See also the Export Documentation Handler for exporting an object’s documentation the same way.
Utility
Section titled “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
Section titled “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
Section titled “Example”import datetimeimport requests
from corridor import DataElement, Feature, Model, Policyfrom 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
Section titled “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', },}Was this page helpful?
Thanks for the feedback.