Skip to content

Commit 289a3ca

Browse files
committed
Add currencies
1 parent 435c6d7 commit 289a3ca

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

src/pyfirefly/models.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,27 @@ class PreferencesAttributes(DataClassORJSONMixin):
327327
user_group_id: int | None = None
328328
name: str | None = None
329329
data: str | bool | None = None
330+
331+
332+
@dataclass
333+
class Currency(DataClassORJSONMixin):
334+
"""Model for a Firefly currency."""
335+
336+
type: str
337+
id: str
338+
attributes: CurrencyAttributes
339+
340+
341+
@dataclass
342+
class CurrencyAttributes(DataClassORJSONMixin):
343+
"""Attributes of a Firefly currency."""
344+
345+
created_at: str | None = None
346+
updated_at: str | None = None
347+
enabled: bool | None = None
348+
default: bool | None = None
349+
native: bool | None = None
350+
code: str | None = None
351+
name: str | None = None
352+
symbol: str | None = None
353+
decimal_places: int | None = None

src/pyfirefly/pyfirefly.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Bill,
2727
Budget,
2828
Category,
29+
Currency,
2930
Preferences,
3031
Transaction,
3132
)
@@ -348,6 +349,28 @@ async def get_preferences(self) -> list[Preferences]:
348349
preferences = await self._request("preferences")
349350
return [Preferences.from_dict(pref) for pref in preferences["data"]]
350351

352+
async def get_currencies(self) -> list[Currency]:
353+
"""Get currencies from the Firefly server.
354+
355+
Returns
356+
-------
357+
A list of Currency objects containing the currencies.
358+
359+
"""
360+
currencies = await self._request("currencies")
361+
return [Currency.from_dict(cur) for cur in currencies["data"]]
362+
363+
async def get_currency_native(self) -> Currency:
364+
"""Get the native currency of the current administration.
365+
366+
Returns
367+
-------
368+
A Currency object containing the native currency symbol.
369+
370+
"""
371+
currency = await self._request("currencies/native")
372+
return Currency.from_dict(currency["data"])
373+
351374
async def close(self) -> None:
352375
"""Close open client session."""
353376
if self._session and self._close_session:

tests/__snapshots__/test_models.ambr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
# name: test_category_model.1
5151
Category(type='categories', id='2', attributes=CategoryAttributes(created_at='2018-09-17T12:46:47+01:00', updated_at='2018-09-17T12:46:47+01:00', name='Lunch', notes='Some example notes', native_currency_id='5', native_currency_code='EUR', native_currency_symbol='$', native_currency_decimal_places=2, spent=[CategoryAmount(currency_id='5', currency_code='USD', currency_symbol='$', currency_decimal_places=2, sum='-12423.45')], earned=[CategoryAmount(currency_id='5', currency_code='USD', currency_symbol='$', currency_decimal_places=2, sum='123.45')]))
5252
# ---
53+
# name: test_currencies_model
54+
list([
55+
Currency(type='currencies', id='2', attributes=CurrencyAttributes(created_at='2018-09-17T12:46:47+01:00', updated_at='2018-09-17T12:46:47+01:00', enabled=True, default=False, native=False, code='AMS', name='Ankh-Morpork dollar', symbol='AM$', decimal_places=2)),
56+
])
57+
# ---
58+
# name: test_currency_native_model
59+
Currency(type='currencies', id='2', attributes=CurrencyAttributes(created_at='2018-09-17T12:46:47+01:00', updated_at='2018-09-17T12:46:47+01:00', enabled=True, default=False, native=False, code='AMS', name='Ankh-Morpork dollar', symbol='AM$', decimal_places=2))
60+
# ---
5361
# name: test_preferences_model
5462
list([
5563
Preferences(type='preferences', id=2),

tests/fixtures/currencies.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"data": [
3+
{
4+
"type": "currencies",
5+
"id": "2",
6+
"attributes": {
7+
"created_at": "2018-09-17T12:46:47+01:00",
8+
"updated_at": "2018-09-17T12:46:47+01:00",
9+
"enabled": true,
10+
"default": false,
11+
"native": false,
12+
"code": "AMS",
13+
"name": "Ankh-Morpork dollar",
14+
"symbol": "AM$",
15+
"decimal_places": 2
16+
}
17+
}
18+
],
19+
"meta": {
20+
"pagination": {
21+
"total": 3,
22+
"count": 20,
23+
"per_page": 100,
24+
"current_page": 1,
25+
"total_pages": 1
26+
}
27+
},
28+
"links": {
29+
"self": "https://demo.firefly-iii.org/api/v1/OBJECT?&page=4",
30+
"first": "https://demo.firefly-iii.org/api/v1/OBJECT?&page=1",
31+
"next": "https://demo.firefly-iii.org/api/v1/OBJECT?&page=3",
32+
"prev": "https://demo.firefly-iii.org/api/v1/OBJECT?&page=2",
33+
"last": "https://demo.firefly-iii.org/api/v1/OBJECT?&page=12"
34+
}
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"data": {
3+
"type": "currencies",
4+
"id": "2",
5+
"attributes": {
6+
"created_at": "2018-09-17T12:46:47+01:00",
7+
"updated_at": "2018-09-17T12:46:47+01:00",
8+
"enabled": true,
9+
"default": false,
10+
"native": false,
11+
"code": "AMS",
12+
"name": "Ankh-Morpork dollar",
13+
"symbol": "AM$",
14+
"decimal_places": 2
15+
}
16+
}
17+
}

tests/test_models.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,45 @@ async def test_preferences_model(
240240

241241
preferences = await firefly_client.get_preferences()
242242
assert preferences == snapshot
243+
244+
245+
async def test_currencies_model(
246+
aresponses: ResponsesMockServer,
247+
firefly_client: Firefly,
248+
snapshot: SnapshotAssertion,
249+
) -> None:
250+
"""Test the Currencies model."""
251+
aresponses.add(
252+
"localhost:9000",
253+
"/api/v1/currencies",
254+
"GET",
255+
aresponses.Response(
256+
status=200,
257+
headers={"Content-Type": "application/vnd.api+json"},
258+
text=load_fixtures("currencies.json"),
259+
),
260+
)
261+
262+
currencies = await firefly_client.get_currencies()
263+
assert currencies == snapshot
264+
265+
266+
async def test_currency_native_model(
267+
aresponses: ResponsesMockServer,
268+
firefly_client: Firefly,
269+
snapshot: SnapshotAssertion,
270+
) -> None:
271+
"""Test the Currency Native model."""
272+
aresponses.add(
273+
"localhost:9000",
274+
"/api/v1/currencies/native",
275+
"GET",
276+
aresponses.Response(
277+
status=200,
278+
headers={"Content-Type": "application/vnd.api+json"},
279+
text=load_fixtures("currency_native.json"),
280+
),
281+
)
282+
283+
currency_native = await firefly_client.get_currency_native()
284+
assert currency_native == snapshot

0 commit comments

Comments
 (0)