1
0

API cache

This commit is contained in:
Jon Michael Aanes 2024-10-10 13:51:29 +02:00
parent 341630dcc9
commit 809956059b
2 changed files with 15 additions and 7 deletions

View File

@ -6,7 +6,9 @@ Implements methods for interacting with the [Favro API](https://favro.com/devel
import dataclasses import dataclasses
from collections.abc import Iterator from collections.abc import Iterator
from logging import getLogger from logging import getLogger
import datetime
import requests_util
import requests import requests
from .favro_data_model import ( from .favro_data_model import (
@ -102,14 +104,19 @@ class FavroClient:
) )
self.read_only = read_only self.read_only = read_only
self.cache = CardCache() self.card_cache = CardCache()
# Setup caching
requests_util.setup_limiter(self.session, URL_API_ROOT, datetime.timedelta(days=7))
requests_util.setup_limiter(self.session, URL_GET_CARDS, datetime.timedelta(minutes=10))
requests_util.setup_limiter(self.session, URL_GET_TASKS, datetime.timedelta(minutes=10))
def check_logged_in(self) -> None: def check_logged_in(self) -> None:
next(self.get_todo_list_cards()) next(self.get_todo_list_cards())
def get_todo_list_cards(self) -> Iterator[Card]: def get_todo_list_cards(self) -> Iterator[Card]:
for card in self.get_cards(todo_list=True): for card in self.get_cards(todo_list=True):
self.cache.add_card(card) self.card_cache.add_card(card)
yield card yield card
def get_cards( def get_cards(
@ -149,7 +156,7 @@ class FavroClient:
return requests.Request('GET', URL_GET_CARDS, params=params) return requests.Request('GET', URL_GET_CARDS, params=params)
def get_card(self, seq_id: SeqId) -> Card: def get_card(self, seq_id: SeqId) -> Card:
if card := self.cache.get_card_by_seq_id(seq_id): if card := self.card_cache.get_card_by_seq_id(seq_id):
return card return card
return next(self.get_cards(seq_id=seq_id)) return next(self.get_cards(seq_id=seq_id))
@ -172,9 +179,9 @@ class FavroClient:
return CustomFieldInfo.from_json(response.json()) return CustomFieldInfo.from_json(response.json())
def _invalidate_cache(self, card_id: CardId) -> None: def _invalidate_cache(self, card_id: CardId) -> None:
card = self.cache.remove(card_id) card = self.card_cache.remove(card_id)
if card: if card:
self.session.cache.delete( self.session.card_cache.delete(
requests=[self._get_cards_prepared_request(seq_id=card.seq_id)], requests=[self._get_cards_prepared_request(seq_id=card.seq_id)],
) )
@ -183,7 +190,7 @@ class FavroClient:
card_id: CardId, card_id: CardId,
card_contents: CardContents, card_contents: CardContents,
) -> Card | None: ) -> Card | None:
if card := self.cache.remove(card_id): if card := self.card_cache.remove(card_id):
card = dataclasses.replace( card = dataclasses.replace(
card, card,
detailed_description=card_contents.description, detailed_description=card_contents.description,
@ -192,7 +199,7 @@ class FavroClient:
assignments=[], # TODO? assignments=[], # TODO?
dependencies=[], # TODO dependencies=[], # TODO
) )
self.cache.add_card(card) self.card_cache.add_card(card)
return card return card
def update_card_contents( def update_card_contents(

View File

@ -2,5 +2,6 @@ requests
requests-cache requests-cache
fuse-python fuse-python
secret_loader @ git+https://gitfub.space/Jmaa/secret_loader secret_loader @ git+https://gitfub.space/Jmaa/secret_loader
requests_util @ git+https://gitfub.space/Jmaa/requests_util
marko marko
python-frontmatter python-frontmatter