MadeInPierre / finalynx

A minimalistic companion (CLI & web) to organize your investment portfolio, simulate its future, and reach your life goals.
https://finalynx.readthedocs.io
GNU General Public License v3.0
65 stars 13 forks source link

feat(fetch): auto fetch currency #57

Closed Varal7 closed 1 year ago

Varal7 commented 1 year ago

Description

Automatically queries the user's preferred currency from Finary data.

TODO: Save currency in cache.

Varal7 commented 1 year ago

Yeah, so this PR won't work because one needs to save the currency together with line_list in the cache. Otherwise, the next time it's called again, it will default back to [user-config / EUR].

The problem with updating the cache code is that it's breaking backward-compatibility.

For now, I guess it's fine to just manually set the currency in the config file.

MadeInPierre commented 1 year ago

I think the whole fetching process would merit an upgrade in the code structure. If you have many different currencies (I don't have cryptos yet), I can understand that setting everything manually can get tedious.

I also recently discovered #47, which makes me want to create a FinaryLine class containing various info fetched from finary (e.g. line name, id, amount, display amount, account name, currency code/symbol, ...). We would then refactor finary_fetch.py to match this new structure and add an auto-set method to link FinaryLines with Lines.

This would be a breaking change but I don't see why not!

Varal7 commented 1 year ago

Yes, being able to include additional metadata in FinaryLine would be useful. For instance, institution name would be helpful to build the config (in addition to name).

Another thing would be to allow the user to define its own custom callback to fetch from the finary api. I haven't figured out the canonical structure to extract loans from finary api, but I know a way that works for me. So right now, in my config, I'm using the finary api "directly" like this:

from finalynx import FetchFinary
from finalynx.fetch.fetch_finary import ff

fake_portfolio = Portfolio("FakePortfolio")
fetch_finary = FetchFinary(fake_portfolio)
session = fetch_finary._authenticate()
loans = ff.get_loans(session)
amount = loans['result']['data'][0]['account']['display_balance']

Then, I create the real portfolio with

Line("Loan", amount=-amount),

It works but it's a bit hacky.

It would be great to have the concept of FinaryLine so users can define their own custom lines too.

MadeInPierre commented 1 year ago

save the currency together with line_list in the cache. Otherwise, the next time it's called again, it will default back to [user-config / EUR]. The problem with updating the cache code is that it's breaking backward-compatibility.

Actually, this PR could be a good first step in the right direction while waiting to refactor the code with FinaryLine, I think we can keep backwards compatibility by changing this line to:

lines_list.append({"key": key, "id": id, "amount": amount, "currency": currency})
# Also add currency to the method's arguments

Then, when retrieving the cached contents, we default to DEFAULT_CURRENCY in finalynx/config.py if the currency key does not exist:

currency = line["currency"] if "currency" in line.keys() else DEFAULT_CURRENCY  # import finalynx.config
self._match_line([], tree, key=line["key"], id=line["id"], amount=line["amount"], currency=currency)

What do you think?

MadeInPierre commented 1 year ago

New implementation in #70, hopefully this new method fits your needs, thanks for contributing!