From e1f26f703f69a43de86d008b6bf6cf81f9b09a36 Mon Sep 17 00:00:00 2001 From: Louis Seubert Date: Wed, 23 Oct 2024 22:18:58 +0200 Subject: [PATCH] feat: add `http.Client` provider with automatic token --- client.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 client.go diff --git a/client.go b/client.go new file mode 100644 index 0000000..a61f97c --- /dev/null +++ b/client.go @@ -0,0 +1,29 @@ +package sdk + +import ( + "fmt" + "net/http" + "net/url" +) + +func (a *Action) Client() *Client { + c := &Client{Client: &http.Client{}} + c.base = a.env("GITHUB_API_URL") + c.token = fmt.Sprintf("Bearer %s", a.env("GITHUB_TOKEN")) + return c +} + +type Client struct { + *http.Client + base string + token string +} + +func (c *Client) Do(req *http.Request) (*http.Response, error) { + req.Header.Set("Authorization", c.token) + if !req.URL.IsAbs() { + u, _ := url.Parse(fmt.Sprintf("%s%s", c.base, req.URL)) + req.URL = u + } + return c.Client.Do(req) +}