main
1package gitlab
2
3import (
4 "bytes"
5 "context"
6 "fmt"
7 "io"
8 "io/ioutil"
9 "net/http"
10
11 "github.com/xlgmokha/x/pkg/env"
12 "github.com/xlgmokha/x/pkg/x"
13)
14
15type GitLab struct {
16 ctx context.Context
17 client http.Client
18 token string
19 url string
20}
21
22func New(ctx context.Context, token string) *GitLab {
23 return &GitLab{
24 ctx: ctx,
25 client: http.Client{},
26 token: token,
27 url: "https://gitlab.com/api/v4",
28 }
29}
30
31func (gl *GitLab) Group(id int) *Group {
32 return NewGroup(gl, id)
33}
34
35func (gl *GitLab) Get(url string) *http.Response {
36 return gl.execute(gl.newRequest("GET", url))
37}
38
39func (gl *GitLab) newRequest(method string, url string) *http.Request {
40 request := x.Must(http.NewRequestWithContext(gl.ctx, "GET", url, nil))
41 request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", gl.token))
42 return request
43}
44
45func (gl *GitLab) execute(request *http.Request) *http.Response {
46 response := x.Must(gl.client.Do(request))
47
48 if env.Fetch("DUMP", "") != "" {
49 body := x.Must(io.ReadAll(response.Body))
50 fmt.Println(string(body))
51 response.Body = ioutil.NopCloser(bytes.NewBuffer(body))
52 }
53 return response
54}