mirror of
https://github.com/Luzifer/go-holidays.git
synced 2024-12-25 13:21:17 +00:00
Allow filtering holidays by month and date
This commit is contained in:
parent
9fe048b5d0
commit
849af883e3
2 changed files with 21 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue