Skip to content

Commit

Permalink
refactor: Make api client an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Nov 14, 2023
1 parent 833be10 commit f0e90ea
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
34 changes: 2 additions & 32 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion api/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 19 additions & 9 deletions api/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/glossaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit f0e90ea

Please sign in to comment.