Skip to content

Commit e8d2a56

Browse files
Alex-CDgregorriegler
authored andcommitted
Undo dependency injection, refactor httpclient into struct
1 parent ada7f78 commit e8d2a56

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

goal/goal.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func putGoalHttp(goal string, configuration config.Configuration) error {
6464
if err != nil {
6565
return err
6666
}
67-
httpClient := httpclient.GetHttpClient(configuration.TimerInsecure)
68-
_, err = httpclient.SendRequest(requestBody, "PUT", getGoalUrl(configuration), httpClient)
67+
client := httpclient.CreateHttpClient(configuration.TimerInsecure)
68+
_, err = client.SendRequest(requestBody, "PUT", getGoalUrl(configuration))
6969
return err
7070
}
7171

@@ -88,8 +88,8 @@ func deleteGoalHttp(room string, user string, timerService string, disableSslVer
8888
if err != nil {
8989
return err
9090
}
91-
httpClient := httpclient.GetHttpClient(disableSslVerification)
92-
_, err = httpclient.SendRequest(requestBody, "DELETE", timerService+room+"/goal", httpClient)
91+
client := httpclient.CreateHttpClient(disableSslVerification)
92+
_, err = client.SendRequest(requestBody, "DELETE", timerService+room+"/goal")
9393
return err
9494
}
9595

@@ -108,7 +108,7 @@ func showGoal(configuration config.Configuration) error {
108108
}
109109
func getGoalHttp(room string, timerService string, disableSslVerification bool) (string, error) {
110110
url := timerService + room + "/goal"
111-
response, err := httpclient.GetHttpClient(disableSslVerification).Get(url)
111+
response, err := httpclient.GetNetHttpClient(disableSslVerification).Get(url)
112112
if err != nil {
113113
say.Debug(err.Error())
114114
return "", err

httpclient/httpclient.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ import (
1212
"net/url"
1313
)
1414

15-
func GetHttpClient(disableSSLVerification bool) *http.Client {
15+
type Client interface {
16+
SendRequest(method string, url string, body []byte) error
17+
}
18+
19+
type HttpClient struct {
20+
netHttpClient *http.Client
21+
}
22+
23+
func CreateHttpClient(disableSSLVerification bool) HttpClient {
24+
return HttpClient{
25+
netHttpClient: GetNetHttpClient(disableSSLVerification),
26+
}
27+
}
28+
29+
func GetNetHttpClient(disableSSLVerification bool) *http.Client {
1630
if disableSSLVerification {
1731
transCfg := &http.Transport{
1832
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -22,7 +36,7 @@ func GetHttpClient(disableSSLVerification bool) *http.Client {
2236
return http.DefaultClient
2337
}
2438

25-
func SendRequest(requestBody []byte, requestMethod string, requestUrl string, httpClient *http.Client) (string, error) {
39+
func (c HttpClient) SendRequest(requestBody []byte, requestMethod string, requestUrl string) (string, error) {
2640
say.Info(requestMethod + " " + requestUrl + " " + string(requestBody))
2741

2842
responseBody := bytes.NewBuffer(requestBody)
@@ -33,7 +47,7 @@ func SendRequest(requestBody []byte, requestMethod string, requestUrl string, ht
3347
}
3448

3549
request.Header.Set("Content-Type", "application/json")
36-
response, responseErr := httpClient.Do(request)
50+
response, responseErr := c.netHttpClient.Do(request)
3751
if e, ok := responseErr.(*url.Error); ok {
3852
switch e.Err.(type) {
3953
case x509.UnknownAuthorityError:

timer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ func httpPutTimer(timeoutInMinutes int, room string, user string, timerService s
157157
"timer": timeoutInMinutes,
158158
"user": user,
159159
})
160-
httpClient := httpclient.GetHttpClient(disableSSLVerification)
161-
_, err := httpclient.SendRequest(putBody, "PUT", timerService+room, httpClient)
160+
client := httpclient.CreateHttpClient(disableSSLVerification)
161+
_, err := client.SendRequest(putBody, "PUT", timerService+room)
162162
return err
163163
}
164164

@@ -167,8 +167,8 @@ func httpPutBreakTimer(timeoutInMinutes int, room string, user string, timerServ
167167
"breaktimer": timeoutInMinutes,
168168
"user": user,
169169
})
170-
httpClient := httpclient.GetHttpClient(disableSSLVerification)
171-
_, err := httpclient.SendRequest(putBody, "PUT", timerService+room, httpClient)
170+
client := httpclient.CreateHttpClient(disableSSLVerification)
171+
_, err := client.SendRequest(putBody, "PUT", timerService+room)
172172
return err
173173
}
174174

0 commit comments

Comments
 (0)