-
Notifications
You must be signed in to change notification settings - Fork 1
/
categories.go
88 lines (74 loc) · 2.16 KB
/
categories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package fatsecret
import (
"encoding/json"
"errors"
)
type FoodCategory struct {
ID string `json:"food_category_id"`
Name string `json:"food_category_name"`
Description string `json:"food_category_description"`
}
type FoodCategories struct {
Categories []FoodCategory `json:"food_category"`
}
type FoodCategoriesResponse struct {
Categories *FoodCategories `json:"food_categories,omitempty"`
Error *ErrorResponse `json:"error,omitempty"`
}
type FoodSubCategories struct {
SubCategories []string `json:"food_sub_category"`
}
type FoodSubCategoriesResponse struct {
SubCategories *FoodSubCategories `json:"food_sub_categories,omitempty"`
Error *ErrorResponse `json:"error,omitempty"`
}
// FoodCategories invokes the FatSecret 'food_categories.get' API call
// and returns the response as a slice of FoodCategory structs
func (c *Client) FoodCategories() ([]FoodCategory, error) {
// invoke the api call
body, err := c.InvokeAPI(
"food_categories.get",
map[string]string{},
)
if err != nil {
return nil, err
}
// parse the api response
resp := FoodCategoriesResponse{}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
// if an error response was returned
if resp.Error != nil {
// return the response error message
return nil, errors.New(resp.Error.Message)
}
// return the slice of food category entries
return resp.Categories.Categories, nil
}
// FoodSubCategories invokes the FatSecret 'food_sub_categories.get'
// API call and returns a slice of sub-categories for a given category
func (c *Client) FoodSubCategories(id string) ([]string, error) {
// invoke the api call
body, err := c.InvokeAPI(
"food_sub_categories.get",
map[string]string{
"food_category_id": id,
},
)
if err != nil {
return nil, err
}
// parse the api response
resp := FoodSubCategoriesResponse{}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
// if an error response was returned
if resp.Error != nil {
// return the response error message
return nil, errors.New(resp.Error.Message)
}
// return the slice of food sub-categories
return resp.SubCategories.SubCategories, nil
}