Drop ParseIntParam() as it is used only in one place

This commit is contained in:
Krombel 2018-08-07 17:22:26 +02:00
parent 1878b75367
commit 84fde777d9

View file

@ -22,39 +22,25 @@ import (
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
// ParseIntParam takes a request and parses the query param to an int
// returns 0 when not present and -1 when parsing failed as int
func ParseIntParam(req *http.Request, param string) (int64, *util.JSONResponse) {
paramStr := req.URL.Query().Get(param)
if paramStr == "" {
return 0, &util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.MissingArgument(fmt.Sprintf("Param %s not found", param)),
}
}
paramInt, err := strconv.ParseInt(paramStr, 0, 64)
if err != nil {
return -1, &util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(
fmt.Sprintf("Param %s is no valid int (%s)", param, err.Error()),
),
}
}
return paramInt, nil
}
// ParseTSParam takes a req (typically from an application service) and parses a Time object // ParseTSParam takes a req (typically from an application service) and parses a Time object
// from the req if it exists in the query parameters. If it doesn't exist, the // from the req if it exists in the query parameters. If it doesn't exist, the
// current time is returned. // current time is returned.
func ParseTSParam(req *http.Request) (time.Time, *util.JSONResponse) { func ParseTSParam(req *http.Request) (time.Time, *util.JSONResponse) {
ts, err := ParseIntParam(req, "ts") // Use the ts parameter's value for event time if present
tsStr := req.URL.Query().Get("ts")
if tsStr == "" {
return time.Now(), nil
}
// The parameter exists, parse into a Time object
ts, err := strconv.ParseInt(tsStr, 10, 64)
if err != nil { if err != nil {
if ts == 0 { return time.Time{}, &util.JSONResponse{
// param was not available Code: http.StatusBadRequest,
return time.Now(), nil JSON: jsonerror.InvalidArgumentValue(
fmt.Sprintf("Param 'ts' is no valid int (%s)", err.Error()),
),
} }
return time.Time{}, err
} }
return time.Unix(ts/1000, 0), nil return time.Unix(ts/1000, 0), nil