From 5058a30bc25fb5859d63ece2c0a874c445bc7304 Mon Sep 17 00:00:00 2001 From: Alexys Lozada Date: Wed, 16 Aug 2017 14:44:54 -0500 Subject: [PATCH] Create a ResponseHoliday struct For a better control over the returned data, a structure is created that maps the response. --- .gitignore | 2 ++ holidayapi.go | 14 ++++++-------- model.go | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 model.go diff --git a/.gitignore b/.gitignore index daf913b..3464e8a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Folders _obj _test +.idea # Architecture specific extensions/prefixes *.[568vq] @@ -22,3 +23,4 @@ _testmain.go *.exe *.test *.prof +*.out \ No newline at end of file diff --git a/holidayapi.go b/holidayapi.go index 4230179..20b7fd0 100644 --- a/holidayapi.go +++ b/holidayapi.go @@ -21,8 +21,8 @@ func NewV1(key string) *V1 { return v1 } -func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, error) { - var data map[string]interface{} +func (v1 *V1) Holidays(args map[string]interface{}) (ResponseHoliday, error) { + var data ResponseHoliday if _, ok := args["key"]; !ok { 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()) if err != nil { - return nil, err + return data, err } 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) if err != nil { - return nil, err + return data, err } json.Unmarshal([]byte(string(body)), &data) if resp.StatusCode != 200 { - _, ok := data["error"] - - if !ok { - data["error"] = "Unknown error." + if data.Error == "" { + data.Error = "Unknown error." } } diff --git a/model.go b/model.go new file mode 100644 index 0000000..895a6e5 --- /dev/null +++ b/model.go @@ -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"` +}