Skip to content

Commit f0e90ea

Browse files
committed
refactor: Make api client an interface
1 parent 833be10 commit f0e90ea

File tree

5 files changed

+24
-44
lines changed

5 files changed

+24
-44
lines changed

api/client.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,10 @@ package deepl
33
import (
44
"fmt"
55
"net/http"
6-
"net/url"
7-
"strings"
8-
"time"
96
)
107

11-
type Client struct {
12-
httpClient *http.Client
13-
}
14-
15-
func NewClient(timeout time.Duration) *Client {
16-
client := &http.Client{
17-
Timeout: timeout,
18-
}
19-
20-
return &Client{
21-
httpClient: client,
22-
}
8+
type HTTPClient interface {
9+
Do(*http.Request) (*http.Response, error)
2310
}
2411

2512
type HTTPError struct {
@@ -34,20 +21,3 @@ func (err HTTPError) Error() string {
3421
return fmt.Sprintf("%d - %s", err.StatusCode, http.StatusText(err.StatusCode))
3522
}
3623
}
37-
38-
func (c *Client) do(method string, url string, data url.Values, headers http.Header) (*http.Response, error) {
39-
req, err := http.NewRequest(method, url, strings.NewReader(data.Encode()))
40-
if err != nil {
41-
return nil, err
42-
}
43-
44-
for k, vs := range headers {
45-
for _, v := range vs {
46-
req.Header.Add(k, v)
47-
}
48-
}
49-
50-
res, err := c.httpClient.Do(req)
51-
52-
return res, err
53-
}

api/document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (t *Translator) TranslateDocumentUpload(filePath string, targetLang string,
8181
req.Header.Set("Authorization", fmt.Sprintf("DeepL-Auth-Key %s", t.authKey))
8282
req.Header.Add("Content-Type", mpw.FormDataContentType())
8383

84-
res, err := t.httpClient.httpClient.Do(req)
84+
res, err := t.client.Do(req)
8585
if err != nil {
8686
return nil, err
8787
} else if res.StatusCode != http.StatusOK {

api/translator.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const (
1414
)
1515

1616
type Translator struct {
17-
httpClient *Client
18-
serverURL string
19-
authKey string
17+
client HTTPClient
18+
serverURL string
19+
authKey string
2020
}
2121

2222
// TranslatorOption is a functional option for configuring the Translator
@@ -54,12 +54,13 @@ func NewTranslator(authKey string, opts ...TranslatorOption) (*Translator, error
5454

5555
// Set up default http client
5656
timeout := time.Second * 30
57-
httpClient := NewClient(timeout)
5857

5958
t := &Translator{
60-
httpClient: httpClient,
61-
serverURL: serverURL,
62-
authKey: authKey,
59+
client: &http.Client{
60+
Timeout: timeout,
61+
},
62+
serverURL: serverURL,
63+
authKey: authKey,
6364
}
6465

6566
// Parse and apply options
@@ -82,9 +83,18 @@ func (t *Translator) callAPI(method string, endpoint string, data url.Values, he
8283
headers.Set("Content-Type", "application/x-www-form-urlencoded")
8384
}
8485

85-
res, err := t.httpClient.do(method, url, data, headers)
86+
req, err := http.NewRequest(method, url, strings.NewReader(data.Encode()))
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
for k, vs := range headers {
92+
for _, v := range vs {
93+
req.Header.Add(k, v)
94+
}
95+
}
8696

87-
return res, err
97+
return t.client.Do(req)
8898
}
8999

90100
// authKeyIsFreeAccount determines whether the supplied auth key belongs to a Free account

internal/cmd/document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/spf13/cobra"
1212
"github.com/spf13/pflag"
1313

14-
table "github.com/cluttrdev/deepl-go/internal"
1514
deepl "github.com/cluttrdev/deepl-go/api"
15+
table "github.com/cluttrdev/deepl-go/internal"
1616
)
1717

1818
var documentCmd = &cobra.Command{

internal/cmd/glossaries.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
"github.com/spf13/cobra"
1010

11-
table "github.com/cluttrdev/deepl-go/internal"
1211
deepl "github.com/cluttrdev/deepl-go/api"
12+
table "github.com/cluttrdev/deepl-go/internal"
1313
)
1414

1515
var glossaryCmd = &cobra.Command{

0 commit comments

Comments
 (0)