-
Notifications
You must be signed in to change notification settings - Fork 1
/
fatsecret.go
191 lines (176 loc) · 5.08 KB
/
fatsecret.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package fatsecret_go
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// clientID, clientSecret
// return token
var ClientID string
var ClientSecret string
var AuthToken string
var Regional string
var Languages string
var Scope string
func InitFatSecret(clientID, clientSecret, Region, Language, scope string) {
var err error
ClientID = clientID
ClientSecret = clientSecret
Regional = Region
Languages = Language
Scope = scope
AuthToken, err = GetFatSecretAuthorization(clientID, clientSecret, scope)
if err != nil {
fmt.Println(err)
}
go refreshToken()
}
func GetFatSecretAuthorization(clientID, clientSecret, scope string) (string, error) {
endPoint := "https://oauth.fatsecret.com/connect/token"
data := url.Values{}
data.Set("client_id", clientID)
data.Set("client_secret", clientSecret)
data.Set("grant_type", "client_credentials")
data.Set("scope", scope)
data.Set("json", "true")
req, err := http.NewRequest("POST", endPoint, strings.NewReader(data.Encode()))
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
resultMap := map[string]interface{}{}
err = json.Unmarshal(body, &resultMap)
if err != nil {
return "", err
}
return resultMap["access_token"].(string), err
}
func refreshToken() {
ticker := time.NewTicker(time.Hour * 23)
failedCount := 0
for {
select {
case <-ticker.C:
var err error
AuthToken, err = GetFatSecretAuthorization(ClientID, ClientSecret, Scope)
if err != nil {
failedCount++
continue
}
if failedCount > 5 {
failedCount = 0
}
}
}
}
func FoodGetV2(foodId string) (map[string]interface{}, error) {
resultMap := map[string]interface{}{}
endPoint := fmt.Sprintf("https://platform.fatsecret.com/rest/server.api?method=food.get.v2&food_id=%s&format=json®ion=%s",
foodId, Regional)
data := url.Values{}
req, err := http.NewRequest("POST", endPoint, strings.NewReader(data.Encode()))
if err != nil {
return resultMap, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", AuthToken))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return resultMap, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return resultMap, err
}
err = json.Unmarshal(body, &resultMap)
return resultMap, err
}
func FoodCategoriesGet() (map[string]interface{}, error) {
resultMap := map[string]interface{}{}
endPoint := fmt.Sprintf("https://platform.fatsecret.com/rest/server.api?method=food_categories.get&format=json®ion=%s", Regional)
data := url.Values{}
req, err := http.NewRequest("POST", endPoint, strings.NewReader(data.Encode()))
if err != nil {
return resultMap, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", AuthToken))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return resultMap, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return resultMap, err
}
err = json.Unmarshal(body, &resultMap)
return resultMap, err
}
func FoodSubCategoriesGet(categoryId string) (map[string]interface{}, error) {
resultMap := map[string]interface{}{}
method := "food_sub_categories.get"
endPoint := fmt.Sprintf("https://platform.fatsecret.com/rest/server.api?method=%s&food_category_id=%s&format=json®ion=%s", method, categoryId, Regional)
data := url.Values{}
req, err := http.NewRequest("POST", endPoint, strings.NewReader(data.Encode()))
if err != nil {
return resultMap, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", AuthToken))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return resultMap, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return resultMap, err
}
err = json.Unmarshal(body, &resultMap)
return resultMap, err
}
func FoodsSearch(keywords string, page, offset int) (map[string]interface{}, error) {
resultMap := map[string]interface{}{}
method := "foods.search"
endPoint := fmt.Sprintf("https://platform.fatsecret.com/rest/server.api?method=%s&search_expression=%s&page_number=%d&max_results=%d&format=json®ion=KR", method, keywords, page, offset)
data := url.Values{}
req, err := http.NewRequest("POST", endPoint, strings.NewReader(data.Encode()))
if err != nil {
return resultMap, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", AuthToken))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return resultMap, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return resultMap, err
}
err = json.Unmarshal(body, &resultMap)
return resultMap, err
}