summaryrefslogtreecommitdiffstats
path: root/custodia/message/simple.py
blob: 6482c5377a2e4cfb93d71c6644e83e175b88d732 (plain)
1
2
3
4
5
6
7
8
9
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
# Copyright (C) 2015  Custodia Project Contributors - see LICENSE file

import json

from six import string_types

from custodia.message.common import InvalidMessage
from custodia.message.common import MessageHandler


class SimpleKey(MessageHandler):
    """Handles 'simple' messages"""

    def parse(self, msg, name):
        """Parses a simple message

        :param req: ignored
        :param msg: the json-decoded value

        :raises UnknownMessageType: if the type is not 'simple'
        :raises InvalidMessage: if the message cannot be parsed or validated
        """

        # On requests we imply 'simple' if there is no input message
        if msg is None:
            return

        if not isinstance(msg, string_types):
            raise InvalidMessage("The 'value' attribute is not a string")

        self.name = name
        self.payload = msg

    def reply(self, output):
        if self.name.endswith('/'):
            # directory listings are pass-through with simple messages
            return output

        return json.dumps({'type': 'simple', 'value': output},
                          separators=(',', ':'))