func runQuery(queryLang, code string) (interface{}, error) { qs := &QuadStore{} var ses query.HTTP switch queryLang { case "gremlin": ses = gremlin.NewSession(qs, 100*time.Second, false) case "mql": ses = mql.NewSession(qs) default: } result, err := ses.Parse(code) switch result { case query.Parsed: output, err := Run(code, ses) if err != nil { return nil, err } return output, nil case query.ParseFail: ses = nil return nil, err default: ses = nil return nil, fmt.Errorf("Incomplete data?") } }
func (api *API) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int { h, err := api.GetHandleForRequest(r) var ses query.HTTP switch params.ByName("query_lang") { case "gremlin": ses = gremlin.NewSession(h.QuadStore, api.config.Timeout, false) case "mql": ses = mql.NewSession(h.QuadStore) default: return jsonResponse(w, 400, "Need a query language.") } bodyBytes, err := ioutil.ReadAll(r.Body) if err != nil { return jsonResponse(w, 400, err) } code := string(bodyBytes) result, err := ses.Parse(code) switch result { case query.Parsed: var output []byte var err error output, err = GetQueryShape(code, ses) if err != nil { return jsonResponse(w, 400, err) } fmt.Fprint(w, string(output)) return 200 case query.ParseFail: return jsonResponse(w, 400, err) default: return jsonResponse(w, 500, "Incomplete data?") } }