feat: artifacts client
All checks were successful
default / ensure tests work (push) Successful in 27s
All checks were successful
default / ensure tests work (push) Successful in 27s
This commit is contained in:
parent
24ad00274d
commit
d77ef9f9c6
1 changed files with 1364 additions and 6 deletions
37
client.go
37
client.go
|
|
@ -2,16 +2,25 @@ package sdk
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NewClient returns an HTTP client that resolves relative request URLs against
|
||||
// base and sets the Authorization header to "Bearer token" on every request.
|
||||
func NewClient(base, token string) *Client {
|
||||
return &Client{
|
||||
Client: &http.Client{},
|
||||
base: base,
|
||||
token: fmt.Sprintf("Bearer %s", token),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
return NewClient(context.APIURL, context.Token)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
|
|
@ -31,3 +40,23 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|||
}
|
||||
return c.Client.Do(req)
|
||||
}
|
||||
|
||||
// HasContentType reports whether the response content-type matches the given
|
||||
// media type. An empty content-type is treated as application/octet-stream.
|
||||
func HasContentType(r *http.Response, mimetype string) bool {
|
||||
contentType := r.Header.Get("content-type")
|
||||
if contentType == "" {
|
||||
return mimetype == "application/octet-stream"
|
||||
}
|
||||
|
||||
for _, v := range strings.Split(contentType, ",") {
|
||||
t, _, err := mime.ParseMediaType(v)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if t == mimetype {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue