Skip to content

Latest commit

 

History

History
104 lines (87 loc) · 6 KB

File metadata and controls

104 lines (87 loc) · 6 KB

Local unit-conversion HTTP API

The loopback numforge_web server exposes the same sourced unit registry and conversion core as the C API. The SK/EN browser page uses these routes. They are intended for the local application, with the existing request framing, origin checks and calculator resource limits.

The conversion core belongs to the standalone numeric library. The HTTP adapter borrows session values from the private application layer; it does not own client pools or persistence. See Application design.

Catalogue

GET /api/units returns { "ok": true, "units": [...] }. Each entry has id, symbol, quantity, name_en, name_sk, source_url and pi_power. Use the case-sensitive ASCII id in requests; localized names and Unicode symbols are display labels. Order and array indexes are not identifiers.

Quantity values are length, area, volume, mass, time, speed, temperature, temperature_interval, information and angle. The client can filter matching quantities; the server independently checks compatibility. Temperature points and temperature intervals are distinct.

Conversion

Send POST /api/convert?from=km&to=m with a plain UTF-8 expression body, for example 1/3. Supply Content-Length as for /api/evaluate. Units are separate query parameters; 2 km is not calculator syntax. Quantity-valued expressions are rejected with quantity_not_allowed. Use convert(qty(1; "km"); "km"; "m") for an explicit numeric coordinate, or use /api/evaluate for Quantity arithmetic. Percent-encode IDs containing /, for example from=km%2Fh.

{"ok":true,"result":"1000/3","unit":"m","symbol":"m","approx":"333.3333333333","input_approximate":false,"factor_approximate":false}

Successful responses may include approx, a secondary decimal display hint for fractions up to 16 characters, matching the calculator's hint policy (10 decimal places, half-even rounding, scientific notation for tiny nonzero values). It uses the same output unit and does not replace the exact result, change the copy value or describe an error bound. The field is absent for non-fraction results or when the optional hint cannot be generated. Conversion confirmation and history entries also include the optional approx hint, so restoring a saved fraction retains its secondary display.

Parameter Values / default
from, to Required catalogue IDs.
precision Working significant digits, 1–10000; default 34.
places Display decimal places, 0–10000, or full; default 10.
rounding toward_zero, away_from_zero, floor, ceiling, half_up, half_even (default).
notation auto (default), plain, scientific, math, fraction.
angle rad (default) or deg, for functions in the input expression.
client Optional existing calculator session ID: 32 lowercase hexadecimal characters.
snapshot Optional 1: include the authoritative value for a history record.

Unknown, duplicate, empty or malformed query parameters are rejected. The precision parameter here controls working precision; /api/evaluate has its existing precision contract. Display settings do not increase working accuracy. angle does not select conversion units: from and to always do that.

Integer and rational expression values reach exact-factor conversion directly, without a decimal display round trip. Pi-based angle conversions use the guarded approximate path. input_approximate reports a decimal approximation produced by the evaluator; factor_approximate reports a conversion requiring pi. Neither flag certifies exact displayed text or correctly rounded arbitrary input: display rounding may occur even when both are false. fraction preserves exact rational results; approximate inputs remain approximate decimal results.

Sessions and errors

Without client, expressions are stateless. With an existing client, the expression may read stored variables and confirmed ans. Conversion never changes variables, ans, history, the random generator, pending preview or session revision, including on failure. An expired/missing session is not created or reset. Assignments and rand() are rejected. There is no action or revision parameter on this read-only endpoint. Confirmed conversion history is handled by the separate session/conversion API, independently of calculator ans/history and with its own revision ordering.

With snapshot=1, success additionally includes "value":{"kind":"rational","text":"1000/3","unit":"m","precision":34}. text is the reduced rational encoding of the authoritative converted value, independent of display notation/rounding. For decimal approximations, kind is decimal_approximation: the encoding retains the finite computed approximation and its requested working precision. It does not imply exact mathematics. Serialization shares the request budget and is limited to 65536 numeric bytes and the bounded JSON response. Oversized snapshots fail without session mutation. Omitting snapshot keeps the original lightweight preview contract.

Conversion failures normally return HTTP 400 with ok: false, code, error, calculator status and one-based column. Codes include invalid_options, unknown_unit, incompatible_units, assignment_not_allowed, random_not_allowed, session_expired, invalid_body, expression_error, time_limit and value_too_large. Allocation failure returns HTTP 500 with out_of_memory. Unit/query errors use column 1; expression errors retain their input position.

Existing HTTP framing errors keep their existing response schema: for example missing Content-Length is 411, foreign Origin is 403 and a body exceeding 4096 bytes is 413. Embedded NUL bytes are rejected. Evaluation, conversion and result formatting share the calculator's cooperative time/allocation budget. This is not a public hosted-service isolation model; see the roadmap.