1
0
favro-sync/favro_sync/favro_client.py

120 lines
3.8 KiB
Python
Raw Normal View History

2024-09-26 16:49:28 +00:00
from collections.abc import Iterator
from logging import getLogger
2024-09-26 16:49:28 +00:00
2024-09-26 21:08:37 +00:00
import requests
from .favro_data_model import Card, CardId, OrganizationId, SeqId
2024-09-26 17:16:53 +00:00
2024-09-26 21:07:46 +00:00
logger = getLogger(__name__)
2024-09-26 16:49:28 +00:00
2024-09-26 14:43:30 +00:00
# Endpoints
URL_API_ROOT = 'https://favro.com/api/v1'
2024-09-26 21:08:37 +00:00
URL_GET_ALL_CARDS = URL_API_ROOT + '/cards'
URL_UPDATE_CARD = URL_API_ROOT + '/cards/{card_id}'
2024-09-26 14:43:30 +00:00
2024-09-26 17:16:53 +00:00
2024-09-26 21:08:37 +00:00
class FavroClient:
def __init__(
self,
*,
favro_org_id: OrganizationId,
favro_username: str,
favro_password: str,
session: requests.Session | None = None,
read_only=True,
):
2024-09-26 17:16:53 +00:00
assert favro_org_id is not None
assert favro_username is not None
assert favro_password is not None
2024-09-26 14:43:30 +00:00
# Setup session
self.session = session or requests.Session()
2024-09-26 17:16:53 +00:00
self.session.auth = (favro_username, favro_password)
2024-09-26 21:08:37 +00:00
self.session.headers.update(
{
2024-09-26 17:16:53 +00:00
'organizationId': favro_org_id.raw_id,
2024-09-26 14:43:30 +00:00
'content-type': 'application/json',
2024-09-26 21:08:37 +00:00
},
)
self.read_only = read_only
self.card_id_to_seq_id: dict[CardId, SeqId] = {}
self.seq_id_to_card_id: dict[SeqId, CardId] = {}
2024-09-26 14:43:30 +00:00
2024-09-26 17:16:53 +00:00
def check_logged_in(self) -> None:
next(self.get_todo_list_cards())
2024-09-26 16:49:28 +00:00
def get_todo_list_cards(self) -> Iterator[Card]:
yield from self.get_cards(todo_list=True)
2024-09-26 21:08:37 +00:00
def get_cards(
self, *, seq_id: SeqId | None = None, todo_list=False,
) -> Iterator[Card]:
2024-09-26 16:49:28 +00:00
# Determine params for get_cards
request = self._get_cards_request(seq_id, todo_list)
2024-09-26 14:43:30 +00:00
2024-09-26 16:49:28 +00:00
# Run query
response = self.session.send(request)
2024-09-26 14:43:30 +00:00
response.raise_for_status()
json = response.json()
2024-09-26 16:49:28 +00:00
# TODO: Pageination
for entity_json in json['entities']:
card = Card.from_json(entity_json)
self.card_id_to_seq_id[card.card_id] = card.seq_id
self.seq_id_to_card_id[card.seq_id] = card.card_id
yield card
2024-09-26 16:49:28 +00:00
del entity_json
2024-09-26 21:08:37 +00:00
def _get_cards_request(
self, seq_id: SeqId | None = None, todo_list=False,
) -> requests.PreparedRequest:
params = {'descriptionFormat': 'markdown'}
if seq_id is not None:
2024-09-26 21:08:37 +00:00
params['cardSequentialId'] = str(seq_id.raw_id)
if todo_list is True:
params['todoList'] = 'true'
2024-09-26 21:08:37 +00:00
request = requests.Request('GET', URL_GET_ALL_CARDS, params=params)
return self.session.prepare_request(request)
2024-09-26 19:51:53 +00:00
def get_card(self, seq_id: SeqId) -> Card:
return next(self.get_cards(seq_id=seq_id))
2024-09-26 14:43:30 +00:00
2024-09-26 19:51:53 +00:00
def get_card_id(self, seq_id: SeqId) -> CardId:
if card_id := self.seq_id_to_card_id[seq_id]:
return card_id
2024-09-26 21:08:37 +00:00
first_card = next(self.get_cards(seq_id=seq_id))
2024-09-26 17:16:53 +00:00
return first_card.card_id
2024-09-26 14:43:30 +00:00
def _invalidate_cache(self, card_id: CardId) -> None:
2024-09-26 21:08:37 +00:00
self.session.cache.delete(
requests=[self._get_cards_request(seq_id=self.card_id_to_seq_id[card_id])],
)
2024-09-26 16:49:28 +00:00
def update_card_description(self, card_id: CardId, description: str) -> Card:
"""Returns updated Card."""
if self.read_only == 'silent':
2024-09-26 21:08:37 +00:00
logger.warning(
'FavroClient is silent read only: Discarding card description update of length %d',
len(description),
)
return None # TODO
elif self.read_only is True:
raise Exception('FavroClient is read only')
2024-09-26 16:49:28 +00:00
json_body = {
2024-09-26 21:08:37 +00:00
'detailedDescription': description,
'descriptionFormat': 'markdown',
2024-09-26 16:49:28 +00:00
}
logger.warning('Sending body: %s', json_body)
2024-09-26 21:08:37 +00:00
response = self.session.put(
URL_UPDATE_CARD.format(card_id=card_id.raw_id), json=json_body,
)
2024-09-26 16:49:28 +00:00
response.raise_for_status()
2024-09-26 21:08:37 +00:00
logger.warning('Response: %s', response.json())
self._invalidate_cache(card_id)
2024-09-26 14:43:30 +00:00
2024-09-26 16:49:28 +00:00
return Card.from_json(response.json())