feat(http): add support for get /api/v2/query

pull/10616/head
Michael Desa 2019-01-02 14:36:16 -05:00
parent 6efa557281
commit cf2ba751db
3 changed files with 43 additions and 5 deletions

View File

@ -337,8 +337,18 @@ func QueryRequestFromProxyRequest(req *query.ProxyRequest) (*QueryRequest, error
func decodeQueryRequest(ctx context.Context, r *http.Request, svc platform.OrganizationService) (*QueryRequest, error) {
var req QueryRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, err
// TODO(desa): I'm not sure I like this kind of conditional logic, but it feels better than
// introducing another method that does this exact thing.
if r.Method == http.MethodGet {
qp := r.URL.Query()
req.Query = qp.Get("query")
if req.Query == "" {
return nil, errors.New("query param \"query\" is required")
}
} else {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, err
}
}
req = req.WithDefaults()

View File

@ -47,7 +47,8 @@ func NewFluxHandler() *FluxHandler {
Logger: zap.NewNop(),
}
h.HandlerFunc("POST", fluxPath, h.handlePostQuery)
h.HandlerFunc("POST", fluxPath, h.handleQuery)
h.HandlerFunc("GET", fluxPath, h.handleQuery)
h.HandlerFunc("POST", "/api/v2/query/ast", h.postFluxAST)
h.HandlerFunc("POST", "/api/v2/query/analyze", h.postQueryAnalyze)
h.HandlerFunc("POST", "/api/v2/query/spec", h.postFluxSpec)
@ -56,7 +57,7 @@ func NewFluxHandler() *FluxHandler {
return h
}
func (h *FluxHandler) handlePostQuery(w http.ResponseWriter, r *http.Request) {
func (h *FluxHandler) handleQuery(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
a, err := pcontext.GetAuthorizer(ctx)

View File

@ -436,7 +436,7 @@ func Test_decodeProxyQueryRequest(t *testing.T) {
wantErr bool
}{
{
name: "valid query request",
name: "valid post query request",
args: args{
r: httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"query": "from()"}`)),
svc: &mock.OrganizationService{
@ -462,6 +462,33 @@ func Test_decodeProxyQueryRequest(t *testing.T) {
},
},
},
{
name: "valid get query request",
args: args{
r: httptest.NewRequest("GET", "/api/v2/query?query=from(bucket%3A%20%22mybucket%22)&org=myorg", nil),
svc: &mock.OrganizationService{
FindOrganizationF: func(ctx context.Context, filter platform.OrganizationFilter) (*platform.Organization, error) {
return &platform.Organization{
ID: func() platform.ID { s, _ := platform.IDFromString("deadbeefdeadbeef"); return *s }(),
}, nil
},
},
},
want: &query.ProxyRequest{
Request: query.Request{
OrganizationID: func() platform.ID { s, _ := platform.IDFromString("deadbeefdeadbeef"); return *s }(),
Compiler: lang.FluxCompiler{
Query: "from(bucket: \"mybucket\")",
},
},
Dialect: &csv.Dialect{
ResultEncoderConfig: csv.ResultEncoderConfig{
NoHeader: false,
Delimiter: ',',
},
},
},
},
}
var cmpOptions = cmp.Options{
cmpopts.IgnoreUnexported(query.ProxyRequest{}),