mirror of
https://github.com/holidayapi/holidayapi-go.git
synced 2025-06-21 04:26:33 +00:00
parent
6556bc0cb8
commit
9c712ca63c
3 changed files with 33 additions and 11 deletions
|
@ -2,6 +2,8 @@ package holidayapi
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/simplereach/timeutils"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -21,8 +23,27 @@ func NewV1(key string) *V1 {
|
|||
return v1
|
||||
}
|
||||
|
||||
func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, error) {
|
||||
var data map[string]interface{}
|
||||
type Respone struct {
|
||||
Status int32 `json:"status"`
|
||||
Requests Request `json:"requests"`
|
||||
Holidays []Holiday `json:"holidays"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Used int32 `json:"used"`
|
||||
Available int32 `json:"available"`
|
||||
Resets timeutils.Time `json:"resets"`
|
||||
}
|
||||
|
||||
type Holiday struct {
|
||||
Name string `json:"name"`
|
||||
Date timeutils.Time `json:"date,omniempty"`
|
||||
Observed timeutils.Time `json:"observed"`
|
||||
Public bool `json:"public"`
|
||||
}
|
||||
|
||||
func (v1 *V1) Holidays(args map[string]string) (Respone, error) {
|
||||
var data Respone
|
||||
|
||||
if _, ok := args["key"]; !ok {
|
||||
args["key"] = v1.Key
|
||||
|
@ -31,13 +52,13 @@ func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, err
|
|||
params := url.Values{}
|
||||
|
||||
for k, v := range args {
|
||||
params.Add(k, v.(string))
|
||||
params.Add(k, v)
|
||||
}
|
||||
|
||||
resp, err := http.Get(v1.Url + params.Encode())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return Respone{}, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
@ -45,17 +66,16 @@ func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, err
|
|||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return Respone{}, err
|
||||
}
|
||||
|
||||
json.Unmarshal([]byte(string(body)), &data)
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
return Respone{}, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
_, ok := data["error"]
|
||||
|
||||
if !ok {
|
||||
data["error"] = "Unknown error."
|
||||
}
|
||||
return Respone{}, errors.New("Unknown error!")
|
||||
}
|
||||
|
||||
return data, nil
|
||||
|
|
1
holidayapi_test.go
Normal file
1
holidayapi_test.go
Normal file
|
@ -0,0 +1 @@
|
|||
package holidayapi
|
1
models.go
Normal file
1
models.go
Normal file
|
@ -0,0 +1 @@
|
|||
package holidayapi
|
Loading…
Add table
Add a link
Reference in a new issue