Skip to content

fix(price): point ratesapi at Frankfurter, as beanprice does - #2864

Merged
robcohen merged 4 commits into
mainfrom
fix/ratesapi-use-frankfurter
Nov 5, 2026
Merged

robcohen merged 4 commits into
mainfrom
fix/ratesapi-use-frankfurter

Conversation

@robcohen

@robcohen robcohen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

What changed

rledger price --source ratesapi now fetches from Frankfurter (https://api.frankfurter.dev/v1), the open-source keyless API that serves the ECB reference rates, and the provider bean-price's ratesapi source uses. This is not full bean-price parity: rledger's ratesapi ticker is the base currency (EUR with -c USD), not bean-price's ratesapi/EUR-USD form, and ^ inversion is not supported (to be tracked separately). The source name, ticker format, historical coverage (from 1999-01-04), feed-date handling and weekend clamp are unchanged.

Why

The source called api.exchangerate.host, which now answers every request without an API key with HTTP 401 missing_access_key. Every pair failed, while the source still said it needs no key.

Also

  • Response parsing is split out of fetch_rate into parse_response, so it is tested without the network.
  • An HTTP error now carries Frankfurter's own message: the request reads the body on any status (ureq 3 otherwise turns a 4xx into a bare StatusCode error), so a user sees HTTP 404 for USD-XXX: not found or HTTP 422 for EUR-EUR: bad currency pair. A body with no message falls back to "the currency pair may be unsupported".
  • The configured price-fetch timeout now applies to ratesapi requests (RequestBuilder::config().timeout_global). The note in sources/mod.rs saying ureq 3 has no per-request timeout is corrected. The other sources still ignore their timeout: there is no shared request helper, so that is a per-source change for a follow-up.
  • An error body names the provider's message: Frankfurter's message, or a keyed service's error.info.
  • Docs (docs/commands/price.md) and the CHANGELOG are updated.

No other provider and no fallback chain are added.

How to test

  • cargo test -p rustledger --lib ratesapi: 10 tests, including canned Frankfurter bodies for latest, historical and a weekend request, error bodies (not found, missing_access_key), a non-JSON body, a missing pair, and HTTP 404/422/502 responses through the same handle_response step the live request uses.

  • Live, checked against the ECB (data-api.ecb.europa.eu, EXR D.USD.EUR):

    Command Result ECB
    rledger price -s ratesapi EUR EUR: 1.1267 USD 1.1267 (2026-11-04)
    rledger price -s ratesapi -d 2026-10-30 EUR EUR: 1.1172 USD 1.1172
    rledger price -s ratesapi -d 2026-11-01 EUR (a Sunday) EUR: 1.1172 USD (Friday's rate)
    rledger price -s ratesapi XXX HTTP 404 for XXX-USD: not found

cargo test -p rustledger --all-features (47 result lines, 0 failed), clippy -D warnings, rustdoc -Dwarnings and cargo fmt --check pass locally.

🤖 Generated with Claude Code

api.exchangerate.host now answers every request without an API key with
`missing_access_key`, so `rledger price --source ratesapi` failed for every
pair while the source still said it needs no key. It now asks Frankfurter
(api.frankfurter.dev/v1), the open-source keyless API serving the same ECB
reference rates, which is what beanprice's ratesapi source uses. Response
parsing is split out and covered by network-free tests (latest, historical,
a weekend request, error bodies, a non-JSON body); an HTTP error names the
pair and says it may be unsupported.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 5, 2026 05:17
@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.78882% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...rates/rustledger/src/cmd/price/sources/ratesapi.rs 93.78% 10 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 31.8ms ± 0.6ms 492.0ms ± 14.0ms 15.5x 34.6 MB / 51.1 MB
investment 52.1ms ± 1.8ms 1792.0ms ± 8.9ms 34.4x 44.7 MB / 59.1 MB
fifo 50.9ms ± 1.0ms 8843.9ms ± 127.6ms 173.7x 40.6 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 32.5ms ± 0.9ms 31.8ms ± 0.6ms -2.1% ✅ within noise 34.6 MB → 34.6 MB (+0.0%)
investment 54.0ms ± 6.4ms 52.1ms ± 1.8ms -3.5% ✅ within noise 44.7 MB → 44.7 MB (+0.2%)
fifo 52.2ms ± 0.8ms 50.9ms ± 1.0ms -2.5% ✅ within noise 40.8 MB → 40.6 MB (-0.5%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.5ms, investment 3.0ms, fifo 0.6ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 826
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

… ratesapi

ureq 3 turns a 4xx/5xx into a bare StatusCode error, so Frankfurter's
`{"message": ...}` (404 not found, 422 bad currency pair / invalid date)
never reached the user. The request now reads the body on any status and
one `handle_response` step reports `HTTP 404 for USD-XXX: not found`;
tests feed it canned 404/422/502 bodies. The configured timeout is now
applied per request (`config().timeout_global`); the note in sources/mod.rs
that ureq 3 has no per-request timeout is corrected (the other sources do
not apply theirs yet). Docs and CHANGELOG no longer claim full bean-price
parity: rledger's ratesapi ticker is the base currency, not bean-price's
`BASE-SYMBOL` form, and `^` inversion is not supported. Also fixes a stale
exchangerate.host comment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The fallback hint said the pair may be unsupported for any status; a 5xx
or gateway page now says the service may be unavailable. Rewraps the
struct doc and drops the 422 example, which a same-currency pair never
reaches (the identity arm answers it).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen enabled auto-merge (squash) November 5, 2026 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant