diff --git a/api/client.go b/api/client.go index 8490267..4ca41f5 100644 --- a/api/client.go +++ b/api/client.go @@ -3,23 +3,10 @@ package deepl import ( "fmt" "net/http" - "net/url" - "strings" - "time" ) -type Client struct { - httpClient *http.Client -} - -func NewClient(timeout time.Duration) *Client { - client := &http.Client{ - Timeout: timeout, - } - - return &Client{ - httpClient: client, - } +type HTTPClient interface { + Do(*http.Request) (*http.Response, error) } type HTTPError struct { @@ -34,20 +21,3 @@ func (err HTTPError) Error() string { return fmt.Sprintf("%d - %s", err.StatusCode, http.StatusText(err.StatusCode)) } } - -func (c *Client) do(method string, url string, data url.Values, headers http.Header) (*http.Response, error) { - req, err := http.NewRequest(method, url, strings.NewReader(data.Encode())) - if err != nil { - return nil, err - } - - for k, vs := range headers { - for _, v := range vs { - req.Header.Add(k, v) - } - } - - res, err := c.httpClient.Do(req) - - return res, err -} diff --git a/api/document.go b/api/document.go index 6bb7239..13d5db8 100644 --- a/api/document.go +++ b/api/document.go @@ -81,7 +81,7 @@ func (t *Translator) TranslateDocumentUpload(filePath string, targetLang string, req.Header.Set("Authorization", fmt.Sprintf("DeepL-Auth-Key %s", t.authKey)) req.Header.Add("Content-Type", mpw.FormDataContentType()) - res, err := t.httpClient.httpClient.Do(req) + res, err := t.client.Do(req) if err != nil { return nil, err } else if res.StatusCode != http.StatusOK { diff --git a/api/translator.go b/api/translator.go index 156512f..876c899 100644 --- a/api/translator.go +++ b/api/translator.go @@ -14,9 +14,9 @@ const ( ) type Translator struct { - httpClient *Client - serverURL string - authKey string + client HTTPClient + serverURL string + authKey string } // TranslatorOption is a functional option for configuring the Translator @@ -54,12 +54,13 @@ func NewTranslator(authKey string, opts ...TranslatorOption) (*Translator, error // Set up default http client timeout := time.Second * 30 - httpClient := NewClient(timeout) t := &Translator{ - httpClient: httpClient, - serverURL: serverURL, - authKey: authKey, + client: &http.Client{ + Timeout: timeout, + }, + serverURL: serverURL, + authKey: authKey, } // Parse and apply options @@ -82,9 +83,18 @@ func (t *Translator) callAPI(method string, endpoint string, data url.Values, he headers.Set("Content-Type", "application/x-www-form-urlencoded") } - res, err := t.httpClient.do(method, url, data, headers) + req, err := http.NewRequest(method, url, strings.NewReader(data.Encode())) + if err != nil { + return nil, err + } + + for k, vs := range headers { + for _, v := range vs { + req.Header.Add(k, v) + } + } - return res, err + return t.client.Do(req) } // authKeyIsFreeAccount determines whether the supplied auth key belongs to a Free account diff --git a/internal/cmd/document.go b/internal/cmd/document.go index f3ea905..13bc954 100644 --- a/internal/cmd/document.go +++ b/internal/cmd/document.go @@ -11,8 +11,8 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" - table "github.com/cluttrdev/deepl-go/internal" deepl "github.com/cluttrdev/deepl-go/api" + table "github.com/cluttrdev/deepl-go/internal" ) var documentCmd = &cobra.Command{ diff --git a/internal/cmd/glossaries.go b/internal/cmd/glossaries.go index e08a19a..de4d36a 100644 --- a/internal/cmd/glossaries.go +++ b/internal/cmd/glossaries.go @@ -8,8 +8,8 @@ import ( "github.com/spf13/cobra" - table "github.com/cluttrdev/deepl-go/internal" deepl "github.com/cluttrdev/deepl-go/api" + table "github.com/cluttrdev/deepl-go/internal" ) var glossaryCmd = &cobra.Command{