33 lines
583 B
Go
33 lines
583 B
Go
package sdk
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
func (a *Action) Client() *Client {
|
|
c := &Client{Client: &http.Client{}}
|
|
context := a.Context()
|
|
c.base = context.APIURL
|
|
c.token = fmt.Sprintf("Bearer %s", context.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)
|
|
}
|