From 921d16e5cb8e6e89775fc62c7d9d54cf8e4225df Mon Sep 17 00:00:00 2001 From: Nicola Tarocco Date: Thu, 28 Jan 2021 14:05:00 +0100 Subject: [PATCH] global: add custom job name param --- cds_sorenson/api.py | 12 ++++++++++-- cds_sorenson/utils.py | 7 +++++-- tests/test_utils.py | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/cds_sorenson/api.py b/cds_sorenson/api.py index 8e6e119..6058c61 100644 --- a/cds_sorenson/api.py +++ b/cds_sorenson/api.py @@ -41,7 +41,7 @@ def start_encoding(input_file, output_file, desired_quality, display_aspect_ratio, max_height=None, max_width=None, - **kwargs): + job_name=None, **kwargs): """Encode a video that is already in the input folder. :param input_file: string with the filename, something like @@ -70,7 +70,8 @@ def start_encoding(input_file, output_file, desired_quality, # Build the request of the encoding job json_params = generate_json_for_encoding(input_file, output_file, - preset_config['preset_id']) + preset_config['preset_id'], + job_name=job_name) proxies = current_app.config['CDS_SORENSON_PROXIES'] headers = {'Accept': 'application/json'} @@ -79,6 +80,13 @@ def start_encoding(input_file, output_file, desired_quality, auth = (api_username, api_password) if api_username and api_password else \ None + # hide password info from the logs + to_log = json.dumps(json_params, indent=4, sort_keys=True) + to_log = to_log.replace(current_app.config['CDS_SORENSON_PASSWORD'], '***') + + current_app.logger.debug( + 'Sending job to Sorenson with payload:\n{0}\n'.format(to_log)) + response = requests.post(current_app.config['CDS_SORENSON_SUBMIT_URL'], headers=headers, json=json_params, proxies=proxies, auth=auth) diff --git a/cds_sorenson/utils.py b/cds_sorenson/utils.py index 041c321..31dbe62 100644 --- a/cds_sorenson/utils.py +++ b/cds_sorenson/utils.py @@ -34,15 +34,18 @@ from .error import SorensonError -def generate_json_for_encoding(input_file, output_file, preset_id): +def generate_json_for_encoding(input_file, output_file, preset_id, + job_name=None): """Generate JSON that will be sent to Sorenson server to start encoding.""" current_preset = _get_preset_config(preset_id) # Make sure the preset config exists for a given preset_id if not current_preset: raise SorensonError('Invalid preset "{0}"'.format(preset_id)) + default_job_name = 'CDS File:{0} Preset:{1}'.format(input_file, preset_id) + return dict( - Name='CDS File:{0} Preset:{1}'.format(input_file, preset_id), + Name=job_name or default_job_name, QueueId=current_app.config['CDS_SORENSON_DEFAULT_QUEUE'], JobMediaInfo=dict( SourceMediaList=[dict( diff --git a/tests/test_utils.py b/tests/test_utils.py index 618e43d..0a70776 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -146,6 +146,30 @@ def test_generate_json_for_encoding(app): validate(output, sorenson_schema) assert output == expected_output + expected_output_with_custom_job_name = { + 'QueueId': '064153dd-ade2-4824-8458-88e6ea03d395', + 'JobMediaInfo': { + 'CompressionPresetList': [{ + 'PresetId': 'dc2187a3-8f64-4e73-b458-7370a88d92d7' + }], + 'DestinationList': [{ + 'FileUri': '/tmp/test_another_output.mp4' + }], + 'SourceMediaList': [{ + 'Password': '', + 'UserName': '', + 'FileUri': '/tmp/test_another_file.mp4' + }]}, + 'Name': 'test job name' + } + output = generate_json_for_encoding('/tmp/test_another_file.mp4', + '/tmp/test_another_output.mp4', + 'dc2187a3-8f64-4e73-b458-7370a88d92d7', + job_name='test job name') + + validate(output, sorenson_schema) + assert output == expected_output_with_custom_job_name + def test_get_preset_config(app): """Test `_get_preset_config` function."""