add structs (#2)

* fix: added models
This commit is contained in:
gelleson 2019-06-02 00:49:30 +06:00 committed by Josh Sherman
parent 6556bc0cb8
commit 9c712ca63c
3 changed files with 33 additions and 11 deletions

View file

@ -2,6 +2,8 @@ package holidayapi
import ( import (
"encoding/json" "encoding/json"
"github.com/pkg/errors"
"github.com/simplereach/timeutils"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -21,8 +23,27 @@ func NewV1(key string) *V1 {
return v1 return v1
} }
func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, error) { type Respone struct {
var data map[string]interface{} 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 { if _, ok := args["key"]; !ok {
args["key"] = v1.Key args["key"] = v1.Key
@ -31,13 +52,13 @@ func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, err
params := url.Values{} params := url.Values{}
for k, v := range args { for k, v := range args {
params.Add(k, v.(string)) params.Add(k, v)
} }
resp, err := http.Get(v1.Url + params.Encode()) resp, err := http.Get(v1.Url + params.Encode())
if err != nil { if err != nil {
return nil, err return Respone{}, err
} }
defer resp.Body.Close() 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) body, err := ioutil.ReadAll(resp.Body)
if err != nil { 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 { if resp.StatusCode != 200 {
_, ok := data["error"] return Respone{}, errors.New("Unknown error!")
if !ok {
data["error"] = "Unknown error."
}
} }
return data, nil return data, nil

1
holidayapi_test.go Normal file
View file

@ -0,0 +1 @@
package holidayapi

1
models.go Normal file
View file

@ -0,0 +1 @@
package holidayapi