feat: add http.Client provider with automatic token

This commit is contained in:
Louis Seubert 2024-10-23 22:18:58 +02:00
parent 76a93c0d8c
commit e1f26f703f
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD

29
client.go Normal file
View file

@ -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)
}