Completed
Push — master ( 72b331...d4b7d2 )
by Steffen
02:14
created

kuon.watcher.adapters.models.search_response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 28
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

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