steamsalty/deepL/http.go
Noah Theus 1e94b1fc1e
Some checks failed
Build and Push to Docker Hub on push to any branch / docker (push) Has been cancelled
feat: Automatic comment translation using DeepL
2025-10-13 06:08:26 +02:00

60 lines
1.7 KiB
Go

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
}