2018-05-14 16:26:38 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-04-11 02:28:21 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/kit/tracing"
|
2018-05-14 16:26:38 +00:00
|
|
|
)
|
|
|
|
|
2018-09-17 02:39:46 +00:00
|
|
|
// Service connects to an InfluxDB via HTTP.
|
|
|
|
type Service struct {
|
|
|
|
Addr string
|
|
|
|
Token string
|
|
|
|
InsecureSkipVerify bool
|
|
|
|
|
|
|
|
*AuthorizationService
|
|
|
|
*OrganizationService
|
|
|
|
*UserService
|
|
|
|
*BucketService
|
2019-02-14 20:32:54 +00:00
|
|
|
*VariableService
|
2018-09-17 02:39:46 +00:00
|
|
|
*DashboardService
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns a service that is an HTTP
|
|
|
|
// client to a remote
|
|
|
|
func NewService(addr, token string) *Service {
|
|
|
|
return &Service{
|
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
AuthorizationService: &AuthorizationService{
|
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
},
|
|
|
|
OrganizationService: &OrganizationService{
|
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
},
|
|
|
|
UserService: &UserService{
|
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
},
|
|
|
|
BucketService: &BucketService{
|
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
},
|
|
|
|
DashboardService: &DashboardService{
|
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
},
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableService: &VariableService{
|
2018-09-18 15:30:52 +00:00
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
},
|
2018-09-17 02:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 05:48:15 +00:00
|
|
|
// NewURL concats addr and path.
|
2019-05-09 17:41:14 +00:00
|
|
|
func NewURL(addr, path string) (*url.URL, error) {
|
2018-05-14 16:26:38 +00:00
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u.Path = path
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2019-06-26 05:48:15 +00:00
|
|
|
// NewClient returns an http.Client that pools connections and injects a span.
|
2019-05-09 17:41:14 +00:00
|
|
|
func NewClient(scheme string, insecure bool) *traceClient {
|
2018-08-15 20:14:51 +00:00
|
|
|
hc := &traceClient{
|
|
|
|
Client: http.Client{
|
|
|
|
Transport: defaultTransport,
|
|
|
|
},
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
|
|
|
if scheme == "https" && insecure {
|
|
|
|
hc.Transport = skipVerifyTransport
|
|
|
|
}
|
|
|
|
|
|
|
|
return hc
|
|
|
|
}
|
2018-08-15 20:14:51 +00:00
|
|
|
|
|
|
|
// traceClient always injects any opentracing trace into the client requests.
|
|
|
|
type traceClient struct {
|
|
|
|
http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do injects the trace and then performs the request.
|
|
|
|
func (c *traceClient) Do(r *http.Request) (*http.Response, error) {
|
2019-04-11 02:28:21 +00:00
|
|
|
span, _ := tracing.StartSpanFromContext(r.Context())
|
2019-06-26 05:48:15 +00:00
|
|
|
defer span.Finish()
|
2019-04-11 02:28:21 +00:00
|
|
|
tracing.InjectToHTTPRequest(span, r)
|
2018-08-15 20:14:51 +00:00
|
|
|
return c.Client.Do(r)
|
|
|
|
}
|