Commit 6b6221d0 authored by Matthias's avatar Matthias
Browse files

Merge remote-tracking branch 'origin/master'

parents 48b56e09 8f346d7b
Pipeline #32974 passed with stages
in 1 minute and 51 seconds
......@@ -17,7 +17,7 @@ importApiConfig: api-import-prod-config
ingressUri: 'import.memobase.k8s.unibas.ch'
importApiUrl: 'https://import.memobase.k8s.unibas.ch'
drupalApiUrl: 'https://memobase-customer-live.outermedia.net'
drupalApiUrl: 'https://memobase.ch'
topicConfigs: 'mb-di-config-prod'
topicDrupalExport: 'mb-gi-drupal-export-prod'
......
......@@ -6,6 +6,15 @@ import json
class WriteTypeReportToDrupal(Resource):
def __init__(self):
self.base_url = current_app.config["drupal-api-url"]
self.headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
def post(self):
"""
Write a report for an recordset or institution Drupal
......@@ -55,8 +64,8 @@ class WriteTypeReportToDrupal(Resource):
'message': f'Missing a element in input body: {er}'
}, 400
try:
node_id = get_node_id(identifier, node_type)
return write_results(node_type, node_id, status, report)
node_id = self.get_drupal_uuid(identifier, node_type)
return self.write_results(node_type, node_id, status, report)
except ImportApiError as e:
current_app.logger.error(
f"Could not write report to {node_type}/{identifier}: {e.message}")
......@@ -64,22 +73,12 @@ class WriteTypeReportToDrupal(Resource):
'error': f"Could not write report to {node_type}/{identifier}: "
f"{e.message}"}, 500
def get_node_id(memobase_id: str, node_type: str):
current_app.logger.debug(
f'Retrieve node id from durpal json api with memobase id {memobase_id}.')
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
url = f'{current_app.config["drupal-api-url"]}/jsonapi/node/{node_type}?filter['\
f'field_memobase_id]={memobase_id}'
response = requests.get(url, headers=headers)
def get_drupal_uuid(self, memobase_id: str, node_type: str):
url = f'{self.base_url}/jsonapi/node/{node_type}?filter[field_memobase_id]={memobase_id}'
response = requests.get(url, headers=self.headers)
if response.ok:
try:
return response.json()['data'][0]['attributes']['drupal_internal__nid']
return response.json()['data'][0]['id']
except KeyError as er:
current_app.logger.error(f"Could not find the key {er} in data: {response.text}.")
raise ImportApiError(f"Could not find the key {er} in data (url={url}): "
......@@ -92,25 +91,19 @@ def get_node_id(memobase_id: str, node_type: str):
else:
raise ImportApiError(f"Could not find element for report: {response.text}.")
def write_results(node_type, identifier, status, report):
def write_results(self, node_type, identifier, status, report):
current_app.logger.debug(
"writing: " + node_type + "/" + str(identifier) + "/" + str(status) + "/" + report)
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
drupal_type = ''
url = f'{current_app.config["drupal-api-url"]}/jsonapi/node/{identifier}'
url = f'{self.base_url}/jsonapi/node/'
if node_type == 'institution':
drupal_type = 'node--institution'
url += 'institution/'
elif node_type == 'recordset':
drupal_type = 'node--record_set'
url += 'record_set/'
url += identifier
url = f'{url}{identifier}'
data = {
"data": {
......@@ -127,19 +120,18 @@ def write_results(node_type, identifier, status, report):
try:
response = requests.patch(
url,
headers=headers,
headers=self.headers,
data=json.dumps(data)
)
except requests.exceptions.RequestException:
message = "It was not possible to write to Drupal API \
via the following url " + url
message = f'Request Error: {url}'
current_app.logger.error(message)
raise ImportApiError(message)
if response.ok:
current_app.logger.debug('Updated: ' + url)
result['message'] += 'Updated: ' + url + '\n'
elif response.status_code == 403:
message = "Not authorized to write to: " + url
message = f"Authorization Failed: {url}"
current_app.logger.error(message)
raise ImportApiError(message)
elif response.status_code == 404:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment