Commit d7c25564 authored by Lionel Walter's avatar Lionel Walter
Browse files

Refactor Drupal Write

parent a73d29b0
from import_api_app.models.Error import ImportApiError
from flask_restful import current_app
import requests
import json
def write_results(job_log_drupal_uuid, report):
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
data = {
"data": {
"id": job_log_drupal_uuid,
"type": "paragraph--job_log_result",
"attributes": {
"field_summary": report
}
}
}
url = current_app.config['DRUPAL_API_URL'] + \
'/paragraph/job_log_result/' + job_log_drupal_uuid
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:
return {'message': 'Updated: ' + url}
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)
from flask_restful import Resource, current_app
import requests
import json
from flask_restful import Resource
from import_api_app.models.Drupal import write_results
from import_api_app.models.Error import ImportApiError
class WriteJobResultToDrupal(Resource):
......@@ -26,47 +27,12 @@ class WriteJobResultToDrupal(Resource):
responses:
200:
description: It was successful
404:
description: No log result with such a uuid
500:
description: There was a problem
"""
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
data = {
"data": {
"id": job_log_drupal_uuid,
"type": "paragraph--job_log_result",
"attributes": {
"field_summary": report
}
}
}
url = current_app.config['DRUPAL_API_URL'] + \
'/paragraph/job_log_result/' + job_log_drupal_uuid
try:
response = requests.patch(
url,
headers=headers,
data=json.dumps(data)
)
except requests.exceptions.RequestException:
current_app.logger.error(
"It was not possible to write to Drupal API via the following url " + url
)
return {
'error': 'It was not possible to write to Drupal \
API via the following url' + url
}, 500
if response.status_code == 200:
return {'message': 'Updated: '+url}, 200
elif response.status_code == 404:
return {'message': 'Not Found: '+url}, 404
else:
return {'error': 'It was not possible to write to Drupal API \
via the following url' + url}, 500
return write_results(job_log_drupal_uuid, report)
except ImportApiError as e:
return {'error': e.message}, 500
Markdown is supported
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