MCPcopy
hub / github.com/cayleygraph/cayley / ServeV1Query

Method ServeV1Query

internal/http/query.go:78–142  ·  view source on GitHub ↗

TODO(barakmich): Turn this into proper middleware.

(w http.ResponseWriter, r *http.Request, params httprouter.Params)

Source from the content-addressed store, hash-verified

76
77// TODO(barakmich): Turn this into proper middleware.
78func (api *API) ServeV1Query(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
79 ctx, cancel := api.contextForRequest(r)
80 defer cancel()
81 l := query.GetLanguage(params.ByName("query_lang"))
82 if l == nil {
83 jsonResponse(w, http.StatusBadRequest, "Unknown query language.")
84 return
85 }
86 errFunc := defaultErrorFunc
87 if l.HTTPError != nil {
88 errFunc = l.HTTPError
89 }
90 select {
91 case <-ctx.Done():
92 errFunc(w, ctx.Err())
93 return
94 default:
95 }
96 h, err := api.GetHandleForRequest(r)
97 if err != nil {
98 errFunc(w, err)
99 return
100 }
101 if l.HTTPQuery != nil {
102 defer r.Body.Close()
103 l.HTTPQuery(ctx, h.QuadStore, w, r.Body)
104 return
105 }
106 if l.HTTP == nil {
107 errFunc(w, errors.New("HTTP interface is not supported for this query language."))
108 return
109 }
110
111 par, _ := url.ParseQuery(r.URL.RawQuery)
112 limit, _ := strconv.Atoi(par.Get("limit"))
113 if limit == 0 {
114 limit = 100
115 }
116
117 ses := l.HTTP(h.QuadStore)
118 bodyBytes, err := ioutil.ReadAll(r.Body)
119 if err != nil {
120 errFunc(w, err)
121 return
122 }
123 it, err := ses.Execute(ctx, string(bodyBytes), query.Options{
124 Collation: query.JSON,
125 Limit: limit,
126 })
127 if err != nil {
128 errFunc(w, err)
129 return
130 }
131 defer it.Close()
132
133 var out []interface{}
134 for it.Next(ctx) {
135 out = append(out, it.Result())

Callers

nothing calls this directly

Calls 11

contextForRequestMethod · 0.95
GetHandleForRequestMethod · 0.95
GetLanguageFunction · 0.92
WriteResultFunction · 0.85
jsonResponseFunction · 0.70
ErrMethod · 0.65
CloseMethod · 0.65
ExecuteMethod · 0.65
NextMethod · 0.65
ResultMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected