deepl

package
v0.0.0-...-7296226 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// NameEnvKeyAPIDefault is default environment key name of DeepL API key to search.
	NameEnvKeyAPIDefault = "DEEPL_API_KEY"
	// StatusQuotaExceeded is the status code of DeepL API when the character limit
	// for translation has been reached.
	StatusQuotaExceeded = 456
	// UserAgentDefault is the default user agent string.
	UserAgentDefault = "Deepl-Go-Client"
)

Variables

View Source
var (
	// NameEnvKeyAPI is the environment variable name of DeepL API key to search.
	// By default, it is "DEEPL_API_KEY".
	NameEnvKeyAPI = NameEnvKeyAPIDefault
	// UserAgent is the user agent used in HTTP requests. By default, it is
	// "Deepl-Go-Client".
	UserAgent = UserAgentDefault
)
View Source
var AppendErrPos = true

AppendErrPos is a global flag to disable the file name and line number of the caller from the error message. If set to false, it will not be appended.

Functions

func NewErr

func NewErr(msgs ...interface{}) error

NewErr returns a new error object with the given message appending the file name and line number of the caller.

It is a wrapper of errors.New() and errors.Errorf(). Which is the alternative of deprecated github.com/pkg/errors.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-deepl/deepl"
)

func main() {
	if err := deepl.NewErr(); err == nil {
		fmt.Println("empty args returns nil")
	}

	// Note the output contains the file name and line number of the caller.
	if err := deepl.NewErr("simple error message at line 22"); err != nil {
		fmt.Println(err)
	}

	if err := deepl.NewErr("%v error message at line 26", "formatted"); err != nil {
		fmt.Println(err)
	}

	if err := deepl.NewErr("%v error message(s) at line 30", 3); err != nil {
		fmt.Println(err)
	}

	if err := deepl.NewErr(1, 2, 3); err != nil {
		fmt.Println(err)
	}
}
Output:

empty args returns nil
simple error message at line 22 (file: examples_test.go, line: 22)
formatted error message at line 26 (file: examples_test.go, line: 26)
3 error message(s) at line 30 (file: examples_test.go, line: 30)
1 2 3 (file: examples_test.go, line: 34)

func SetCustomURL

func SetCustomURL(url string)

SetCustomURL sets the base URL to be forced to use.

func WrapIfErr

func WrapIfErr(err error, msgs ...interface{}) error

WrapIfErr returns nil if err is nil.

Otherwise, it returns an error annotating err with a stack trace at the point WrapIfErr is called. The supplied message contains the file name and line number of the caller.

Note that if the "msgs" arg is more than one, the first arg is used as a format string and the rest are used as arguments.

E.g.

WrapIfErr(nil, "it wil do nothing")
WrapIfErr(err)                                 // returns err as is
WrapIfErr(err, "failed to do something")       // eq to errors.Wrap
WrapIfErr(err, "failed to do %s", "something") // eq to errors.Wrapf

It is a wrapper of errors.Wrap() and errors.Wrapf(). Which is the alternative of deprecated github.com/pkg/errors.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-deepl/deepl"
)

func main() {
	var err error

	// deepl.WrapIfErr returns nil if err is nil
	fmt.Println("err is nil:", deepl.WrapIfErr(err, "error at line 49"))

	// Cause err to be non-nil
	err = deepl.NewErr("error occurred at line 52")
	// Wrap with no additional message
	fmt.Println("err is non-nil:\n", deepl.WrapIfErr(err))
	// Wrap with additional message
	fmt.Println("err is non-nil:\n", deepl.WrapIfErr(err, "wrapped at line 56"))
}
Output:

err is nil: <nil>
err is non-nil:
 error occurred at line 52 (file: examples_test.go, line: 52)
err is non-nil:
 wrapped at line 56 (file: examples_test.go, line: 56): error occurred at line 52 (file: examples_test.go, line: 52)
Example (Disable_error_position)
package main

import (
	"fmt"

	"github.com/KEINOS/go-deepl/deepl"
)

func main() {
	// Backup and defer restore the original value of deepl.AppendErrPos
	oldAppendErrPos := deepl.AppendErrPos
	defer func() {
		deepl.AppendErrPos = oldAppendErrPos
	}()

	{
		deepl.AppendErrPos = false // Disable appending the error position

		err := deepl.NewErr("error occurred at line 75")
		fmt.Println(deepl.WrapIfErr(err, "wrapped at line 76"))
	}
	{
		deepl.AppendErrPos = true // Enable appending the error position (default)

		err := deepl.NewErr("error occurred at line 81")
		fmt.Println(deepl.WrapIfErr(err, "wrapped at line 82"))
	}
}
Output:

wrapped at line 76: error occurred at line 75
wrapped at line 82 (file: examples_test.go, line: 82): error occurred at line 81 (file: examples_test.go, line: 81)

Types

type APIType

type APIType int

APIType is an enum type for the API version.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-deepl/deepl"
)

func main() {
	fmt.Println(deepl.APIFree)
	fmt.Println(deepl.APIFree.BaseURL())

	fmt.Println(deepl.APIPro)
	fmt.Println(deepl.APIPro.BaseURL())
}
Output:

https://api-free.deepl.com
https://api-free.deepl.com
https://api.deepl.com
https://api.deepl.com
Example (Custom_url)
package main

import (
	"fmt"

	"github.com/KEINOS/go-deepl/deepl"
)

func main() {
	// Force to use the custom URL
	deepl.SetCustomURL("http://localhost:8080")

	defer func() {
		deepl.SetCustomURL("")
	}()

	fmt.Println(deepl.APIFree)
	fmt.Println(deepl.APIPro)
	fmt.Println(deepl.APICustom)
}
Output:

http://localhost:8080
http://localhost:8080
http://localhost:8080
const (
	// APICustom represents the test API in the local server.
	// To use this, you need to set the base URL of the local server
	// via SetCustomURL() method.
	APICustom APIType = iota
	// APIFree represents the DeepL API for free account. It has a limitation
	// of 500,000 characters per month.
	APIFree
	// APIPro represents the DeepL API for pro/paid account. It has no limitation
	// of characters per month.
	APIPro
)

func (APIType) BaseURL

func (a APIType) BaseURL() string

BaseURL returns the base URL of the API.

func (APIType) String

func (a APIType) String() string

String is a Stringer implementation for APIType. Which returns the base URL of the API as it's string representation.

type AccountStatus

type AccountStatus struct {
	CharacterCount int `json:"character_count"`
	CharacterLimit int `json:"character_limit"`
}

type Client

type Client struct {
	BaseURL    *url.URL
	HTTPClient *http.Client
	Logger     *log.Logger
}

Client is a struct that holds the basic client information.

func New

func New(apiType APIType, logger *log.Logger) (*Client, error)

New returns a new Client instance. It will request to the given rawBaseURL and use the given logger. If the logger is nil, it will use the default logger which simply logs to stderr.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-deepl/deepl"
)

func main() {
	// Create a new client for the free account of DeepL API
	// and use the default logger (stderr) by passing nil.
	cli, err := deepl.New(deepl.APIFree, nil)
	if err != nil {
		panic(err)
	}

	fmt.Println(cli.BaseURL)
}
Output:

https://api-free.deepl.com

func (*Client) GetAccountStatus

func (c *Client) GetAccountStatus(ctx context.Context) (*AccountStatus, error)

GetAccountStatus returns the account status. The API key is retrieved via getAPIKey() function which tries to get the API key from the environment variable "DEEPL_API_KEY".

func (*Client) TranslateSentence

func (c *Client) TranslateSentence(
	ctx context.Context,
	text string,
	sourceLang string,
	targetLang string,
) (*TranslateResponse, error)

TranslateSentence translates the given text from the sourceLang to the targetLang.

type ErrorResponse

type ErrorResponse struct {
	ErrMessage string `json:"message"`
}

type TranslateResponse

type TranslateResponse struct {
	Translations []translation `json:"translations"`
}

Jump to

Keyboard shortcuts

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