MCPcopy
hub / github.com/usefathom/fathom / GetRequestParams

Function GetRequestParams

pkg/api/params.go:21–63  ·  view source on GitHub ↗

GetRequestParams parses the query parameters and returns commonly used API parameters, with defaults

(r *http.Request)

Source from the content-addressed store, hash-verified

19
20// GetRequestParams parses the query parameters and returns commonly used API parameters, with defaults
21func GetRequestParams(r *http.Request) *Params {
22 params := &Params{
23 SiteID: 0,
24 Limit: 20,
25 Offset: 0,
26 StartDate: time.Now(),
27 EndDate: time.Now().AddDate(0, 0, -7),
28 }
29
30 vars := mux.Vars(r)
31 if _, ok := vars["id"]; ok {
32 if siteID, err := strconv.ParseInt(vars["id"], 10, 64); err == nil {
33 params.SiteID = siteID
34 }
35 }
36
37 q := r.URL.Query()
38 if q.Get("after") != "" {
39 if after, err := strconv.ParseInt(q.Get("after"), 10, 64); err == nil && after > 0 {
40 params.StartDate = time.Unix(after, 0)
41 }
42 }
43
44 if q.Get("before") != "" {
45 if before, err := strconv.ParseInt(q.Get("before"), 10, 64); err == nil && before > 0 {
46 params.EndDate = time.Unix(before, 0)
47 }
48 }
49
50 if q.Get("limit") != "" {
51 if limit, err := strconv.Atoi(q.Get("limit")); err == nil && limit > 0 {
52 params.Limit = limit
53 }
54 }
55
56 if q.Get("offset") != "" {
57 if offset, err := strconv.Atoi(q.Get("offset")); err == nil && offset > 0 {
58 params.Offset = offset
59 }
60 }
61
62 return params
63}

Calls

no outgoing calls

Tested by 1

TestGetRequestParamsFunction · 0.68