Files
karma/internal/uri/uri.go
Richard Maynard ec14be0288 feat(backend): add support for custom headers (#368)
This will allow the AlertManager upstreams to be sent user defined HTTP headers.
2019-01-17 08:53:33 +00:00

37 lines
799 B
Go

package uri
import (
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// Reader reads from a specific URI schema
type Reader interface {
Read(string, map[string]string) (io.ReadCloser, error)
}
// NewReader creates an instance of URIReader that can handle URI schema
// for the passed uri string
func NewReader(uri string, timeout time.Duration, clientTransport http.RoundTripper, headers map[string]string) (Reader, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
switch u.Scheme {
case "http", "https":
client := http.Client{
Timeout: timeout,
Transport: clientTransport,
}
return &HTTPURIReader{client: client}, nil
case "file":
return &FileURIReader{}, nil
default:
return nil, fmt.Errorf("unsupported URI scheme '%s' in '%s'", u.Scheme, u)
}
}