Commit 9148e34

mo khan <mo@mokhan.ca>
2024-06-05 17:16:20
Extract methods to send http request
1 parent bf9dafc
Changed files (2)
pkg/gitlab/client.go
@@ -33,9 +33,18 @@ func (gl *GitLab) Group(id int) *Group {
 }
 
 func (gl *GitLab) Get(url string) *http.Response {
+	return gl.execute(gl.newRequest("GET", url))
+}
+
+func (gl *GitLab) newRequest(method string, url string) *http.Request {
 	request := x.Must(http.NewRequestWithContext(gl.ctx, "GET", url, nil))
 	request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", gl.token))
+	return request
+}
+
+func (gl *GitLab) execute(request *http.Request) *http.Response {
 	response := x.Must(gl.client.Do(request))
+
 	if env.Fetch("DUMP", "") != "" {
 		body := x.Must(io.ReadAll(response.Body))
 		fmt.Println(string(body))
pkg/gitlab/group.go
@@ -7,21 +7,21 @@ import (
 )
 
 type Group struct {
-	client *GitLab
-	id     int
-	url    string
+	api *GitLab
+	id  int
+	url string
 }
 
 func NewGroup(gl *GitLab, id int) *Group {
 	return &Group{
-		client: gl,
-		id:     id,
-		url:    fmt.Sprintf("%v/groups/%v", gl.url, id),
+		api: gl,
+		id:  id,
+		url: fmt.Sprintf("%v/groups/%v", gl.url, id),
 	}
 }
 
 func (group *Group) Issues() []Issue {
-	response := group.client.Get(group.url + "/issues")
+	response := group.api.Get(group.url + "/issues")
 	defer response.Body.Close()
 
 	return x.Must(FromIssues(response.Body))