Skip to content

Commit 93c1570

Browse files
Alex-CDgregorriegler
authored andcommitted
Refactor SendRequest to inject http.client, refactor duplicated snippets
- Removed unused getHttpClient function in httpclient.go - Removed httpclient initialisation from sendRequest - inject httpClient into sendRequest, and initialise it using getHttpClient
1 parent f8e68f2 commit 93c1570

File tree

3 files changed

+11
-23
lines changed

3 files changed

+11
-23
lines changed

goal/goal.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func putGoalHttp(goal string, configuration config.Configuration) error {
6464
if err != nil {
6565
return err
6666
}
67-
_, err = httpclient.SendRequest(requestBody, "PUT", getGoalUrl(configuration), configuration.TimerInsecure)
67+
68+
httpClient := httpclient.GetHttpClient(configuration.TimerInsecure)
69+
_, err = httpclient.SendRequest(requestBody, "PUT", getGoalUrl(configuration), httpClient)
70+
6871
return err
6972
}
7073

@@ -87,7 +90,8 @@ func deleteGoalHttp(room string, user string, timerService string, disableSslVer
8790
if err != nil {
8891
return err
8992
}
90-
_, err = httpclient.SendRequest(requestBody, "DELETE", timerService+room+"/goal", disableSslVerification)
93+
httpClient := httpclient.GetHttpClient(disableSslVerification)
94+
_, err = httpclient.SendRequest(requestBody, "DELETE", timerService+room+"/goal", httpClient)
9195
return err
9296
}
9397

httpclient/httpclient.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@ func GetHttpClient(disableSSLVerification bool) *http.Client {
2222
return http.DefaultClient
2323
}
2424

25-
func SendRequest(requestBody []byte, requestMethod string, requestUrl string, disableSSLVerification bool) (string, error) {
25+
func SendRequest(requestBody []byte, requestMethod string, requestUrl string, httpClient *http.Client) (string, error) {
2626
say.Info(requestMethod + " " + requestUrl + " " + string(requestBody))
2727

2828
responseBody := bytes.NewBuffer(requestBody)
2929
request, requestCreationError := http.NewRequest(requestMethod, requestUrl, responseBody)
3030

31-
httpClient := http.DefaultClient
32-
if disableSSLVerification {
33-
transCfg := &http.Transport{
34-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
35-
}
36-
httpClient = &http.Client{Transport: transCfg}
37-
}
38-
3931
if requestCreationError != nil {
4032
return "", fmt.Errorf("failed to create the http request object: %w", requestCreationError)
4133
}
@@ -73,13 +65,3 @@ func SendRequest(requestBody []byte, requestMethod string, requestUrl string, di
7365
}
7466
return body, nil
7567
}
76-
77-
func getHttpClient(disableSSLVerification bool) *http.Client {
78-
if disableSSLVerification {
79-
transCfg := &http.Transport{
80-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
81-
}
82-
return &http.Client{Transport: transCfg}
83-
}
84-
return http.DefaultClient
85-
}

timer.go

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

@@ -166,7 +167,8 @@ func httpPutBreakTimer(timeoutInMinutes int, room string, user string, timerServ
166167
"breaktimer": timeoutInMinutes,
167168
"user": user,
168169
})
169-
_, err := httpclient.SendRequest(putBody, "PUT", timerService+room, disableSSLVerification)
170+
httpClient := httpclient.GetHttpClient(disableSSLVerification)
171+
_, err := httpclient.SendRequest(putBody, "PUT", timerService+room, httpClient)
170172
return err
171173
}
172174

0 commit comments

Comments
 (0)