From 849af883e33753b0f3edbebd1f01853c950cf73b Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 2 Feb 2017 11:17:58 +0100 Subject: [PATCH] Allow filtering holidays by month and date --- cmd/holiday-api/index.html | 9 +++++++-- cmd/holiday-api/main.go | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/holiday-api/index.html b/cmd/holiday-api/index.html index ee91d18..f1c6643 100644 --- a/cmd/holiday-api/index.html +++ b/cmd/holiday-api/index.html @@ -31,12 +31,17 @@ This instance of `holiday-api` is a convenient wrapper around my [go-holiday lib ## Usage -The supported URL scheme is `/{country-code}/{year}` while `{year}` is optional and if not set the current year is used. +The supported URL scheme is `/{country-code}/{year}/{month}/{day}`: +- `{country-code}` **(required)** - ISO 3166-1 alpha-2 country code +- `{year}` _(optional)_ - If not set the current year is used +- `{month}` _(optional)_ - If specified list is filtered for given month +- `{day}` _(optional)_ - If specified list is filtered for given day Examples: - [`/us`](/us) - Federal holidays for the United States for the current year - [`/de/2017`](/de/2017) - National holidays in Germany for 2017 - [`/de-by/2017`](/de-by/2017) - Holidays for Bavaria for 2017 (Uses [ISO 3166-2:DE](https://en.wikipedia.org/wiki/ISO_3166-2:DE) codes) +- [`/de-by/2017/10`](/de-by/2017/10) - Holidays for Bavaria for October 2017 No request limits are enforced so please play nice with the API. @@ -44,7 +49,7 @@ No request limits are enforced so please play nice with the API. The API will respond with - `HTTP 200` and a JSON array if the query was successful -- `HTTP 404` if your URL is nvalid. +- `HTTP 404` if your URL is invalid. - `HTTP 500` if the requested country-code is not included / supported ### JSON format diff --git a/cmd/holiday-api/main.go b/cmd/holiday-api/main.go index a7c4ae7..9f1f815 100644 --- a/cmd/holiday-api/main.go +++ b/cmd/holiday-api/main.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "strconv" + "strings" "time" holidays "github.com/Luzifer/go-holidays" @@ -38,6 +39,8 @@ func init() { func main() { r := mux.NewRouter() + r.HandleFunc("/{country-code:[a-z-]+}/{year:[0-9]{4}}/{month:[0-9]{2}}/{day:[0-9]{2}}", handleHolidays) + r.HandleFunc("/{country-code:[a-z-]+}/{year:[0-9]{4}}/{month:[0-9]{2}}", handleHolidays) r.HandleFunc("/{country-code:[a-z-]+}/{year:[0-9]{4}}", handleHolidays) r.HandleFunc("/{country-code:[a-z-]+}", handleHolidays) r.HandleFunc("/", handleReadme) @@ -60,14 +63,23 @@ func handleHolidays(res http.ResponseWriter, r *http.Request) { } } - holidays, err := holidays.GetHolidays(countryCode, year) + check := strings.TrimRight(strings.Join([]string{strconv.Itoa(year), vars["month"], vars["day"]}, "-"), "-") + + days, err := holidays.GetHolidays(countryCode, year) if err != nil { http.Error(res, "An error ocurred: "+err.Error(), http.StatusInternalServerError) return } + outputSet := []holidays.Holiday{} + for _, h := range days { + if strings.HasPrefix(h.Date, check) { + outputSet = append(outputSet, h) + } + } + res.Header().Set("Content-Type", "application/json") - json.NewEncoder(res).Encode(holidays) + json.NewEncoder(res).Encode(outputSet) } func handleReadme(res http.ResponseWriter, r *http.Request) {