feat: add http.Client
provider with automatic token
This commit is contained in:
parent
76a93c0d8c
commit
7d728dcbc2
1 changed files with 32 additions and 0 deletions
32
client.go
Normal file
32
client.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
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, err := url.Parse(fmt.Sprintf("%s%s", c.base, req.URL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.URL = u
|
||||
}
|
||||
return c.Client.Do(req)
|
||||
}
|
Loading…
Reference in a new issue