-
Notifications
You must be signed in to change notification settings - Fork 86
/
data_source_menu.go
110 lines (104 loc) · 2.74 KB
/
data_source_menu.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
package main
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"net/http"
"sort"
"strconv"
"strings"
"time"
)
func dataSourceMenu() *schema.Resource {
return &schema.Resource{
Read: resourceMenuRead,
Schema: map[string]*schema.Schema{
"store_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"menu": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"code": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"price_cents": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}
func resourceMenuRead(d *schema.ResourceData, m interface{}) error {
var client = &http.Client{Timeout: 10 * time.Second}
store_id := d.Get("store_id").(string)
menuitems, err := getAllMenuItems(fmt.Sprintf("https://order.dominos.com/power/store/%s/menu?lang=en&structured=true", store_id), client)
if err != nil {
return err
}
menu := make([]map[string]interface{}, 0, len(menuitems))
for i := range menuitems {
menu = append(menu, map[string]interface{}{"name": menuitems[i].Name, "code": menuitems[i].Code, "price_cents": menuitems[i].PriceCents})
}
if err := d.Set("menu", menu); err != nil {
return err
}
log.Printf("len menu: %d", len(menu))
d.SetId(store_id)
return nil
}
type MenuItem struct {
Code string
Name string
PriceCents int64
}
func getMenuApiObject(url string, client *http.Client) (map[string]interface{}, error) {
r, err := client.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
resp := make(map[string]interface{})
err = json.NewDecoder(r.Body).Decode(&resp)
return resp, err
}
func getAllMenuItems(url string, client *http.Client) ([]MenuItem, error) {
resp, err := getMenuApiObject(url, client)
if err != nil {
return nil, err
}
products := resp["Variants"].(map[string]interface{})
all_products := make([]MenuItem, 0, len(products))
log.Printf("len products: %d", len(products))
for name, d := range products {
dict := d.(map[string]interface{})
price := dict["Price"].(string)
price = strings.Replace(price, ".", "", 1)
price_cents, err := strconv.ParseInt(price, 10, 64)
if err != nil {
continue
}
all_products = append(all_products, MenuItem{
Code: name,
Name: dict["Name"].(string),
PriceCents: price_cents,
})
}
sort.Slice(all_products, func(i, j int) bool {
return all_products[i].Code < all_products[j].Code
})
// for each entry in Products, make a MenuItem struct and return it.
return all_products, nil
}