diff options
| author | mo khan <mo@mokhan.ca> | 2024-06-05 11:16:20 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2024-06-05 11:16:20 -0600 |
| commit | 9148e34fe2bc7b7d2c5e69b77784928d19e3a139 (patch) | |
| tree | f80f8fe0006ded2a722e1ba1134a44b3be3aea9c | |
| parent | bf9dafca5a42d47af68ee8178b797662d69541bd (diff) | |
Extract methods to send http request
| -rw-r--r-- | pkg/gitlab/client.go | 9 | ||||
| -rw-r--r-- | pkg/gitlab/group.go | 14 |
2 files changed, 16 insertions, 7 deletions
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)) |
