WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit b549178

Browse files
author
虫子樱桃
committed
add proxy
1 parent 75bf9c4 commit b549178

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

deepl.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"math/big"
1010
"math/rand"
1111
"net/http"
12+
"net/url"
1213
"strings"
1314
"time"
1415
)
@@ -100,7 +101,14 @@ func adjustJsonContent(id int64, jsonContent []byte) []byte {
100101
// SK: Slovakian
101102
// SL: Slovenian
102103
// SV: Swedish
103-
func Translate(sourceLanguage, targetLanguage, textToTranslate string) (jsonRpcResponse *JsonRpcResponse, err error) {
104+
func Translate(sourceLanguage, targetLanguage, textToTranslate string, options ...Option) (jsonRpcResponse *JsonRpcResponse, err error) {
105+
106+
clientOpt := &deepLClientOption{}
107+
if len(options) > 0 {
108+
for _, optFunc := range options {
109+
optFunc(clientOpt)
110+
}
111+
}
104112
if sourceLanguage == "" {
105113
lang := whatlanggo.DetectLang(textToTranslate)
106114
deepLLang := strings.ToUpper(lang.Iso6391())
@@ -147,6 +155,13 @@ func Translate(sourceLanguage, targetLanguage, textToTranslate string) (jsonRpcR
147155
request.Header.Set("Connection", "keep-alive")
148156

149157
client := &http.Client{}
158+
if clientOpt.httpProxy != "" {
159+
httpProxy, _ := url.Parse(clientOpt.httpProxy)
160+
if httpProxy != nil {
161+
client.Transport = &http.Transport{Proxy: http.ProxyURL(httpProxy)}
162+
}
163+
}
164+
150165
resp, err := client.Do(request)
151166
if err != nil {
152167
return nil, err

deepl_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ func TestTranslate(t *testing.T) {
1010
t.Log(translate.Result.Texts[0].Text)
1111
t.Log(len(translate.Result.Texts[0].Alternatives))
1212
}
13+
func TestTranslateWithHttpProxy(t *testing.T) {
14+
translate, err := Translate("", "zh", "I love Go programming language",
15+
WithHttpProxy("http://127.0.0.1:10808"))
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
t.Log(translate.Result.Texts[0].Text)
20+
t.Log(len(translate.Result.Texts[0].Alternatives))
21+
}

options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package deepl
2+
3+
type deepLClientOption struct {
4+
httpProxy string
5+
}
6+
7+
type Option func(option *deepLClientOption)
8+
9+
func WithHttpProxy(proxy string) Option {
10+
return func(option *deepLClientOption) {
11+
option.httpProxy = proxy
12+
}
13+
}

0 commit comments

Comments
 (0)