mirror of
https://github.com/watn3y/steamsalty.git
synced 2025-10-28 01:21:07 +01:00
feat: Automatic comment translation using DeepL
Some checks failed
Build and Push to Docker Hub on push to any branch / docker (push) Has been cancelled
Some checks failed
Build and Push to Docker Hub on push to any branch / docker (push) Has been cancelled
This commit is contained in:
parent
c6c7480493
commit
1e94b1fc1e
10 changed files with 250 additions and 20 deletions
60
deepL/http.go
Normal file
60
deepL/http.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package deepl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func get(endpoint string) (responseBody []byte, err error) {
|
||||
httpReq, _ := http.NewRequest("GET", client.baseURL+endpoint, nil)
|
||||
httpReq.Header.Set("Authorization", "DeepL-Auth-Key "+client.authKey)
|
||||
|
||||
resp, err := client.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("endpoint", endpoint).Msg("Failed to send GET request to DeepL API")
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Error().Str("response", resp.Status).Str("endpoint", endpoint).Msg("Failed to send GET request to DeepL API, non 200 HTTP response")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("endpoint", endpoint).Msg("Failed to read DeepL API response")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return respBody, nil
|
||||
}
|
||||
|
||||
func post(endpoint string, data []byte) (responseBody []byte, err error) {
|
||||
httpReq, _ := http.NewRequest("POST", client.baseURL+endpoint, bytes.NewReader(data))
|
||||
httpReq.Header.Set("Authorization", "DeepL-Auth-Key "+client.authKey)
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := client.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("endpoint", endpoint).Msg("Failed to send request to POST DeepL API")
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Error().Str("response", resp.Status).Str("endpoint", endpoint).Msg("Failed to send POST request to DeepL API, non 200 HTTP response")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("endpoint", endpoint).Msg("Failed to read DeepL API response")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return respBody, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue