TMT API Client
Overview
TmtClient (defined in src/tmt/mod.rs) is the low-level HTTP client responsible for communicating with the TMT translation API. It is initialised once and shared (via Arc) across all concurrent translation tasks.
Responsibilities
- Construct
TmtRequestobjects from text and language pair. - Execute HTTP POST requests to the configured
base_url. - Parse
TmtResponseand map HTTP status codes to typedTmtErrorvariants. - Enforce a minimum inter-request delay via a
Mutex-protected timestamp.
Request Format
Each API call sends a JSON body of roughly this shape:
{
"text": "Hello world.",
"src_lang": "en",
"tgt_lang": "ne"
}
The text field is limited to MAX_REQUEST_TEXT_BYTES (5,000 characters). Longer inputs must be split upstream by TranslationService.
Response Handling
A successful response returns the translated text. Failure responses are mapped as follows:
| HTTP Status | TmtError / AppError |
|---|---|
200 OK | Success |
429 Too Many Requests | RateLimit (triggers backoff) |
5xx | Network (triggers retry) |
| Other | UnexpectedStatus |
Rate-Limiting Gate
TmtClient holds a Mutex<Instant> recording the time of the last request. When --rate-limit-ms is configured, each call acquires the mutex, sleeps until the required interval has elapsed since the last request, fires the HTTP call, and then updates the stored instant before releasing the mutex. This serialises the delay calculation even when multiple tokio tasks are running concurrently.