fathom/pkg/api/params.go

46 lines
853 B
Go
Raw Normal View History

package api
import (
2016-12-11 13:50:01 +00:00
"net/http"
"strconv"
2016-12-11 14:33:23 +00:00
"strings"
2016-12-11 13:50:01 +00:00
"time"
)
2018-05-07 16:59:52 +00:00
type Params struct {
Limit int
StartDate time.Time
EndDate time.Time
}
2018-05-07 16:59:52 +00:00
func GetRequestParams(r *http.Request) *Params {
params := &Params{
Limit: 20,
StartDate: time.Now(),
EndDate: time.Now().AddDate(0, 0, -7),
2016-12-11 13:50:01 +00:00
}
2018-05-07 16:59:52 +00:00
q := r.URL.Query()
if after, err := strconv.ParseInt(q.Get("after"), 10, 64); err == nil && after > 0 {
params.StartDate = time.Unix(after, 0)
}
2018-05-07 16:59:52 +00:00
if before, err := strconv.ParseInt(q.Get("before"), 10, 64); err == nil && before > 0 {
params.EndDate = time.Unix(before, 0)
2016-12-11 13:50:01 +00:00
}
2018-05-07 16:59:52 +00:00
if limit, err := strconv.Atoi(q.Get("limit")); err == nil && limit > 0 {
params.Limit = limit
2016-12-11 13:50:01 +00:00
}
2018-05-07 16:59:52 +00:00
return params
}
2016-12-11 14:33:23 +00:00
func parseMajorMinor(v string) string {
parts := strings.SplitN(v, ".", 3)
if len(parts) > 1 {
v = parts[0] + "." + parts[1]
}
return v
}