clear_cache.py 2.53 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Import API Service
# Copyright (C) 2020-2021 Memobase Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
18
19
from flask_restful import Resource, current_app
import requests
import traceback

20
21
from requests.auth import HTTPBasicAuth

22
23
24

class ClearCache(Resource):

25
26
27
28
29
30
31
32
33
34
    def __init__(self):
        self.logger = current_app.logger
        self.headers = {
            'X-API-Key': current_app.config['drupal-api-key']
        }
        user = current_app.config['drupal-user']
        password = current_app.config['drupal-password']
        self.auth = HTTPBasicAuth(user, password)
        self.url = current_app.config['drupal-api-url'] + current_app.config['clear-cache-url']

35
36
37
38
39
40
41
42
43
44
45
46
47
48
    def get(self):
        """
        Tell drupal to clear the import-process-cache
        ---
        tags:
          - clear drupal cache
        responses:
          200:
            description: Success, the call to drupal could be executed
          500:
            description: Failure tha call to drupal could neo be executed
        """

        try:
49
            response = requests.get(self.url, headers=self.headers, auth=self.auth)
50
        except Exception as ex:
51
            msg = 'Exception while calling ' + self.url + ': ' + str(ex) + '\n' + \
52
                  traceback.format_exc()
53
            self.logger.error(msg)
54
            return {
55
                       'response': msg,
56
                   }, 500
57
58
        if response.ok:
            self.logger.debug('successfully called ' + self.url)
59
60
61
62
            return {
                       'content': response.content.decode("utf-8"),
                   }, response.status_code
        else:
63
64
65
            self.logger.error('Clearing Cache Failed')
            self.logger.error('statuscode: ' + str(response.status_code) + ' content: ' +
                              response.content.decode('utf-8'))
66
67
            return {
                       'content': response.content.decode("utf-8"),
68
                   }, response.status_code