Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 TmtRequest objects from text and language pair.
  • Execute HTTP POST requests to the configured base_url.
  • Parse TmtResponse and map HTTP status codes to typed TmtError variants.
  • 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 StatusTmtError / AppError
200 OKSuccess
429 Too Many RequestsRateLimit (triggers backoff)
5xxNetwork (triggers retry)
OtherUnexpectedStatus

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.