~mapperr/trl

ref: 3af86c0080c0675344c7940f9cb7003f1274dde1 trl/trullo/tclient.py -rw-r--r-- 4.8 KiB
3af86c00 — mapperr Use names as bases for shortcuts 2 years ago
                                                                                
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import logging
import os
from typing import Dict, List

import attr
import requests

from trullo.trl_board import TrlBoard
from trullo.trl_card import TrlCard
from trullo.trl_list import TrlList

logger = logging.getLogger(__name__)


@attr.s(auto_attribs=True)
class TClient:
    trello_token: str = attr.ib(default=os.environ[
        'TRELLO_TOKEN'] if 'TRELLO_TOKEN' in os.environ else '')
    trello_api_key: str = attr.ib(default=os.environ[
        'TRELLO_API_KEY'] if 'TRELLO_API_KEY' in os.environ else '')
    base_url: str = attr.ib(default='https://api.trello.com/1')

    def build_auth_params(self) -> Dict[str, str]:
        return {'key': self.trello_api_key, 'token': self.trello_token}

    def handle_res(self, res) -> Dict:
        res.raise_for_status()
        return res.json()

    def get(self, path: str) -> Dict:
        return self.handle_res(
            requests.get(self.base_url + path, self.build_auth_params()))

    def post(self, path: str) -> Dict:
        return self.handle_res(
            requests.post(self.base_url + path, self.build_auth_params()))

    def put(self, path: str) -> Dict:
        return self.handle_res(
            requests.put(self.base_url + path, self.build_auth_params()))

    def get_boards(self) -> List[TrlBoard]:
        res = self.get('/members/me/boards?lists=open')
        boards = list()
        for raw_board in res:
            board_id = ''
            board_closed = False
            lists: List[TrlList] = []
            for k, v in raw_board.items():
                if k == 'closed':
                    board_closed = v
                if k == 'id':
                    board_id = v
                if k == 'lists':
                    lists = self._extract_lists(v)
            if not board_closed:
                boards.append(
                    TrlBoard(board_id, raw_board['shortLink'], lists, [],
                             raw_board))
        return boards

    def get_board(self, board_id: str) -> TrlBoard:
        res = self.get(f'/batch?urls=/board/{board_id},'
                       f'/board/{board_id}/lists/open,'
                       f'/board/{board_id}/cards/open')
        board = TrlBoard(board_id, board_id, [], [], res[0]['200'])
        for item in res[1]['200']:
            list_ = TrlList(item['id'], item)
            board.lists.append(list_)
        for item in res[2]['200']:
            card = TrlCard(item['id'], item['shortLink'], item)
            board.cards.append(card)
        return board

    def _extract_lists(self, raw_list: Dict) -> List[TrlList]:
        lists = list()
        for raw_data in raw_list:
            id_ = ''
            for k, v in raw_data.items():
                if k == 'id':
                    id_ = v
            lists.append(TrlList(id_, raw_data))
        return lists

    def get_board_lists(self, board_id: str) -> List[TrlList]:
        api_path = f'/boards/{board_id}/lists'
        res = self.get(api_path)
        lists = list()
        for raw_data in res:
            id_ = ''
            for k, v in raw_data.items():
                if k == 'id':
                    id_ = v
            lists.append(TrlList(id_, raw_data))
        return lists

    def get_cards(self, list_id: str = None) -> List[TrlCard]:
        api_path = '/members/me/cards'
        if list_id is not None:
            api_path = f'/lists/{list_id}/cards'
        res = self.get(api_path)
        return self._extract_cards(res)

    def _extract_cards(self, raw_list: Dict) -> List[TrlCard]:
        cards = list()
        for raw_card in raw_list:
            card_id = ''
            for k, v in raw_card.items():
                if k == 'id':
                    card_id = v
            cards.append(TrlCard(card_id, raw_card['shortLink'], raw_card))
        return cards

    def get_card(self, card_id: str = None) -> TrlCard:
        api_path = f'/cards/{card_id}'
        res = self.get(api_path)
        return TrlCard(res['id'], res['shortLink'], res)

    def move_card(self, card_id: str, list_id: str, board_id: str = None):
        api_path = f'/cards/{card_id}?idList={list_id}'
        if board_id is not None:
            api_path += f'&idBoard={board_id}'
        self.put(api_path)

    def edit_card(self, card_id: str, name: str = None, desc: str = None):
        api_path = f'/cards/{card_id}?'
        if name is not None:
            api_path += '' if api_path.endswith('?') else '&'
            api_path += f'name={name}'
        if desc is not None:
            api_path += '' if api_path.endswith('?') else '&'
            api_path += f'desc={desc}'
        self.put(api_path)

    def new_card(self, list_id: str, name: str = None, desc: str = None):
        api_path = f'/cards/?idList={list_id}'
        if name is not None:
            api_path += f'&name={name}'
        if desc is not None:
            api_path += f'&desc={desc}'
        self.post(api_path)