added timout to deepl post request
This commit is contained in:
@@ -51,7 +51,10 @@ csgowtfd:
|
|||||||
write: 30
|
write: 30
|
||||||
# seconds before closing idle connections
|
# seconds before closing idle connections
|
||||||
idle: 120
|
idle: 120
|
||||||
deepl:
|
|
||||||
base_url: api-free.deepl.com
|
deepl:
|
||||||
# get your API key on deepl.com
|
base_url: api-free.deepl.com
|
||||||
api_key: <api_key>
|
# 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 {
|
if translate {
|
||||||
lang, _ := tag[0].Base()
|
lang, _ := tag[0].Base()
|
||||||
translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(),
|
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 {
|
if err != nil {
|
||||||
log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err)
|
log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err)
|
||||||
goto sendNormalResp
|
goto sendNormalResp
|
||||||
|
@@ -70,11 +70,12 @@ type Conf struct {
|
|||||||
Write int
|
Write int
|
||||||
Idle int
|
Idle int
|
||||||
}
|
}
|
||||||
DeepL struct {
|
|
||||||
BaseURL string `yaml:"base_url"`
|
|
||||||
APIKey string `yaml:"api_key"`
|
|
||||||
} `yaml:"deepl"`
|
|
||||||
}
|
}
|
||||||
|
DeepL struct {
|
||||||
|
BaseURL string `yaml:"base_url"`
|
||||||
|
APIKey string `yaml:"api_key"`
|
||||||
|
Timeout int `yaml:"timeout"`
|
||||||
|
} `yaml:"deepl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommunityXML struct {
|
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 := url.Values{}
|
||||||
v.Set("auth_key", apiKey)
|
v.Set("auth_key", apiKey)
|
||||||
v.Set("text", text)
|
v.Set("text", text)
|
||||||
v.Set("target_lang", language)
|
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 {
|
if err != nil {
|
||||||
return "", "", fmt.Errorf("deepl response: %w", err)
|
return "", "", fmt.Errorf("deepl response: %w", err)
|
||||||
} else if dlResp.StatusCode != http.StatusOK {
|
} else if dlResp.StatusCode != http.StatusOK {
|
||||||
return "", "", fmt.Errorf("deepl response %d", dlResp.StatusCode)
|
return "", "", fmt.Errorf("deepl response %d", dlResp.StatusCode)
|
||||||
} else {
|
} else {
|
||||||
respBytes, err := io.ReadAll(dlResp.Body)
|
respBytes, err := io.ReadAll(dlResp.Body)
|
||||||
|
defer dlResp.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", fmt.Errorf("error reading deepl response: %w", err)
|
return "", "", fmt.Errorf("error reading deepl response: %w", err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user