2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-04-30 22:02:28 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2018-10-07 09:40:35 +00:00
|
|
|
"git.xdrm.io/go/aicra/api"
|
2019-05-01 11:44:45 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/config"
|
|
|
|
"git.xdrm.io/go/aicra/internal/reqdata"
|
2018-07-06 08:49:52 +00:00
|
|
|
)
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// extractParameters extracts parameters for the request and checks
|
|
|
|
// every single one according to configuration options
|
|
|
|
func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]*config.Parameter) (map[string]interface{}, api.Error) {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// init vars
|
|
|
|
apiError := api.ErrorSuccess()
|
|
|
|
parameters := make(map[string]interface{})
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// for each param of the config
|
|
|
|
for name, param := range methodParam {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
/* (1) Extract value */
|
|
|
|
p, isset := store.Set[name]
|
|
|
|
|
|
|
|
/* (2) Required & missing */
|
|
|
|
if !isset && !param.Optional {
|
|
|
|
apiError = api.ErrorMissingParam()
|
|
|
|
apiError.Put(name)
|
|
|
|
return nil, apiError
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Optional & missing: set default value */
|
|
|
|
if !isset {
|
|
|
|
p = &reqdata.Parameter{
|
|
|
|
Parsed: true,
|
|
|
|
File: param.Type == "FILE",
|
|
|
|
Value: nil,
|
|
|
|
}
|
|
|
|
if param.Default != nil {
|
|
|
|
p.Value = *param.Default
|
|
|
|
}
|
|
|
|
|
|
|
|
// we are done
|
|
|
|
parameters[param.Rename] = p.Value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Parse parameter if not file */
|
|
|
|
if !p.File {
|
|
|
|
p.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Fail on unexpected multipart file */
|
|
|
|
waitFile, gotFile := param.Type == "FILE", p.File
|
|
|
|
if gotFile && !waitFile || !gotFile && waitFile {
|
|
|
|
apiError = api.ErrorInvalidParam()
|
|
|
|
apiError.Put(param.Rename)
|
|
|
|
apiError.Put("FILE")
|
|
|
|
return nil, apiError
|
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
/* (6) Do not check if file */
|
|
|
|
if gotFile {
|
|
|
|
parameters[param.Rename] = p.Value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (7) Check type */
|
2019-05-01 13:14:49 +00:00
|
|
|
if s.Checkers.Run(param.Type, p.Value) != nil {
|
2019-05-01 11:44:45 +00:00
|
|
|
|
|
|
|
apiError = api.ErrorInvalidParam()
|
|
|
|
apiError.Put(param.Rename)
|
|
|
|
apiError.Put(param.Type)
|
|
|
|
apiError.Put(p.Value)
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters[param.Rename] = p.Value
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
return parameters, apiError
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints an HTTP response
|
|
|
|
func httpPrint(r http.ResponseWriter, res *api.Response) {
|
2019-05-01 14:40:26 +00:00
|
|
|
r.WriteHeader(res.Status)
|
2019-05-01 11:44:45 +00:00
|
|
|
|
2018-07-06 08:49:52 +00:00
|
|
|
// write this json
|
2019-05-01 11:44:45 +00:00
|
|
|
jsonResponse, _ := json.Marshal(res)
|
2018-07-06 08:49:52 +00:00
|
|
|
r.Header().Add("Content-Type", "application/json")
|
|
|
|
r.Write(jsonResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints an error as HTTP response
|
2019-05-01 11:44:45 +00:00
|
|
|
func httpError(r http.ResponseWriter, e api.Error) {
|
|
|
|
JSON, _ := json.Marshal(e)
|
2018-07-06 08:49:52 +00:00
|
|
|
r.Header().Add("Content-Type", "application/json")
|
2018-07-08 23:00:45 +00:00
|
|
|
r.Write(JSON)
|
2018-07-06 08:49:52 +00:00
|
|
|
log.Printf("[http.fail] %s\n", e.Reason)
|
|
|
|
}
|