Skip to content

Commit

Permalink
Undo dependency injection, refactor httpclient into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-CD authored and gregorriegler committed Oct 15, 2024
1 parent ada7f78 commit e8d2a56
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
10 changes: 5 additions & 5 deletions goal/goal.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func putGoalHttp(goal string, configuration config.Configuration) error {
if err != nil {
return err
}
httpClient := httpclient.GetHttpClient(configuration.TimerInsecure)
_, err = httpclient.SendRequest(requestBody, "PUT", getGoalUrl(configuration), httpClient)
client := httpclient.CreateHttpClient(configuration.TimerInsecure)
_, err = client.SendRequest(requestBody, "PUT", getGoalUrl(configuration))
return err
}

Expand All @@ -88,8 +88,8 @@ func deleteGoalHttp(room string, user string, timerService string, disableSslVer
if err != nil {
return err
}
httpClient := httpclient.GetHttpClient(disableSslVerification)
_, err = httpclient.SendRequest(requestBody, "DELETE", timerService+room+"/goal", httpClient)
client := httpclient.CreateHttpClient(disableSslVerification)
_, err = client.SendRequest(requestBody, "DELETE", timerService+room+"/goal")
return err
}

Expand All @@ -108,7 +108,7 @@ func showGoal(configuration config.Configuration) error {
}
func getGoalHttp(room string, timerService string, disableSslVerification bool) (string, error) {
url := timerService + room + "/goal"
response, err := httpclient.GetHttpClient(disableSslVerification).Get(url)
response, err := httpclient.GetNetHttpClient(disableSslVerification).Get(url)
if err != nil {
say.Debug(err.Error())
return "", err
Expand Down
20 changes: 17 additions & 3 deletions httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ import (
"net/url"
)

func GetHttpClient(disableSSLVerification bool) *http.Client {
type Client interface {
SendRequest(method string, url string, body []byte) error
}

type HttpClient struct {
netHttpClient *http.Client
}

func CreateHttpClient(disableSSLVerification bool) HttpClient {
return HttpClient{
netHttpClient: GetNetHttpClient(disableSSLVerification),
}
}

func GetNetHttpClient(disableSSLVerification bool) *http.Client {
if disableSSLVerification {
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Expand All @@ -22,7 +36,7 @@ func GetHttpClient(disableSSLVerification bool) *http.Client {
return http.DefaultClient
}

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

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

request.Header.Set("Content-Type", "application/json")
response, responseErr := httpClient.Do(request)
response, responseErr := c.netHttpClient.Do(request)
if e, ok := responseErr.(*url.Error); ok {
switch e.Err.(type) {
case x509.UnknownAuthorityError:
Expand Down
8 changes: 4 additions & 4 deletions timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func httpPutTimer(timeoutInMinutes int, room string, user string, timerService s
"timer": timeoutInMinutes,
"user": user,
})
httpClient := httpclient.GetHttpClient(disableSSLVerification)
_, err := httpclient.SendRequest(putBody, "PUT", timerService+room, httpClient)
client := httpclient.CreateHttpClient(disableSSLVerification)
_, err := client.SendRequest(putBody, "PUT", timerService+room)
return err
}

Expand All @@ -167,8 +167,8 @@ func httpPutBreakTimer(timeoutInMinutes int, room string, user string, timerServ
"breaktimer": timeoutInMinutes,
"user": user,
})
httpClient := httpclient.GetHttpClient(disableSSLVerification)
_, err := httpclient.SendRequest(putBody, "PUT", timerService+room, httpClient)
client := httpclient.CreateHttpClient(disableSSLVerification)
_, err := client.SendRequest(putBody, "PUT", timerService+room)
return err
}

Expand Down

0 comments on commit e8d2a56

Please sign in to comment.