-
Notifications
You must be signed in to change notification settings - Fork 4
/
deepl.go
219 lines (179 loc) · 5.16 KB
/
deepl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package deepl
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"golang.org/x/xerrors"
)
type Client struct {
BaseURL *url.URL
HTTPClient *http.Client
Logger *log.Logger
}
func New(rawBaseURL string, logger *log.Logger) (*Client, error) {
baseURL, err := url.Parse(rawBaseURL)
if err != nil {
err := xerrors.Errorf("Failed to parse URL")
return nil, err
}
if logger == nil {
logger = log.New(os.Stderr, "[Log]", log.LstdFlags)
}
return &Client{
BaseURL: baseURL,
HTTPClient: http.DefaultClient,
Logger: logger,
}, nil
}
type TranslateResponse struct {
Translations []translation `json:"translations"`
}
type translation struct {
DetectedSourceLanguage string `json:"detected_source_language"`
Text string `json:"text"`
}
type ErrorResponse struct {
ErrMessage string `json:"message"`
}
type AccountStatus struct {
CharacterCount int `json:"character_count"`
CharacterLimit int `json:"character_limit"`
}
func getAPIKey() (string, error) {
// need to parepare setting API key in env
val, ok := os.LookupEnv("DEEPL_API_KEY")
if !ok {
err := xerrors.New("Not set API key environment")
return "", err
} else if val == "" {
err := xerrors.New("DEEPL_API_KEY is empty")
return "", err
}
apiKey := os.Getenv("DEEPL_API_KEY")
return apiKey, nil
}
func decodeBody(bodyBytes []byte, outStruct interface{}) error {
if err := json.Unmarshal(bodyBytes, outStruct); err != nil {
return err
}
return nil
}
func responseParse(resp *http.Response, outStruct interface{}) error {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
err := xerrors.Errorf("Failed to read response: %w", err)
return err
}
// http response failed and received to error message in json
var errResp ErrorResponse
var errMessage string
if resp.StatusCode != http.StatusOK && len(bodyBytes) != 0 {
err := decodeBody(bodyBytes, &errResp)
if err != nil {
return xerrors.Errorf("Failed to decode error response: %w", err)
}
errMessage = errResp.ErrMessage
}
switch resp.StatusCode {
case http.StatusOK:
err := decodeBody(bodyBytes, &outStruct)
if err != nil {
return xerrors.Errorf("Failed to parse Json: %w", err)
}
return nil
case http.StatusBadRequest:
return xerrors.Errorf("Bad request. Please check error message and your parameters. Error message is %s", errMessage)
case http.StatusForbidden:
return xerrors.New("Authorization failed. Please supply a valid auth_key parameter.")
case http.StatusNotFound:
return xerrors.New("The requested resource clould not be found.")
case http.StatusRequestEntityTooLarge:
return xerrors.New("The request size exceeds the limit.")
case http.StatusTooManyRequests:
return xerrors.New("Too many requests. Please wait and resend your request.")
case 456:
return xerrors.New("Quota exceeded. The character limit has been reached.")
case http.StatusServiceUnavailable:
return xerrors.New("Resource currently unavailable. Try again later.")
default:
// Response status code 5** is internal error but error code "503" is http.StatusServiceUnavailable
if resp.StatusCode >= 500 {
return xerrors.New("Internal error")
}
return xerrors.New("Unexpected error")
}
}
func (c *Client) GetAccountStatus(ctx context.Context) (*AccountStatus, error) {
var accountStatusResp AccountStatus
reqURL := *c.BaseURL
// Set path
reqURL.Path = path.Join(reqURL.Path, "v2", "usage")
q := reqURL.Query()
apiKey, err := getAPIKey()
if err != nil {
return nil, err
}
q.Add("auth_key", apiKey)
reqURL.RawQuery = q.Encode()
// make new request
req, err := http.NewRequest(http.MethodPost, reqURL.String(), nil)
if err != nil {
err := xerrors.Errorf("Failed to create request: %w", err)
return nil, err
}
// set header
req.Header.Set("User-Agent", "Deepl-Go-Client")
// set context
req = req.WithContext(ctx)
resp, err := c.HTTPClient.Do(req)
if err != nil {
err := xerrors.Errorf("Failed to send http request: %w", err)
return nil, err
}
defer resp.Body.Close()
if err := responseParse(resp, &accountStatusResp); err != nil {
return nil, err
}
return &accountStatusResp, nil
}
func (c *Client) TranslateSentence(ctx context.Context, text string, sourceLang string, targetLang string) (*TranslateResponse, error) {
var transResp TranslateResponse
reqURL := *c.BaseURL
// Set path
reqURL.Path = path.Join(reqURL.Path, "v2", "translate")
q := reqURL.Query()
apiKey, err := getAPIKey()
if err != nil {
return nil, err
}
q.Add("auth_key", apiKey)
q.Add("text", text)
q.Add("target_lang", targetLang)
q.Add("source_lang", sourceLang)
reqURL.RawQuery = q.Encode()
// make new request
req, err := http.NewRequest(http.MethodPost, reqURL.String(), nil)
if err != nil {
err := xerrors.Errorf("Failed to create request: %w", err)
return nil, err
}
// set header
req.Header.Set("User-Agent", "Deepl-Go-Client")
// set context
req = req.WithContext(ctx)
resp, err := c.HTTPClient.Do(req)
if err != nil {
err := xerrors.Errorf("Failed to send http request: %w", err)
return nil, err
}
defer resp.Body.Close()
if err := responseParse(resp, &transResp); err != nil {
return nil, err
}
return &transResp, nil
}