Files
sdk/api_translate.go

415 lines
18 KiB
Go

/*
Hanzo Cloud API
The Hanzo Cloud API as a customer calls it: every operation under /v1/ except the operator's admin product, relay routes, legacy spellings and capabilities still reached by flag. Tagged by product: the first path segment after /v1/.
API version: v1
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package hanzoai
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
)
// TranslateAPIService TranslateAPI service
type TranslateAPIService service
type TranslateAPIGetTranslateMemoryRequest struct {
ctx context.Context
ApiService *TranslateAPIService
target *string
state *string
limit *int64
}
// Target narrows to one target language tag (BCP-47, e.g. \"es\" or \"pt-BR\").
func (r TranslateAPIGetTranslateMemoryRequest) Target(target string) TranslateAPIGetTranslateMemoryRequest {
r.target = &target
return r
}
// State narrows to one position on the review ladder: machine, suggested, approved or published.
func (r TranslateAPIGetTranslateMemoryRequest) State(state string) TranslateAPIGetTranslateMemoryRequest {
r.state = &state
return r
}
// Limit caps the rows returned. Non-positive or unparseable means the server default (200); the ceiling is 1000.
func (r TranslateAPIGetTranslateMemoryRequest) Limit(limit int64) TranslateAPIGetTranslateMemoryRequest {
r.limit = &limit
return r
}
func (r TranslateAPIGetTranslateMemoryRequest) Execute() (*TranslateMemoryPage, *http.Response, error) {
return r.ApiService.GetTranslateMemoryExecute(r)
}
/*
GetTranslateMemory Returns the org's own translation-memory entries, newest first, optionally narrowed to one target language and/or one position on the review ladder.
Returns the org's own translation-memory entries, newest first, optionally
narrowed to one target language and/or one position on the review ladder. It is
the review lane's read: what a human reviewer works through.
The org is ALWAYS the validated principal's org, never a request field, so one
tenant can never read another's memory — the entries hold customer source text.
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return TranslateAPIGetTranslateMemoryRequest
*/
func (a *TranslateAPIService) GetTranslateMemory(ctx context.Context) TranslateAPIGetTranslateMemoryRequest {
return TranslateAPIGetTranslateMemoryRequest{
ApiService: a,
ctx: ctx,
}
}
// Execute executes the request
//
// @return TranslateMemoryPage
func (a *TranslateAPIService) GetTranslateMemoryExecute(r TranslateAPIGetTranslateMemoryRequest) (*TranslateMemoryPage, *http.Response, error) {
var (
localVarHTTPMethod = http.MethodGet
localVarPostBody interface{}
formFiles []formFile
localVarReturnValue *TranslateMemoryPage
)
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "TranslateAPIService.GetTranslateMemory")
if err != nil {
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
}
localVarPath := localBasePath + "/v1/translate/memory"
localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}
if r.target != nil {
parameterAddToHeaderOrQuery(localVarQueryParams, "target", r.target, "form", "")
}
if r.state != nil {
parameterAddToHeaderOrQuery(localVarQueryParams, "state", r.state, "form", "")
}
if r.limit != nil {
parameterAddToHeaderOrQuery(localVarQueryParams, "limit", r.limit, "form", "")
}
// to determine the Content-Type header
localVarHTTPContentTypes := []string{}
// set Content-Type header
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}
// to determine the Accept header
localVarHTTPHeaderAccepts := []string{"application/json", "application/problem+json"}
// set Accept header
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
if err != nil {
return localVarReturnValue, nil, err
}
localVarHTTPResponse, err := a.client.callAPI(req)
if err != nil || localVarHTTPResponse == nil {
return localVarReturnValue, localVarHTTPResponse, err
}
localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
localVarHTTPResponse.Body.Close()
localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
if err != nil {
return localVarReturnValue, localVarHTTPResponse, err
}
if localVarHTTPResponse.StatusCode >= 300 {
newErr := &GenericOpenAPIError{
body: localVarBody,
error: localVarHTTPResponse.Status,
}
var v ProblemDetails
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHTTPResponse, newErr
}
newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
newErr.model = v
return localVarReturnValue, localVarHTTPResponse, newErr
}
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr := &GenericOpenAPIError{
body: localVarBody,
error: err.Error(),
}
return localVarReturnValue, localVarHTTPResponse, newErr
}
return localVarReturnValue, localVarHTTPResponse, nil
}
type TranslateAPIPostTranslateRequest struct {
ctx context.Context
ApiService *TranslateAPIService
brief *Brief
}
func (r TranslateAPIPostTranslateRequest) Brief(brief Brief) TranslateAPIPostTranslateRequest {
r.brief = &brief
return r
}
func (r TranslateAPIPostTranslateRequest) Execute() (*Delivery, *http.Response, error) {
return r.ApiService.PostTranslateExecute(r)
}
/*
PostTranslate Translate a string or a batch into one target language
Returns one translation per input string, in input order, each carrying where it sits on the review ladder and whether it came from your memory rather than an engine — plus a usage block of REAL counts (strings, cached, translated, failed, and the source characters of the strings an engine translated). Send `text` for one string or `batch` for many, never both. When you name no `source`, the detected one is reported back.
THE TRANSLATION MEMORY IS CONSULTED FIRST AND IT IS NORMATIVE, NOT A CACHE. Every string keys on (source text, declared source language, target, glossary version, tier, format, model); a hit is returned VERBATIM and never re-translated, which is what makes a locale rebuild idempotent under a non-deterministic model and the bill proportional to what actually changed. The same text through another model or another format is another entry. A string translated with its source detected also answers a later call that declares one, so declaring `source` never throws away work done without it; one translated under a declared source answers only that source. Misses go to the engine and are written back at state `machine`. Editing a glossary term changes the key, so a stale rendering can never be served.
IT CANNOT TRAMPLE REVIEWED WORK. A write from this route may create an entry or refresh one still at `machine`, and nothing else — a string a human moved to approved or published through the memory review lane survives every rebuild, and comes back here unchanged. The memory is the caller's OWN org's, a separate store per org: the source text you send is customer content and lands nowhere else. Read it back or review it at /v1/translate/memory.
`tier` picks the engine and defaults to quality — the model plane, which carries context, terminology and tone, and which bills its own tokens, so nothing is charged twice here. `bulk` is the high-volume engine and is metered HERE, on the source characters of the strings it translated: a fully-cached rebuild, or a string that failed, costs zero. BULK NEVER FALLS BACK TO QUALITY — on a deployment that does not serve it the answer is 503 for that tier, so a caller is never quietly served, or charged, at a tier it did not ask for. A bulk request beyond its balance is refused with the nested {"error":{"code","message"}} body at 402/503.
`model` names the model the quality tier runs on, through the same metered model plane as the default (enso-auto), which decides whether your org may use it exactly as it does for chat. That is how an org points translation at a model it serves itself, on its own linked GPU say: by the alias it registered for it. A name from an upstream model family is rewritten to the default before it runs, so a model of your own must be named by your org's alias; the reply's `model` names the one it runs on. `model` with `tier: bulk` is 400.
`target` IS CHECKED FOR SHAPE, NOT FOR SUPPORT: anything BCP-47-shaped is accepted (`es`, `pt-BR`), anything else is 400. There is no unsupported-language error — a well-formed tag no engine can actually render is passed straight through, and whatever comes back is what gets stored and returned. `format` (text, html, markdown) tells the engine what markup to preserve; `glossary` fixes terms verbatim.
A BATCH IS TRANSLATED IN PARTS of about 8 KB, each its own completion with a ceiling sized to it. A part that comes back cut short (any finish other than stop), malformed, or covering the wrong number of strings is asked again one string at a time. Every translation must keep its source's shape: the same {placeholders}, printf conversions in the same order, the same ICU arguments and selectors with an other branch (the words in their branches are translated), the same tags with the same attributes (alt, title, aria-label and placeholder are words), the same link targets and URLs; a string whose shape moved is asked once more. A string that still fails comes back with `error` naming what went wrong (a lost placeholder, an added URL) and no text, is not stored, and fails nothing else.
THE WORK IS BOUNDED: at most two model calls per part, one held for every part still to come, a stop after two parts in a row return nothing, 90 seconds per call, and a stop when the caller disconnects. Strings the call did not reach come back with `error` saying why. What came back is stored, so sending the batch again asks only for the rest. When no string at all can be returned, or the engine fails, the answer is 502 with code `translate_failed`.
Requires a validated principal — 401 without one, and the org is always that principal's. At most 512 strings per call and 32768 characters per string (400 past either), and 262144 characters in all (413 past it: split the batch).
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return TranslateAPIPostTranslateRequest
*/
func (a *TranslateAPIService) PostTranslate(ctx context.Context) TranslateAPIPostTranslateRequest {
return TranslateAPIPostTranslateRequest{
ApiService: a,
ctx: ctx,
}
}
// Execute executes the request
//
// @return Delivery
func (a *TranslateAPIService) PostTranslateExecute(r TranslateAPIPostTranslateRequest) (*Delivery, *http.Response, error) {
var (
localVarHTTPMethod = http.MethodPost
localVarPostBody interface{}
formFiles []formFile
localVarReturnValue *Delivery
)
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "TranslateAPIService.PostTranslate")
if err != nil {
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
}
localVarPath := localBasePath + "/v1/translate"
localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}
// to determine the Content-Type header
localVarHTTPContentTypes := []string{"application/json"}
// set Content-Type header
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}
// to determine the Accept header
localVarHTTPHeaderAccepts := []string{"application/json"}
// set Accept header
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = r.brief
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
if err != nil {
return localVarReturnValue, nil, err
}
localVarHTTPResponse, err := a.client.callAPI(req)
if err != nil || localVarHTTPResponse == nil {
return localVarReturnValue, localVarHTTPResponse, err
}
localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
localVarHTTPResponse.Body.Close()
localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
if err != nil {
return localVarReturnValue, localVarHTTPResponse, err
}
if localVarHTTPResponse.StatusCode >= 300 {
newErr := &GenericOpenAPIError{
body: localVarBody,
error: localVarHTTPResponse.Status,
}
return localVarReturnValue, localVarHTTPResponse, newErr
}
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr := &GenericOpenAPIError{
body: localVarBody,
error: err.Error(),
}
return localVarReturnValue, localVarHTTPResponse, newErr
}
return localVarReturnValue, localVarHTTPResponse, nil
}
type TranslateAPIPutTranslateMemoryRequest struct {
ctx context.Context
ApiService *TranslateAPIService
translateReviewRequest *TranslateReviewRequest
}
func (r TranslateAPIPutTranslateMemoryRequest) TranslateReviewRequest(translateReviewRequest TranslateReviewRequest) TranslateAPIPutTranslateMemoryRequest {
r.translateReviewRequest = &translateReviewRequest
return r
}
func (r TranslateAPIPutTranslateMemoryRequest) Execute() (*TranslateMemoryEntry, *http.Response, error) {
return r.ApiService.PutTranslateMemoryExecute(r)
}
/*
PutTranslateMemory Records a human decision on one translation-memory entry, and returns the entry as stored.
Records a human decision on one translation-memory entry, and returns the
entry as stored. A human write always wins over the stored value, and once it lands
at approved or published no machine write can move it again — which is what makes a
locale rebuild safe to run against reviewed work.
The org is ALWAYS the validated principal's org, never a request field, so a review
can only ever land in the caller's own memory.
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return TranslateAPIPutTranslateMemoryRequest
*/
func (a *TranslateAPIService) PutTranslateMemory(ctx context.Context) TranslateAPIPutTranslateMemoryRequest {
return TranslateAPIPutTranslateMemoryRequest{
ApiService: a,
ctx: ctx,
}
}
// Execute executes the request
//
// @return TranslateMemoryEntry
func (a *TranslateAPIService) PutTranslateMemoryExecute(r TranslateAPIPutTranslateMemoryRequest) (*TranslateMemoryEntry, *http.Response, error) {
var (
localVarHTTPMethod = http.MethodPut
localVarPostBody interface{}
formFiles []formFile
localVarReturnValue *TranslateMemoryEntry
)
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "TranslateAPIService.PutTranslateMemory")
if err != nil {
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
}
localVarPath := localBasePath + "/v1/translate/memory"
localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}
if r.translateReviewRequest == nil {
return localVarReturnValue, nil, reportError("translateReviewRequest is required and must be specified")
}
// to determine the Content-Type header
localVarHTTPContentTypes := []string{"application/json"}
// set Content-Type header
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}
// to determine the Accept header
localVarHTTPHeaderAccepts := []string{"application/json", "application/problem+json"}
// set Accept header
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = r.translateReviewRequest
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
if err != nil {
return localVarReturnValue, nil, err
}
localVarHTTPResponse, err := a.client.callAPI(req)
if err != nil || localVarHTTPResponse == nil {
return localVarReturnValue, localVarHTTPResponse, err
}
localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
localVarHTTPResponse.Body.Close()
localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
if err != nil {
return localVarReturnValue, localVarHTTPResponse, err
}
if localVarHTTPResponse.StatusCode >= 300 {
newErr := &GenericOpenAPIError{
body: localVarBody,
error: localVarHTTPResponse.Status,
}
var v ProblemDetails
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHTTPResponse, newErr
}
newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
newErr.model = v
return localVarReturnValue, localVarHTTPResponse, newErr
}
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr := &GenericOpenAPIError{
body: localVarBody,
error: err.Error(),
}
return localVarReturnValue, localVarHTTPResponse, newErr
}
return localVarReturnValue, localVarHTTPResponse, nil
}