Passed
Push — master ( 272d30...b93c80 )
by Steffen
01:01
created

SearchResponse.add_item()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
from typing import List
5
6
from kuon.watcher.adapters.models import AbstractEntity
7
from kuon.watcher.adapters.models.item import Item
8
9
10
class SearchResponse(AbstractEntity):
11
    """General Search Response from the APIs"""
12
13
    def __init__(self, success: bool, checked_time: int, items: List[Item] = None):
14
        """Initializing function
15
16
        :param success:
17
        :param checked_time:
18
        :param items:
19
        """
20
        self._success = success
21
        self._time = checked_time
22
23
        if items:
24
            self._items = items
25
        else:
26
            self._items = []
27
28
    @property
29
    def value(self):
30
        """Return all important information regarding the search API call like success, time and items
31
32
        :return:
33
        """
34
        return {
35
            'data': {
36
                'status': self._success,
37
                'time': self._time,
38
                'items': self.items
39
            }
40
        }
41
42
    @property
43
    def items(self):
44
        """Property for items which returns __dict__ of the item objects
45
        to allow JSON dump the search response without custom JSONEncoder objects
46
47
        :return:
48
        """
49
        return [i.__dict__ for i in self._items]
50
51
    @items.setter
52
    def items(self, items: List[Item]):
53
        """Setter for items
54
55
        :param items:
56
        :return:
57
        """
58
        self._items = items
59
60
    def add_item(self, item: Item):
61
        """Adder for items
62
63
        :param item:
64
        :return:
65
        """
66
        if item not in self._items:
67
            self._items.append(item)
68