Files
grafana/pkg/util/url.go
T

53 lines
1016 B
Go
Raw Normal View History

2015-01-08 09:00:00 +01:00
package util
2014-10-03 09:03:16 +02:00
import (
"net/url"
"strings"
2014-10-03 09:03:16 +02:00
)
// URLQueryReader is a ApiURL query type.
type URLQueryReader struct {
2014-10-03 09:03:16 +02:00
values url.Values
}
// NewURLQueryReader parses a raw query and returns it as a URLQueryReader type.
func NewURLQueryReader(urlInfo *url.URL) (*URLQueryReader, error) {
u, err := url.ParseQuery(urlInfo.RawQuery)
2017-11-28 22:20:22 +08:00
if err != nil {
return nil, err
2014-10-03 09:03:16 +02:00
}
2017-11-28 22:20:22 +08:00
return &URLQueryReader{
2017-11-28 22:20:22 +08:00
values: u,
}, nil
2014-10-03 09:03:16 +02:00
}
// Get parse parameters from an ApiURL. If the parameter does not exist, it returns
2019-01-28 22:09:40 +01:00
// the default value.
func (r *URLQueryReader) Get(name string, def string) string {
2014-10-03 09:03:16 +02:00
val := r.values[name]
if len(val) == 0 {
return def
}
return val[0]
}
// JoinURLFragments joins two ApiURL fragments into only one ApiURL string.
func JoinURLFragments(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
if len(b) == 0 {
return a
}
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}