forked from lgrees/resy-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
72 lines (61 loc) · 1.65 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
package schedule
import (
"encoding/json"
"fmt"
"strconv"
"github.com/fanniva/resy-cli/internal/utils/http"
)
type searchRequest struct {
Query string `json:"query"`
PerPage int64 `json:"per_page"`
Types []string `json:"types"`
}
type searchResponse struct {
Search struct {
Hits []struct {
Locality string `json:"locality"`
Rating struct {
Average float32 `json:"average"`
Count int64 `json:"count"`
} `json:"rating"`
Id struct {
Resy int64 `json:"resy"`
}
Name string `json:"name"`
Neighborhood string `json:"neighborhood"`
Cuisine []string `json:"cuisine"`
} `json:"hits"`
} `json:"search"`
}
func searchVenues(query string) (*[]surveyVenue, error) {
searchRequest := searchRequest{
Query: query,
PerPage: 600,
Types: []string{"venue"},
}
body, err := json.Marshal(searchRequest)
if err != nil {
return nil, err
}
responseBody, statusCode, err := http.PostJSON("https://api.resy.com/3/venuesearch/search", &http.Req{Body: body})
if err != nil {
return nil, err
}
if statusCode >= 400 || responseBody == nil {
return nil, fmt.Errorf("failed to get search results, status code: %d", statusCode)
}
var res searchResponse
_ = json.Unmarshal(responseBody, &res)
ret := make([]surveyVenue, 0)
for _, s := range res.Search.Hits {
v := surveyVenue{
Id: strconv.Itoa(int(s.Id.Resy)),
Name: s.Name,
Rating: fmt.Sprintf("%s (%d reviews)", strconv.FormatFloat(float64(s.Rating.Average), 'f', 2, 64), s.Rating.Count),
Location: fmt.Sprintf("%s, %s", s.Neighborhood, s.Locality),
Cuisine: s.Cuisine[0],
}
ret = append(ret, v)
}
return &ret, nil
}