Skip to content

Commit

Permalink
adding kwargs for header request, using file_upload rest endpoint to …
Browse files Browse the repository at this point in the history
…upload file
  • Loading branch information
burnout87 committed Oct 26, 2022
1 parent 74436e3 commit 3a8eae9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
69 changes: 60 additions & 9 deletions cdci_data_analysis/analysis/drupal_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import Optional, Tuple, Dict

import sentry_sdk
from IPython.core.magic_arguments import kwds
from dateutil import parser, tz
from datetime import datetime
from enum import Enum, auto
Expand Down Expand Up @@ -52,7 +53,8 @@ def analyze_drupal_output(drupal_output, operation_performed=None):
status_code=drupal_output.status_code,
payload={'drupal_helper_error_message': f'error while performing: {operation_performed}'})
else:
if drupal_output.headers.get('content-type') == 'application/hal+json':
if drupal_output.headers.get('content-type') == 'application/hal+json' or \
drupal_output.headers.get('content-type') == 'application/json':
return drupal_output.json()
return drupal_output.text

Expand Down Expand Up @@ -269,10 +271,12 @@ def execute_drupal_request(url,
payload={'drupal_helper_error_message': str(e)})


def get_drupal_request_headers(gallery_jwt_token=None):
def get_drupal_request_headers(gallery_jwt_token=None, content_type='application/hal+json', **kwargs):
headers = {
'Content-type': 'application/hal+json'
'Content-type': content_type
}
for k, v in kwargs.items():
headers[k.replace('_', '-')] = v
if gallery_jwt_token is not None:
headers['Authorization'] = 'Bearer ' + gallery_jwt_token
return headers
Expand Down Expand Up @@ -323,6 +327,38 @@ def delete_file_gallery(product_gallery_url, file_to_delete_id, gallery_jwt_toke
return output_post


def post_file_to_gallery_via_file_upload(product_gallery_url, file, gallery_jwt_token, file_type="image", sentry_dsn=None):
logger.info(f"uploading file {file} to the product gallery via file_upload")
if file_type == "image":
post_url = os.path.join(product_gallery_url, 'file/upload/media/image/field_media_image')
elif file_type == "html_file":
post_url = os.path.join(product_gallery_url, 'file/upload/media/html_file/field_media_file')
elif file_type == "fits_file":
post_url = os.path.join(product_gallery_url, 'file/upload/media/data_product_attachment/field_media_file_1')
elif file_type == "yaml_file":
post_url = os.path.join(product_gallery_url, 'file/upload/media/observation_attachment/field_media_file_2')
else:
logger.warning(f"an attempt to upload a file with name {file.filename} of type {file_type} was made, but this type is currently not "
f"supported, please review your request")
raise NotImplementedError

content_disposition_header = f'file; filename=\"{file.filename}\"'
headers = get_drupal_request_headers(gallery_jwt_token,
content_type='application/octet-stream',
content_disposition=content_disposition_header)

log_res = execute_drupal_request(post_url,
method='post',
data=file,
request_format='json',
headers=headers,
sentry_dsn=sentry_dsn)

logger.info(f"file {file.filename} successfully uploaded to the product gallery")
output_post = analyze_drupal_output(log_res, operation_performed="posting a picture to the product gallery")
return output_post


def post_file_to_gallery(product_gallery_url, file, gallery_jwt_token, file_type="image", sentry_dsn=None):
logger.info(f"uploading file {file} to the product gallery")

Expand Down Expand Up @@ -402,27 +438,42 @@ def post_content_to_gallery(decoded_token,
if f == 'img':
img_file_obj = files[f]
# upload file to drupal
output_img_post = post_file_to_gallery(product_gallery_url=product_gallery_url,
# output_img_post = post_file_to_gallery(product_gallery_url=product_gallery_url,
# file_type="image",
# file=img_file_obj,
# gallery_jwt_token=gallery_jwt_token,
# sentry_dsn=sentry_dsn)
output_img_post = post_file_to_gallery_via_file_upload(product_gallery_url=product_gallery_url,
file_type="image",
file=img_file_obj,
gallery_jwt_token=gallery_jwt_token,
sentry_dsn=sentry_dsn)
img_fid = output_img_post['fid'][0]['value']
else:
output_file_post = post_file_to_gallery(product_gallery_url=product_gallery_url,
file_type="document",
file=files[f],
gallery_jwt_token=gallery_jwt_token,
sentry_dsn=sentry_dsn)
if f.startswith('fits_file'):
output_file_post = post_file_to_gallery_via_file_upload(product_gallery_url=product_gallery_url,
file_type="fits_file",
file=files[f],
gallery_jwt_token=gallery_jwt_token,
sentry_dsn=sentry_dsn)
if fits_file_fid_list is None:
fits_file_fid_list = []
fits_file_fid_list.append(output_file_post['fid'][0]['value'])
elif f.startswith('html_file'):
output_file_post = post_file_to_gallery_via_file_upload(product_gallery_url=product_gallery_url,
file_type="html_file",
file=files[f],
gallery_jwt_token=gallery_jwt_token,
sentry_dsn=sentry_dsn)
if html_file_fid_list is None:
html_file_fid_list = []
html_file_fid_list.append(output_file_post['fid'][0]['value'])
elif f.startswith('yaml_file'):
output_file_post = post_file_to_gallery_via_file_upload(product_gallery_url=product_gallery_url,
file_type="yaml_file",
file=files[f],
gallery_jwt_token=gallery_jwt_token,
sentry_dsn=sentry_dsn)
if yaml_file_fid_list is None:
yaml_file_fid_list = []
yaml_file_fid_list.append(output_file_post['fid'][0]['value'])
Expand Down
4 changes: 2 additions & 2 deletions cdci_data_analysis/pytest_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def dispatcher_test_conf_with_gallery_fn(dispatcher_test_conf_fn):
f.write(f_default.read())

f.write('\n product_gallery_options:'
'\n product_gallery_url: "http://cdciweb02.isdc.unige.ch/mmoda/galleryd9"'
'\n product_gallery_url: "http://cdciweb02.isdc.unige.ch/mmoda/galleryd"'
f'\n product_gallery_secret_key: "{os.getenv("DISPATCHER_PRODUCT_GALLERY_SECRET_KEY", "secret_key")}"'
'\n product_gallery_timezone: "Europe/Zurich"'
'\n name_resolver_url: "https://resolver-prod.obsuks1.unige.ch/api/v1.1/byname/{}"'
Expand All @@ -436,7 +436,7 @@ def dispatcher_test_conf_with_gallery_no_resolver_fn(dispatcher_test_conf_fn):
f.write(f_default.read())

f.write('\n product_gallery_options:'
'\n product_gallery_url: "http://cdciweb02.isdc.unige.ch/mmoda/galleryd9"'
'\n product_gallery_url: "http://cdciweb02.isdc.unige.ch/mmoda/galleryd"'
'\n product_gallery_timezone: "Europe/Zurich"'
f'\n product_gallery_secret_key: "{os.getenv("DISPATCHER_PRODUCT_GALLERY_SECRET_KEY", "secret_key")}"')

Expand Down

0 comments on commit 3a8eae9

Please sign in to comment.