diff --git a/README.md b/README.md index a9a2dd5..ba2acca 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,7 @@ go get -u github.com/anatoliyfedorenko/isdayoff ``` ## Note: -- TZ names should be taken from [IANA](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List) \ No newline at end of file +- TZ names should be taken from [IANA](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List) + +## TODO: +- Add examples \ No newline at end of file diff --git a/isdayoff.go b/isdayoff.go index 73810ef..db8ea00 100644 --- a/isdayoff.go +++ b/isdayoff.go @@ -26,8 +26,7 @@ func NewWithClient(client *http.Client) *Client { // IsLeap checks if year is leap func (c *Client) IsLeap(year int) (bool, error) { url := fmt.Sprintf("https://isdayoff.ru/api/isleap?year=%d", year) - method := "GET" - req, err := http.NewRequest(method, url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return false, fmt.Errorf("http.NewRequest failed: %v", err) } @@ -50,9 +49,9 @@ func (c *Client) IsLeap(year int) (bool, error) { return YearType(string(body)) == YearTypeLeap, nil } -var boolToInt = map[bool]int{ - false: 0, - true: 1, +var boolToStr = map[bool]string{ + false: "0", + true: "1", } // Params contains various filters for request @@ -88,16 +87,15 @@ func (c *Client) GetBy(params Params) ([]DayType, error) { url += fmt.Sprintf("&cc=%v", *params.CountryCode) } if params.Pre != nil { - url += fmt.Sprintf("&pre=%d", boolToInt[*params.Pre]) + url += fmt.Sprintf("&pre=%s", boolToStr[*params.Pre]) } if params.Covid != nil { - url += fmt.Sprintf("&covid=%d", boolToInt[*params.Covid]) + url += fmt.Sprintf("&covid=%s", boolToStr[*params.Covid]) } if params.TZ != nil { url += fmt.Sprintf("&tz=%s", *params.TZ) } - method := "GET" - req, err := http.NewRequest(method, url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, fmt.Errorf("http.NewRequest failed: %v", err) } @@ -127,3 +125,57 @@ func (c *Client) GetBy(params Params) ([]DayType, error) { return result, nil } + +// Today get data for today by particular params +func (c *Client) Today(params Params) (*DayType, error) { + return c.aliasRequest("today", params) +} + +// Tomorrow get data for tomorrow by particular params +func (c *Client) Tomorrow(params Params) (*DayType, error) { + return c.aliasRequest("tomorrow", params) +} + +func (c *Client) aliasRequest(alias string, params Params) (*DayType, error) { + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://isdayoff.ru/%s", alias), nil) + if err != nil { + return nil, fmt.Errorf("http.NewRequest failed: %v", err) + } + + q := req.URL.Query() + if params.CountryCode != nil { + q.Add("cc", string(*params.CountryCode)) + } + if params.Pre != nil { + q.Add("pre ", boolToStr[*params.Pre]) + } + if params.Covid != nil { + q.Add("covid", boolToStr[*params.Covid]) + } + if params.TZ != nil { + q.Add("tz", string(*params.TZ)) + } + + req.URL.RawQuery = q.Encode() + + req.Header.Set("User-Agent", "isdayoff-golang-lib/1.0.0 (https://github.com/anatoliyfedorenko)") + + res, err := c.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("client.Do(req) failed: %v", err) + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, fmt.Errorf("ioutil.ReadAll failed: %v", err) + } + + if res.StatusCode != http.StatusOK { + return nil, fmt.Errorf(string(body)) + } + + result := DayType(body) + + return &result, nil +} diff --git a/isdayoff_test.go b/isdayoff_test.go index 9bf11d9..3ef3a82 100644 --- a/isdayoff_test.go +++ b/isdayoff_test.go @@ -49,3 +49,41 @@ func TestGetByDay(t *testing.T) { t.Errorf("should be 1, equal: %v", len(days)) } } + +func TestToday(t *testing.T) { + client := New() + countryCode := CountryCodeKazakhstan + pre := false + covid := false + day, err := client.Today(Params{ + CountryCode: &countryCode, + Pre: &pre, + Covid: &covid, + }) + if err != nil { + t.Error(err) + } + // This is a dynamicly set parameter, so it can vary from day to day. + if *day != DayTypeNonWorking { + t.Error("should be non working") + } +} + +func TestTomorrow(t *testing.T) { + client := New() + countryCode := CountryCodeKazakhstan + pre := false + covid := false + day, err := client.Tomorrow(Params{ + CountryCode: &countryCode, + Pre: &pre, + Covid: &covid, + }) + if err != nil { + t.Error(err) + } + // This is a dynamicly set parameter, so it can vary from day to day. + if *day != DayTypeWorking { + t.Errorf("should be %v, instead: %v", DayTypeWorking, *day) + } +}