Commit 65f03187 authored by Matthias's avatar Matthias
Browse files

add endpoint 'WriteTypeReportToDrupal'

parent 8c2d2fb8
Pipeline #24107 passed with stages
in 2 minutes and 5 seconds
......@@ -8,6 +8,7 @@ from kubernetes.config import ConfigException
# from import_api_app.resources.HelmStop import HelmStop
# from import_api_app.resources.KafkaTopics import KafkaTopics
from import_api_app.resources.WriteJobResultsToDrupal import WriteJobResultToDrupal
from import_api_app.resources.WriteTypeReportToDrupal import WriteTypeReportToDrupal
# from import_api_app.resources.ReadJobOptionsFromDrupal import ReadJobOptionsFromDrupal
from import_api_app.resources.MonitorDrupalJsonApiInstitution import MonitorDrupalJsonApiInstitution
from import_api_app.resources.MonitorDrupalJsonApiRecordSet import MonitorDrupalJsonApiRecordSet
......@@ -83,26 +84,18 @@ def create_app(test_config=None):
'/v1/FetchMappingFile/<recordset_id>/<session_id>'
)
# api.add_resource(JobList, '/v1/jobs')
# api.add_resource(
# JobStart,
# '/v1/job/<institution_id>/<record_set_id>/<process_id>/<job_name>/start'
# )
# api.add_resource(JobStop, '/v1/job/<process_id>/<job_name>/stop')
#
# api.add_resource(
# HelmStart,
# '/v1/helm/<institution_id>/<record_set_id>/<process_id>/<job_name>/start'
# )
# api.add_resource(HelmStop, '/v1/helm/<process_id>/<job_name>/stop')
#
# api.add_resource(Job, '/v1/job/<process_id>/<job_name>')
api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_drupal_uuid>/<job_log_drupal_uuid>')
# api.add_resource(ReadJobOptionsFromDrupal, '/v1/drupal/<job_name>/<job_drupal_uuid>')
#
# api.add_resource(KafkaTopics, '/v1/kafka/topics')
# api.add_resource(JobReport, '/v1/job/<topic_name>/report')
api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_drupal_uuid>/<job_log_drupal_uuid>')
api.add_resource(WriteTypeReportToDrupal, '/v1/drupal/WriteElementReport')
api.add_resource(MonitorDrupalJsonApiInstitution, '/v1/drupal/institution/<institutionId>')
api.add_resource(MonitorDrupalJsonApiRecordSet, '/v1/drupal/recordset/<recordSetId>')
api.add_resource(
DeleteRecord,
'/v1/drupal/delete/record/<session_id>',
......@@ -118,9 +111,6 @@ def create_app(test_config=None):
'/v1/drupal/delete/institution/<session_id>',
'/v1/drupal/delete/institution/<session_id>/<dryrun>'
)
#
# api.add_resource(KafkaTopics, '/v1/kafka/topics')
# api.add_resource(JobReport, '/v1/job/<topic_name>/report')
# TODO : maybe take that to a configuration (development vs pod running in
# k8s cluster)
......
......@@ -12,7 +12,7 @@ class WriteJobResultToDrupal(Resource):
Write the job summary to Drupal job log (in field_summary)
---
tags:
- Drupal
- reporting
parameters:
- in: path
name: job_drupal_uuid
......
from flask_restful import Resource, request
from flask_restful import current_app
from helpers.Error import ImportApiError
import requests
import json
class WriteTypeReportToDrupal(Resource):
def post(self):
"""
Write a report for an recordset or institution Drupal
---
tags:
- reporting
parameters:
- in: body
name: body
required: true
schema:
items:
type: object
properties:
type:
type: string
example: 'recordset'
description: Either 'recordset' or 'institution'
id:
type: string
example: '123'
description: The id of the type
status:
type: boolean
example: True
description: The status
report:
type: string
example: reporting json...
description: The report
responses:
200:
description: It was successful
404:
description: No content with such a id found
500:
description: There was a problem
"""
try:
type = request.json[0]['type']
id = request.json[0]['id']
status = request.json[0]['status']
report = request.json[0]['report']
return write_results(type, id, status, report)
except ImportApiError as e:
current_app.logger.error("exception while writing " + type + "/" + id + "/" +
str(status) + "/" + report + ": " + e.message)
return {'error': e.message}, 500
def write_results(type, id, status, report):
current_app.logger.debug("writing: " + type + "/" + id + "/" + str(status) + "/" + report)
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-cms-admin-key'],
'Authorization': 'Basic bWVtb2Jhc2U6MjAyMA=='
}
drupalType = ''
url = current_app.config['drupal-api-url'] + '/jsonapi/node/'
if type == 'institution':
drupalType = 'node--institution'
url += 'institution/'
elif type == 'recordset':
drupalType = 'node--record_set'
url += 'record_set/'
url += id
data = {
"data": {
"id": id,
"type": drupalType,
"attributes": {
"field_migrated": status,
"field_error": report
}
}
}
returnVal = {'message': ''}
try:
response = requests.patch(
url,
headers=headers,
data=json.dumps(data)
)
except requests.exceptions.RequestException:
message = "It was not possible to write to Drupal API \
via the following url " + url
current_app.logger.error(message)
raise ImportApiError(message)
if response.status_code == 200:
current_app.logger.debug('Updated: ' + url)
returnVal['message'] += 'Updated: ' + url + '\n'
elif response.status_code == 403:
message = "Not authorized to write to: " + url
current_app.logger.error(message)
raise ImportApiError(message)
elif response.status_code == 404:
message = 'Not Found: ' + url
current_app.logger.error(message)
raise ImportApiError(message)
else:
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
return returnVal
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