@@ 36,8 36,8 @@ func decodeRespose(resp *http.Response)
}
// Given an API Http Response, read it, parse the json and
-// returns a map of Object{ID: string, Name: string}
-func parseJSON(resp *http.Response, errMsgs []string) (map[string]Object, error) {
+// returns a map with the json data
+func parseJSON(resp *http.Response, errMsgs []string) (map[string]any, error) {
out, err := decodeRespose(resp)
if err != nil {
return nil, err
@@ 47,7 47,7 @@ func parseJSON(resp *http.Response, errM
return nil, fmt.Errorf("%w: %s", ErrAPIResponse, out)
}
}
- var objects map[string]Object
+ var objects map[string]any
err = json.Unmarshal([]byte(out), &objects)
if err != nil {
return nil, err
@@ 55,8 55,14 @@ func parseJSON(resp *http.Response, errM
return objects, nil
}
-// Object is a generic representation for Brand and List
-type Object struct {
+// List ...
+type List struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+}
+
+// Brand ...
+type Brand struct {
ID string `json:"id"`
Name string `json:"name"`
}
@@ 246,10 252,9 @@ func (c *Client) CreateCampaign(p Create
return "", fmt.Errorf("%w: %s", ErrAPIResponse, out)
}
-// GetBrands endpoint. If succeed returns a map of Object of the form
-// map["brand1"]Object{ID: string, Name: string}
+// GetBrands endpoint. If succeed returns a slice of Brand
// Otherwhise it returns an error
-func (c *Client) GetBrands() (map[string]Object, error) {
+func (c *Client) GetBrands() ([]Brand, error) {
resp, err := c.Call(getBrandsEndPoint, url.Values{}, []string{})
if err != nil {
return nil, err
@@ 259,18 264,26 @@ func (c *Client) GetBrands() (map[string
"Invalid API key", "No brands found",
}
- return parseJSON(resp, errMsgs)
+ jsonMap, err := parseJSON(resp, errMsgs)
+ if err != nil {
+ return nil, err
+ }
+ var brands []Brand
+ for _, v := range jsonMap {
+ value := v.(map[string]interface{})
+ brands = append(brands, Brand{ID: value["id"].(string), Name: value["name"].(string)})
+ }
+ return brands, err
}
-// GetLists endpoint. If succeed returns a map of Object of the form
-// map["brand1"]Object{ID: string, Name: string}
+// GetLists endpoint. If succeed returns an slice of List
// Otherwhise it returns an error
//
// Params
// BrandID: the id of the brand you want to get the list of lists from.
// IncludeHidden (optional): if you want to retrieve lists that are
// hidden as well, set this to yes. Default is no.
-func (c *Client) GetLists(p GetListsParams) (map[string]Object, error) {
+func (c *Client) GetLists(p GetListsParams) ([]List, error) {
resp, err := c.Call(getListEndPoint, p.buildParams(), []string{"brand_id"})
if err != nil {
return nil, err
@@ 281,5 294,14 @@ func (c *Client) GetLists(p GetListsPara
"Invalid API key", "Brand ID not passed",
"Brand does not exist", "No lists found",
}
- return parseJSON(resp, errMsgs)
+ jsonMap, err := parseJSON(resp, errMsgs)
+ if err != nil {
+ return nil, err
+ }
+ var lists []List
+ for _, v := range jsonMap {
+ value := v.(map[string]interface{})
+ lists = append(lists, List{ID: value["id"].(string), Name: value["name"].(string)})
+ }
+ return lists, err
}
@@ 272,16 272,16 @@ func TestGetBrands(t *testing.T) {
}
client := sendy.NewClient("", "", false)
client.SetClient(MockClient{})
- outMap, err := client.GetBrands()
+ out, err := client.GetBrands()
if err != nil {
t.Errorf("Unexpected err %v", err)
}
- if _, ok := outMap["brand1"]; !ok {
- t.Errorf("brand1 is expected in the parsed map")
+ if len(out) != 2 {
+ t.Errorf("out must be a two elements slice")
}
- if outMap["brand1"].ID != "4" || outMap["brand1"].Name != "Netlandish Inc." {
+ if out[0].ID != "4" || out[0].Name != "Netlandish Inc." {
t.Errorf("A Brand struct with ID: \"4\" and Name: \"Netlandish Inc.\" is expected")
}
@@ 308,16 308,16 @@ func TestGetLists(t *testing.T) {
}
client := sendy.NewClient("", "", false)
client.SetClient(MockClient{})
- outMap, err := client.GetLists(sendy.GetListsParams{BrandID: "12"})
+ out, err := client.GetLists(sendy.GetListsParams{BrandID: "12"})
if err != nil {
t.Errorf("Unexpected err %v", err)
}
- if _, ok := outMap["list1"]; !ok {
- t.Errorf("list1 is expected in the parsed map")
+ if len(out) != 1 {
+ t.Errorf("out must be a single element slice")
}
- if outMap["list1"].ID != "xyz" || outMap["list1"].Name != "Netlandish Inc." {
+ if out[0].ID != "xyz" || out[0].Name != "Netlandish Inc." {
t.Errorf("A List struct with ID: \"xyz\" and Name: \"Netlandish Inc.\" is expected")
}