Skip to content

Commit

Permalink
Add aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy Fedorenko committed Jan 4, 2021
1 parent 57344e5 commit e14e9cd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- TZ names should be taken from [IANA](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List)

## TODO:
- Add examples
70 changes: 61 additions & 9 deletions isdayoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
38 changes: 38 additions & 0 deletions isdayoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit e14e9cd

Please sign in to comment.