2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type response map[string]interface{}
|
|
|
|
|
|
|
|
// TODO: Add tests in utils_http_test.go
|
|
|
|
|
2018-07-24 14:58:43 +08:00
|
|
|
func bodyUnmarshal(r *http.Request, x interface{}) error {
|
2018-05-27 22:40:42 +08:00
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read POST body: %v\n", err)
|
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(b, x); err != nil {
|
|
|
|
return errorInvalidJSONBody
|
|
|
|
}
|
|
|
|
|
|
|
|
xv := reflect.Indirect(reflect.ValueOf(x))
|
|
|
|
for i := 0; i < xv.NumField(); i++ {
|
|
|
|
if xv.Field(i).IsNil() {
|
|
|
|
return errorMissingField
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:58:43 +08:00
|
|
|
func bodyMarshal(w http.ResponseWriter, x map[string]interface{}) error {
|
2018-05-27 22:40:42 +08:00
|
|
|
resp, err := json.Marshal(x)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(`{"success":false,"message":"Some internal error occurred"}`))
|
|
|
|
logger.Errorf("cannot marshal response: %v\n")
|
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(resp)
|
|
|
|
return nil
|
|
|
|
}
|