added timout to deepl post request
This commit is contained in:
@@ -51,7 +51,10 @@ csgowtfd:
|
||||
write: 30
|
||||
# seconds before closing idle connections
|
||||
idle: 120
|
||||
deepl:
|
||||
|
||||
deepl:
|
||||
base_url: api-free.deepl.com
|
||||
# get your API key on deepl.com
|
||||
api_key: <api_key>
|
||||
# timeout in sec to wait for deepl response
|
||||
timeout: 15
|
2
main.go
2
main.go
@@ -702,7 +702,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
if translate {
|
||||
lang, _ := tag[0].Base()
|
||||
translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(),
|
||||
conf.Csgowtfd.DeepL.BaseURL, conf.Csgowtfd.DeepL.APIKey)
|
||||
conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout)
|
||||
if err != nil {
|
||||
log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err)
|
||||
goto sendNormalResp
|
||||
|
@@ -70,11 +70,12 @@ type Conf struct {
|
||||
Write int
|
||||
Idle int
|
||||
}
|
||||
}
|
||||
DeepL struct {
|
||||
BaseURL string `yaml:"base_url"`
|
||||
APIKey string `yaml:"api_key"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
} `yaml:"deepl"`
|
||||
}
|
||||
}
|
||||
|
||||
type CommunityXML struct {
|
||||
@@ -687,18 +688,22 @@ func PlayerFromSteamID64(db *ent.Client, steamID uint64, apiKey string, rl ratel
|
||||
}
|
||||
}
|
||||
|
||||
func TranslateWithDeepL(text string, language string, baseURL string, apiKey string) (translated string, detectedLanguage string, err error) {
|
||||
func TranslateWithDeepL(text string, language string, baseURL string, apiKey string, timeout int) (translated string, detectedLanguage string, err error) {
|
||||
c := &http.Client{
|
||||
Timeout: time.Duration(timeout) * time.Second,
|
||||
}
|
||||
v := url.Values{}
|
||||
v.Set("auth_key", apiKey)
|
||||
v.Set("text", text)
|
||||
v.Set("target_lang", language)
|
||||
dlResp, err := http.PostForm("https://"+baseURL+"/v2/translate", v)
|
||||
dlResp, err := c.PostForm("https://"+baseURL+"/v2/translate", v)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("deepl response: %w", err)
|
||||
} else if dlResp.StatusCode != http.StatusOK {
|
||||
return "", "", fmt.Errorf("deepl response %d", dlResp.StatusCode)
|
||||
} else {
|
||||
respBytes, err := io.ReadAll(dlResp.Body)
|
||||
defer dlResp.Body.Close()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("error reading deepl response: %w", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user