From 9148e34fe2bc7b7d2c5e69b77784928d19e3a139 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 5 Jun 2024 11:16:20 -0600 Subject: Extract methods to send http request --- pkg/gitlab/client.go | 9 +++++++++ pkg/gitlab/group.go | 14 +++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'pkg') diff --git a/pkg/gitlab/client.go b/pkg/gitlab/client.go index e6c79a5..a43e09e 100644 --- a/pkg/gitlab/client.go +++ b/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)) diff --git a/pkg/gitlab/group.go b/pkg/gitlab/group.go index 8fedc45..ff5dfc3 100644 --- a/pkg/gitlab/group.go +++ b/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)) -- cgit v1.2.3