deeplclient

package
v0.0.0-...-13dbc2a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package deeplclient contains the very basic low-level DeepL API client.

Index

Constants

View Source
const (
	// 	LangBG Bulgarian
	LangBG = ApiLang("BG")
	// LangCS Czech
	LangCS = ApiLang("CS")
	// LangDA Danish
	LangDA = ApiLang("DA")
	// LangDE German
	LangDE = ApiLang("DE")
	// LangEL Greek
	LangEL = ApiLang("EL")
	// LangEN English
	LangEN = ApiLang("EN")
	// LangES Spanish
	LangES = ApiLang("ES")
	// LangET Estonian
	LangET = ApiLang("ET")
	// LangFI Finnish
	LangFI = ApiLang("FI")
	// LangFR French
	LangFR = ApiLang("FR")
	// LangHU Hungarian
	LangHU = ApiLang("HU")
	// LangID Indonesian
	LangID = ApiLang("ID")
	// LangIT Italian
	LangIT = ApiLang("IT")
	// LangJA Japanese
	LangJA = ApiLang("JA")
	// LangLT Lithuanian
	LangLT = ApiLang("LT")
	// LangLV Latvian
	LangLV = ApiLang("LV")
	// LangNL Dutch
	LangNL = ApiLang("NL")
	// LangPL Polish
	LangPL = ApiLang("PL")
	// LangPT Portuguese (all Portuguese varieties mixed)
	LangPT = ApiLang("PT")
	// LangRO Romanian
	LangRO = ApiLang("RO")
	// LangRU Russian
	LangRU = ApiLang("RU")
	// LangSK Slovak
	LangSK = ApiLang("SK")
	// LangSL Slovenian
	LangSL = ApiLang("SL")
	// LangSV Swedish
	LangSV = ApiLang("SV")
	// LangTR Turkish
	LangTR = ApiLang("TR")
	// LangUK Ukrainian
	LangUK = ApiLang("UK")
	// LangZH Chinese
	LangZH = ApiLang("ZH")
)
View Source
const (
	// FormalityDefault is the default used.
	FormalityDefault = ApiFormality("default")
	// FormalityMore sets a more formal language.
	FormalityMore = ApiFormality("more")
	// FormalityLess sets a more informal language.
	FormalityLess = ApiFormality("less")
)
View Source
const (
	StatusQueued      = DocumentTranslationStatus("queued")
	StatusTranslating = DocumentTranslationStatus("translating")
	StatusDone        = DocumentTranslationStatus("done")
	StatusError       = DocumentTranslationStatus("error")
)
View Source
const (
	// StatusQuotaExceeded is the unofficial internal HTTP status code for "Quota exceeded"
	StatusQuotaExceeded = 456
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiFormality

type ApiFormality string

ApiFormality is used to set whether the translation should lean towards formal or informal language.

type ApiLang

type ApiLang string

ApiLang is a wrapper type for languages used in requests/responses within the translation function.

func LangFromString

func LangFromString(apiLangString string) (error, ApiLang)

LangFromString tries to find and return the matching wrapped API language type.

func (ApiLang) String

func (apiLang ApiLang) String() string

String returns the very basic string representation of the API language.

type AuthFailedErr

type AuthFailedErr struct {
	*KnownRequestErrData
}

AuthFailedErr indicates the response code 403 returned by the remote API server and contains the error message. Normally this error occurs if an invalid auth token was provided.

func (*AuthFailedErr) Error

func (err *AuthFailedErr) Error() string

Error returns a compact version of all error information in order to implement the error interface.

type Client

type Client struct {
	*http.Client
	// AuthKey stores the authentication key required to get access DeepL's API.
	AuthKey     []byte
	EndpointUrl string
}

Client allows easy access to the DeepL API by providing methods for each API function.

func (*Client) CheckDocumentTranslationStatus

func (client *Client) CheckDocumentTranslationStatus(req *DocumentTranslationStatusRequest) (
	resp *DocumentTranslationStatusResponse, err error)

CheckDocumentTranslationStatus returns the current status of the document translation or an error, if something went wrong

func (*Client) DownloadTranslatedDocument

func (client *Client) DownloadTranslatedDocument(req *DocumentTranslationDownloadRequest) (result []byte, err error)

DownloadTranslatedDocument returns the translated document or an error, if something went wrong

func (*Client) GetUsage

func (client *Client) GetUsage() (resp *UsageResponse, err error)

GetUsage returns the usage information for the current billing period.

func (*Client) StartDocumentTranslate

func (client *Client) StartDocumentTranslate(req *DocumentTranslationStartRequest) (
	resp *DocumentTranslationStartResponse, err error)

StartDocumentTranslate starts the translation process of a given document and returns document information or an error if something went wrong

func (*Client) Translate

func (client *Client) Translate(req *TranslationRequest) (resp *TranslationResponse, err error)

Translate translates the requested text and returns the translated text or an error if something went wrong.

type DocumentTranslationDownloadRequest

type DocumentTranslationDownloadRequest DocumentTranslationStartResponse

DocumentTranslationDownloadRequest and DocumentTranslationStartResponse share the same fields

type DocumentTranslationStartRequest

type DocumentTranslationStartRequest struct {
	// SourceLang is the language of the text to be translated. If parameter is omitted, the API will detect the
	// language of the text and translate it.
	SourceLang ApiLang

	// TargetLang determines the language into which you want to translate.
	TargetLang ApiLang

	// File is the file to be translated (only .docx, .pptx, .pdf, .htm(l) and .txt are allowed)
	File []byte

	// Filename describes the name of the file to be translated
	Filename string

	// Formality Sets whether the translated text should lean towards formal or informal language. This feature
	// currently only works for target languages DE (German), FR (French), IT (Italian), ES (Spanish), NL (Dutch),
	// PL (Polish), PT-PT, PT-BR (Portuguese) and RU (Russian).
	Formality ApiFormality

	// GlossaryId A unique ID assigned to a glossary.
	GlossaryId ApiLang
}

DocumentTranslationStartRequest contains the payload data for each document translation request

type DocumentTranslationStartResponse

type DocumentTranslationStartResponse struct {
	// DocumentId is a unique ID which is being used in subsequent API requests regarding the uploaded document
	DocumentId string `json:"document_id"`

	// DocumentKey is an encryption key which is necessary to decrypt this document on download
	DocumentKey string `json:"document_key"`
}

DocumentTranslationStartResponse represents the data of the json response of the document translation API function

type DocumentTranslationStatus

type DocumentTranslationStatus string

DocumentTranslationStatus is a wrapper type for various states that can occur during translation

type DocumentTranslationStatusRequest

type DocumentTranslationStatusRequest DocumentTranslationStartResponse

DocumentTranslationStatusRequest and DocumentTranslationStartResponse share the same fields

type DocumentTranslationStatusResponse

type DocumentTranslationStatusResponse struct {
	// DocumentID is the unique ID of the document
	DocumentId string

	// Status defines the current state of the translation process
	Status DocumentTranslationStatus

	// SecondsRemaining describes the estimated time until the translation is done
	SecondsRemaining uint

	// BilledCharacters is the amount of characters billed
	BilledCharacters uint

	// ErrorMessage describes an error during translation, if one occurred (if not the value is nil)
	ErrorMessage *string
}

DocumentTranslationStatusResponse represents the data of the json response of the document translation status API function

type KnownRequestErrData

type KnownRequestErrData struct {
	// Message holds the error message returned by the server.
	Message string `json:"message"`
}

KnownRequestErrData contains the standard data which can be parsed from json for every error.

type NotFoundErr

type NotFoundErr struct {
	*KnownRequestErrData
}

NotFoundErr indicates the response code 404 returned by the remote API server and contains the error message. Normally this error occurs if an undefined API endpoint of an undefined document status/result is requested.

func (*NotFoundErr) Error

func (err *NotFoundErr) Error() string

Error returns a compact version of all error information in order to implement the error interface.

type QuotaExceededErr

type QuotaExceededErr struct {
	*KnownRequestErrData
}

QuotaExceededErr indicates the response code 403 returned by the remote API server and contains the error message. Normally this error occurs if the character limit has been reached.

func (*QuotaExceededErr) Error

func (err *QuotaExceededErr) Error() string

Error returns a compact version of all error information in order to implement the error interface.

type RequestEntityTooLargeErr

type RequestEntityTooLargeErr struct {
	*KnownRequestErrData
}

RequestEntityTooLargeErr indicates the response code 413 returned by the remote API server and contains the error message. Normally this error occurs if the request size exceeds the current limit.

func (*RequestEntityTooLargeErr) Error

func (err *RequestEntityTooLargeErr) Error() string

Error returns a compact version of all error information in order to implement the error interface.

type TooManyRequestsErr

type TooManyRequestsErr struct {
	*KnownRequestErrData
}

TooManyRequestsErr indicates the response code 429 returned by the remote API server and contains the error message. Normally this error occurs if too many requests have been sent in a short amount of time.

func (*TooManyRequestsErr) Error

func (err *TooManyRequestsErr) Error() string

Error returns a compact version of all error information in order to implement the error interface.

type TranslationRequest

type TranslationRequest struct {

	// Text to be translated. Only UTF8-encoded plain text is supported. The parameter may be specified multiple times
	// and translations are returned in the same order as in the input. Each of the parameter values may contain
	// multiple sentences.
	Text string
	// SourceLang is the language of the text to be translated. If parameter is omitted, the API will detect the
	// language of the text and translate it.
	SourceLang ApiLang
	// TargetLang determines the language into which you want to translate.
	TargetLang ApiLang
	// TagHandling sets which kind of tags should be handled. Comma-separated list of one or more values. See official
	// DeepL API documentation for more details about tag handling.
	TagHandling []string
	// NonSplittingTags contains a list of XML tags which never split sentences. See official DeepL API documentation
	// for more details about tag handling.
	NonSplittingTags []string
	// IgnoreTags contains a list of XML tags whose content is never translated. See official DeepL API documentation
	// for more details about tag handling.
	IgnoreTags []string
	// DoNotSplitSentences sets whether the translation engine should first split the input into sentences. False by
	// default.
	//
	// For applications which are sending one sentence per text parameter, it is advisable to set this to false, in
	// order to prevent the engine from splitting the sentence unintentionally.
	DoNotSplitSentences bool
	// Sets whether the translation engine should preserve some aspects of the formatting, even if it would usually
	// correct some aspects. False by default.
	//
	// The formatting aspects controlled by the setting include:
	// 	punctuation at the beginning and end of the sentence,
	// 	upper/lower case at the beginning of the sentence.
	PreserveFormatting bool
	// Formality Sets whether the translated text should lean towards formal or informal language. This feature
	// currently only works for target languages DE (German), FR (French), IT (Italian), ES (Spanish), NL (Dutch),
	// PL (Polish), PT-PT, PT-BR (Portuguese) and RU (Russian).
	Formality ApiFormality
	// GlossaryId Specify the glossary to use for the translation. Important: This requires the source_lang parameter to
	// be set and the language pair of the glossary has to match the language pair of the request.
	GlossaryId ApiLang
	// Context is an optional field that can be used to provide additional context to the DeepL engine.
	// This Context parameter is an alpha feature.
	Context []string
}

TranslationRequest contains the payload data for each translation request.

type TranslationResponse

type TranslationResponse struct {
	// Translations contains all requested translations and their results.
	Translations []struct {
		// DetectedSourceLanguage contains the ApiLang detected by the DeepL API.
		DetectedSourceLanguage ApiLang `json:"detected_source_language"`
		// Text contains the translated text.
		Text string `json:"text"`
	} `json:"translations"`
}

TranslationResponse represents the data of the json response of the translation API function.

type UnwrappedApiResponseCodeErr

type UnwrappedApiResponseCodeErr int

UnwrappedApiResponseCodeErr represents an error if the API server returns an unexpected status code.

func (UnwrappedApiResponseCodeErr) Error

func (err UnwrappedApiResponseCodeErr) Error() string

Error returns a compact version of all error information in order to implement the error interface.

type UsageResponse

type UsageResponse struct {
	// CharacterCount contains the amount of characters translated so far in the current billing period.
	CharacterCount int64 `json:"character_count"`
	// CharacterLimit contains the maximum volume of characters that can be translated in the current billing period.
	CharacterLimit int64 `json:"character_limit"`
}

UsageResponse represents the data of the json response of the usage API function.

type WrongRequestErr

type WrongRequestErr struct {
	*KnownRequestErrData
}

WrongRequestErr indicates the response code 400 returned by the remote API server and contains the error message.

func (*WrongRequestErr) Error

func (err *WrongRequestErr) Error() string

Error returns a compact version of all error information in order to implement the error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL