-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
101 lines (84 loc) · 2.82 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package dictionaryapi
import (
"net/http"
"github.com/go-resty/resty/v2"
"github.com/privatesquare/bkst-go-utils/utils/errors"
"github.com/privatesquare/bkst-go-utils/utils/httputils"
"github.com/privatesquare/bkst-go-utils/utils/logger"
)
const (
dictionaryApiUrl = "https://api.dictionaryapi.dev"
)
type (
// Client represents the dictionary api client.
Client struct {
httpClient *resty.Client
Word WordsManager
}
// ClientOption are additional settings that can be passed to the Client.
ClientOption func(*Client)
// Error represents the error returned by the dictionary api.
Error struct {
Title string `json:"title"`
Message string `json:"message"`
StatusCode int `json:"statusCode"`
Resolution string `json:"resolution"`
}
)
// NewClient returns a instance of Client with the default settings.
// These settings can be optionally modified using ClientOptions that can be passed to NewClient.
func NewClient(opts ...ClientOption) *Client {
c := &Client{
httpClient: resty.New(),
}
c.Word = &wordManager{Client: c}
for _, opt := range opts {
opt(c)
}
return c
}
// WithHTTPClient can be used as a ClientOption to pass a custom httpClient.
func WithHTTPClient(httpClient *resty.Client) ClientOption {
return func(c *Client) {
c.httpClient = httpClient
}
}
// WithWordsManager can be used as a ClientIOption to pass a custom implementation of the WordsManage interface.
func WithWordsManager(wm WordsManager) ClientOption {
return func(c *Client) {
c.Word = wm
}
}
// request formats and returns a base http request that can be extended later.
// as part of this the baseUrl and the default headers are set in the http client.
func (c *Client) request() *resty.Request {
c.httpClient.SetBaseURL(dictionaryApiUrl)
c.httpClient.SetDisableWarn(true)
return c.httpClient.R().SetHeader(httputils.ContentTypeHeaderKey, httputils.ApplicationJsonMIMEType).
SetHeader(httputils.AcceptHeaderKey, httputils.ApplicationJsonMIMEType)
}
// get makes a GET API call to the dictionaryapi based on the URI passed as input.
// The result is unmarshal-ed into the result interface.
// The method returns an errors.RestErr if
// - there is an error while making the http request
// - the Status code returned by the dictionary API is not 200 - Status Ok
func (c *Client) get(uri string, result interface{}) *errors.RestErr {
apiError := new(Error)
resp, err := c.request().SetResult(result).SetError(apiError).Get(uri)
logger.RestyDebugLogs(resp)
if err != nil {
return &errors.RestErr{
Message: http.StatusText(http.StatusInternalServerError),
StatusCode: http.StatusInternalServerError,
Error: err.Error(),
}
}
if resp.StatusCode() != http.StatusOK {
return &errors.RestErr{
Message: apiError.Message,
StatusCode: resp.StatusCode(),
Error: apiError.Title,
}
}
return nil
}