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