FUNCTIONS: HTTP handler for GET or POST `/$db/_function/$name`
()
| 27 | |
| 28 | // HTTP handler for GET or POST `/$db/_function/$name` |
| 29 | func (h *handler) handleFunctionCall() error { |
| 30 | var maxRequestSize *int |
| 31 | if h.db.Options.UserFunctions != nil { |
| 32 | maxRequestSize = h.db.Options.UserFunctions.MaxRequestSize |
| 33 | } |
| 34 | fnName, fnParams, err := h.getFunctionArgs(maxRequestSize) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | canMutate := h.rq.Method != "GET" |
| 39 | |
| 40 | return db.WithTimeout(h.ctx(), h.db.UserFunctionTimeout, func(ctx context.Context) error { |
| 41 | fn, err := h.db.GetUserFunction(ctx, fnName, fnParams, canMutate) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } else if rows, err := fn.Iterate(); err != nil { |
| 45 | return err |
| 46 | } else if rows != nil { |
| 47 | return h.writeQueryRows(rows) |
| 48 | } else { |
| 49 | // Write the single result to the response: |
| 50 | result, err := fn.Run(ctx) |
| 51 | if err == nil { |
| 52 | h.writeJSON(result) |
| 53 | } |
| 54 | return err |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | // Subroutine for reading function name and arguments from a request |
| 60 | func (h *handler) getFunctionArgs(maxSize *int) (string, map[string]interface{}, error) { |
nothing calls this directly
no test coverage detected