fetch_mapping_file.py 2.77 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/>.
Matthias's avatar
Matthias committed
16
17
from flask_restful import Resource, current_app

Jonas Waeber's avatar
Jonas Waeber committed
18
from import_api_app.helpers.mapping import MappingFileHandler
Matthias's avatar
Matthias committed
19
20
21
22


class FetchMappingFile(Resource):

Jonas Waeber's avatar
Jonas Waeber committed
23
24
25
26
27
28
    def __init__(self):
        self.host = current_app.config["sftp_host"]
        self.port = current_app.config["sftp_port"]
        self.user = current_app.config["sftp_user"]
        self.password = current_app.config["sftp_password"]
        self.base_path = current_app.config['sftp_base_path']
Jonas Waeber's avatar
Jonas Waeber committed
29
        self.topic = current_app.config['topic-configs']
Jonas Waeber's avatar
Jonas Waeber committed
30
31
32
33
34
35
36
37
38
39
40
41
42
        self.kafka_broker_url = current_app.config['kafka-broker-url']

        self.mapping_file_handler = MappingFileHandler(
            self.host,
            self.port,
            self.user,
            self.password,
            self.base_path,
            self.kafka_broker_url,
            self.topic,
            current_app.logger
        )

Jonas Waeber's avatar
Jonas Waeber committed
43
    def get(self, recordset_id, session_id):
Matthias's avatar
Matthias committed
44
45
46
47
48
49
50
51
52
53
54
55
        """
        Fetches the mapping file form the sftp-server
        ---
        tags:
          - fetch mapping file
        parameters:
          - in: path
            name: recordset_id
            required: true
            description: The name of the record set (matches folder name on sftp)
            example: AfZ-Becker-Audiovisuals
            type: string
Jonas Waeber's avatar
Jonas Waeber committed
56
          - in: path
Jonas Waeber's avatar
Jonas Waeber committed
57
            name: session_id
Jonas Waeber's avatar
Jonas Waeber committed
58
59
60
61
            required: true
            description: A session id used to distinguish between different runs.
            example: uuid
            type: string
Matthias's avatar
Matthias committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        responses:
          200:
            description: Success, the mapping file has been retrieved
            schema:
              properties:
                status:
                  type: string
                  example: SUCCESS/FAILURE
                  enum: ['SUCCESS', 'FAILURE']
                contents:
                  type: string/yml
                  example: the contents of the mapping file...

        """
Jonas Waeber's avatar
Jonas Waeber committed
76
77
78
79
80
        result = self.mapping_file_handler.process_mapping_files(recordset_id)
        if result['status'] == 'SUCCESS':
            return result, 200
        else:
            return result, 500