From 70e3bd6a7e9d76dcb16cc587ddbcf184c0360fd7 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 7 Nov 2024 18:59:30 -0500 Subject: server: move parseQuery() to humidity.go --- server/humidity.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'server/humidity.go') diff --git a/server/humidity.go b/server/humidity.go index 079c9bf..3982545 100644 --- a/server/humidity.go +++ b/server/humidity.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "net/url" "strconv" ) @@ -93,3 +94,23 @@ func (h HumidityHandler) average() (Humidity, bool) { } return sum / Humidity(nRooms), true } + +// Parse the value associated with each key in the query string. Returns a map of +// keys and values, or error if one of the keys is missing or if there is no value +// associated with one of the keys. +func parseQuery(query string, keys []string) (map[string]string, error) { + queryVals, err := url.ParseQuery(query) + if err != nil { + return nil, err + } + + vals := make(map[string]string) + for _, key := range keys { + val := queryVals.Get(key) + if val == "" { + return nil, fmt.Errorf("missing key '%s'", key) + } + vals[key] = val + } + return vals, nil +} -- cgit v1.2.3