/
api.go
308 lines (274 loc) · 9.46 KB
/
api.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package seattlefoodtruck
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
ws "github.com/appsbyram/pkg/http"
l "github.com/appsbyram/pkg/logging"
"go.uber.org/zap"
)
const (
//Today for today
Today = "today"
//Tomorrow for tomorrow
Tomorrow = "tomorrow"
//EventsResourcePath represents path to retrieve a collection of event resources
EventsResourcePath = "events"
//LocationResourcePath represents path to retrieve a location resource
LocationResourcePath = "locations/%s"
//TruckResourcePath represents path to retrieve truck
TruckResourcePath = "trucks/%s"
)
//FoodTruckClient represents generic interface for Seattle FoodTruck API client
type FoodTruckClient interface {
GetEvents(id string, onDay string) ([]Event, error)
GetLocation(id string) (Location, error)
GetTruck(id string) (Truck, error)
}
type foodTruckClient struct {
host string
scheme string
basePath string
client *http.Client
logger *zap.SugaredLogger
}
//NewFoodTruckClient returns a new instance of Food Truck Client
func NewFoodTruckClient(ctx context.Context, host, scheme, basePath string) FoodTruckClient {
logger := l.LoggerFromContext(ctx)
return &foodTruckClient{
host: host,
scheme: scheme,
basePath: basePath,
client: http.DefaultClient,
logger: logger,
}
}
func (c *foodTruckClient) GetEvents(id string, on string) ([]Event, error) {
var onDay string
var evr EventsResponse
if len(id) == 0 {
return nil, errors.New("Location ID is missing")
}
switch on {
case Tomorrow:
onDay = time.Now().AddDate(0, 0, 1).Format("2006-01-02")
break
default:
onDay = time.Now().Format("2006-01-02")
break
}
qs := map[string]string{
"include_bookings": "true",
"with_active_trucks": "true",
"with_booking_status": "approved",
"on_day": onDay,
"for_locations": id,
}
endpoint := fmt.Sprintf("%s://%s%s/%s", c.scheme, c.host, c.basePath, EventsResourcePath)
c.logger.Infof("Endpoint: %s", endpoint)
callAPI(endpoint, qs, c.client, &evr)
return evr.Events, nil
}
func (c *foodTruckClient) GetLocation(id string) (Location, error) {
var l Location
if len(id) == 0 {
return l, errors.New("Location ID is missing")
}
endpoint := fmt.Sprintf("%s://%s%s/%s", c.scheme, c.host, c.basePath, fmt.Sprintf(LocationResourcePath, id))
c.logger.Infof("Endpoint: %s", endpoint)
callAPI(endpoint, nil, c.client, &l)
return l, nil
}
func (c *foodTruckClient) GetTruck(id string) (Truck, error) {
var t Truck
if len(id) == 0 {
return t, errors.New("Truck ID is required")
}
endpoint := fmt.Sprintf("%s://%s%s/%s", c.scheme, c.host, c.basePath, fmt.Sprintf(TruckResourcePath, id))
c.logger.Infof("Endpoint: %s", endpoint)
callAPI(endpoint, nil, c.client, &t)
return t, nil
}
func callAPI(endPoint string, qs map[string]string, client *http.Client, data interface{}) error {
url, err := url.Parse(endPoint)
if err != nil {
return err
}
if qs != nil {
query := url.Query()
for k, v := range qs {
query.Add(k, v)
}
//encode and add to url
url.RawQuery = query.Encode()
}
//setup request
req, err := http.NewRequest(http.MethodGet, url.String(), nil)
if err != nil {
return err
}
//call api
resp, err := client.Do(req)
if err != nil {
return err
}
p := ws.NewPayload()
p.ReadResponse(ws.ContentTypeJSON, &data, resp)
return nil
}
//EventsResponse is response from events api
type EventsResponse struct {
Pagination struct {
Page int `json:"page"`
TotalPages int `json:"total_pages"`
TotalCount int `json:"total_count"`
} `json:"pagination"`
Events []Event `json:"events"`
}
//Event represent an event
type Event struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
EventID int `json:"event_id"`
Bookings []struct {
ID int `json:"id"`
Status string `json:"status"`
Paid bool `json:"paid"`
Truck struct {
Name string `json:"name"`
Trailer bool `json:"trailer"`
FoodCategories []string `json:"food_categories"`
ID string `json:"id"`
UID int `json:"uid"`
FeaturedPhoto string `json:"featured_photo"`
} `json:"truck"`
} `json:"bookings"`
WaitlistEntries []struct {
ID int `json:"id"`
Expiration interface{} `json:"expiration"`
Position int `json:"position"`
Truck struct {
Slug string `json:"slug"`
} `json:"truck"`
} `json:"waitlist_entries"`
}
//Location represents a location where you can find truck
type Location struct {
Name string `json:"name"`
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
Address string `json:"address"`
Photo string `json:"photo"`
GooglePlaceID string `json:"google_place_id"`
CreatedAt string `json:"created_at"`
NeighborhoodID int `json:"neighborhood_id"`
Slug string `json:"slug"`
FilteredAddress string `json:"filtered_address"`
ID string `json:"id"`
UID int `json:"uid"`
Neighborhood struct {
Name string `json:"name"`
ID int `json:"id"`
} `json:"neighborhood"`
Pod struct {
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
LoadInSheet string `json:"load_in_sheet"`
W9Required bool `json:"w9_required"`
CoiRequired bool `json:"coi_required"`
HealthRequired bool `json:"health_required"`
HealthSnohomishRequired interface{} `json:"health_snohomish_required"`
} `json:"pod"`
}
//Truck represents a food truck
type Truck struct {
Name string `json:"name"`
Rating float64 `json:"rating"`
UserID int `json:"user_id"`
Featured bool `json:"featured"`
RatingCount int `json:"rating_count"`
ID string `json:"id"`
UID int `json:"uid"`
FeaturedPhoto string `json:"featured_photo"`
Facebook string `json:"facebook"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Twitter string `json:"twitter"`
Instagram string `json:"instagram"`
Yelp string `json:"yelp"`
Description string `json:"description"`
Phone string `json:"phone"`
Email string `json:"email"`
Website string `json:"website"`
Active bool `json:"active"`
ContactName string `json:"contact_name"`
TruckLength int `json:"truck_length"`
TruckWidth int `json:"truck_width"`
Trailer bool `json:"trailer"`
AcceptsCreditCards bool `json:"accepts_credit_cards"`
GlutenFree bool `json:"gluten_free"`
Vegetarian bool `json:"vegetarian"`
Vegan bool `json:"vegan"`
Paleo bool `json:"paleo"`
FutureBookings int `json:"future_bookings"`
FuturePodEvents int `json:"future_pod_events"`
Coi string `json:"coi"`
CoiExpiration string `json:"coi_expiration"`
CoiStatus string `json:"coi_status"`
CoiApproved bool `json:"coi_approved"`
Health string `json:"health"`
HealthExpiration string `json:"health_expiration"`
HealthStatus string `json:"health_status"`
HealthApproved bool `json:"health_approved"`
W9 string `json:"w9"`
W9Expiration interface{} `json:"w9_expiration"`
W9Status string `json:"w9_status"`
W9Approved bool `json:"w9_approved"`
HealthSnohomish string `json:"health_snohomish"`
HealthSnohomishExpiration string `json:"health_snohomish_expiration"`
HealthSnohomishStatus string `json:"health_snohomish_status"`
HealthSnohomishApproved bool `json:"health_snohomish_approved"`
MenuItems []struct {
Name string `json:"name"`
Description string `json:"description"`
Price float64 `json:"price"`
ID int `json:"id"`
} `json:"menu_items"`
Photos []struct {
ID int `json:"id"`
File string `json:"file"`
Position int `json:"position"`
} `json:"photos"`
RelatedTrucks []struct {
Name string `json:"name"`
Rating float64 `json:"rating"`
RatingCount int `json:"rating_count"`
ID string `json:"id"`
FeaturedPhoto string `json:"featured_photo"`
FoodCategories []struct {
Name string `json:"name"`
ID string `json:"id"`
UID int `json:"uid"`
} `json:"food_categories"`
} `json:"related_trucks"`
FoodCategories []struct {
Name string `json:"name"`
ID string `json:"id"`
UID int `json:"uid"`
} `json:"food_categories"`
}
func trimSpaceAndLower(s string) string {
r := strings.TrimSpace(s)
r = strings.ToLower(r)
return r
}