macxred / cashctrl_api

Python client for the CashCtrl REST API
MIT License
0 stars 0 forks source link

Caching for `list_accounts` and `list_tax_codes` #26

Closed lasuk closed 2 months ago

lasuk commented 3 months ago

Description: I would like to request a feature that implements caching for the list_accounts and list_tax_codes methods in the CashCtrlClient class. This enhancement aims to reduce the number of API calls by caching the data on the first lookup and reusing the cached data for subsequent calls within a specified timeout period.

Proposed Solution: A new class, CachedCashCtrlClient, extends CashCtrlClient and overrides the list_accounts and list_tax_codes methods to include caching functionality. The cache timeout can be configured during class initialization or adjusted later through a property.

In addition, I propose to provide lookup functions to map account numbers to account ids, or tax code names to tax ids, and vice versa.

A first implementation is available in the feat/cache branch and can be reviewed here: CachedCashCtrlClient Implementation

Details:

Usage Example:

from cashctrl_api.cashed_client import CachedCashCtrlClient

# Initialize with a cache timeout of 600 seconds (10 minutes)
client = CachedCashCtrlClient(organisation='my_org', api_key='my_api_key', cache_timeout=600)

# Fetch accounts (data will be cached)
accounts = client.list_accounts()

# Fetch tax codes (data will be cached)
tax_codes = client.list_tax_codes()

# Lookup account ID by account number and vice versa
account_id = client.account_to_id(1000)
account = client.account_from_id(1)
lasuk commented 3 months ago

Let's extend the cache to currencies, since currency lookup is also a frequent trigger for an (avoidable) API request.

Steps:

lasuk commented 2 months ago

Implemented as #29