mirror of
https://github.com/holidayapi/holidayapi-go.git
synced 2025-06-21 12:36:33 +00:00
Create a ResponseHoliday struct
For a better control over the returned data, a structure is created that maps the response.
This commit is contained in:
parent
6556bc0cb8
commit
5058a30bc2
3 changed files with 22 additions and 8 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -6,6 +6,7 @@
|
||||||
# Folders
|
# Folders
|
||||||
_obj
|
_obj
|
||||||
_test
|
_test
|
||||||
|
.idea
|
||||||
|
|
||||||
# Architecture specific extensions/prefixes
|
# Architecture specific extensions/prefixes
|
||||||
*.[568vq]
|
*.[568vq]
|
||||||
|
@ -22,3 +23,4 @@ _testmain.go
|
||||||
*.exe
|
*.exe
|
||||||
*.test
|
*.test
|
||||||
*.prof
|
*.prof
|
||||||
|
*.out
|
|
@ -21,8 +21,8 @@ func NewV1(key string) *V1 {
|
||||||
return v1
|
return v1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, error) {
|
func (v1 *V1) Holidays(args map[string]interface{}) (ResponseHoliday, error) {
|
||||||
var data map[string]interface{}
|
var data ResponseHoliday
|
||||||
|
|
||||||
if _, ok := args["key"]; !ok {
|
if _, ok := args["key"]; !ok {
|
||||||
args["key"] = v1.Key
|
args["key"] = v1.Key
|
||||||
|
@ -37,7 +37,7 @@ func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, err
|
||||||
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 data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
@ -45,16 +45,14 @@ 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 data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
json.Unmarshal([]byte(string(body)), &data)
|
json.Unmarshal([]byte(string(body)), &data)
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
_, ok := data["error"]
|
if data.Error == "" {
|
||||||
|
data.Error = "Unknown error."
|
||||||
if !ok {
|
|
||||||
data["error"] = "Unknown error."
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
model.go
Normal file
14
model.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package holidayapi
|
||||||
|
|
||||||
|
type Holiday struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Date string `json:"date"`
|
||||||
|
Observed string `json:"observed"`
|
||||||
|
Public string `json:"public"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResponseHoliday struct {
|
||||||
|
Status int `json:"status"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Holidays map[string][]Holiday `json:"holidays"`
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue