Esempio n. 1
0
func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
	var ses graph.HttpSession
	switch params.ByName("query_lang") {
	case "gremlin":
		ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false)
	case "mql":
		ses = mql.NewMqlSession(api.ts)
	default:
		return FormatJson400(w, "Need a query language.")
	}
	var err error
	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return FormatJson400(w, err)
	}
	code := string(bodyBytes)
	result, err := ses.InputParses(code)
	switch result {
	case graph.Parsed:
		var output []byte
		var err error
		output, err = GetQueryShape(code, ses)
		if err != nil {
			return FormatJson400(w, err)
		}
		fmt.Fprint(w, string(output))
		return 200
	case graph.ParseFail:
		return FormatJson400(w, err)
	default:
		return FormatJsonError(w, 500, "Incomplete data?")
	}
	http.Error(w, "", http.StatusNotFound)
	return http.StatusNotFound
}
Esempio n. 2
0
func GetQueryShape(query string, ses graph.HttpSession) ([]byte, error) {
	c := make(chan map[string]interface{}, 5)
	go ses.GetQuery(query, c)
	var data map[string]interface{}
	for res := range c {
		data = res
	}
	return json.Marshal(data)
}
Esempio n. 3
0
func RunJsonQuery(query string, ses graph.HttpSession) (interface{}, error) {
	c := make(chan interface{}, 5)
	go ses.ExecInput(query, c, 100)
	for res := range c {
		ses.BuildJson(res)
	}
	return ses.GetJson()
}