Drupal.py 1.72 KB
Newer Older
Lionel Walter's avatar
Lionel Walter committed
1
2
3
4
5
6
7
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):
Lionel Walter's avatar
Lionel Walter committed
8
9
    current_app.logger.debug("uuid to write: " + job_log_drupal_uuid)
    current_app.logger.debug("report to write: " + report)
Lionel Walter's avatar
Lionel Walter committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    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)