-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanteen.go
76 lines (58 loc) · 1.55 KB
/
canteen.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
package openmensa
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
)
// A Canteen object
type Canteen struct {
ID int `json:"id"`
Name string `json:"name"`
City string `json:"city"`
Address string `json:"address"`
Coordinate *Coordinate `json:"coordinates"`
}
func (c Canteen) String() string {
return fmt.Sprintf("%s", c.Name)
}
// GetAllCanteens returns a list of all known canteens
// Since OpenMensa uses pagination for the results, this will result in multiple requests.
func GetAllCanteens() (canteens []*Canteen, err error) {
// FIXME: Concurrency anyone?
url, _ := url.Parse(BaseURL + "canteens")
page := 1
for {
params := url.Query()
params.Set("page", strconv.Itoa(page))
url.RawQuery = params.Encode()
resp, err := get(url.String())
var currentCanteens []*Canteen
err = json.Unmarshal(resp, ¤tCanteens)
if err != nil {
return nil, err
}
if len(currentCanteens) == 0 {
// Reached the last page
break
}
canteens = append(canteens, currentCanteens...)
page++
}
return
}
// GetCanteens returns a list of canteen objects for the given ids
func GetCanteens(ids ...int) (canteens []*Canteen, err error) {
url, _ := url.Parse(fmt.Sprintf("%s/canteens", BaseURL))
stringIDs := []string{}
for _, id := range ids {
stringIDs = append(stringIDs, strconv.Itoa(id))
}
params := url.Query()
params.Set("ids", strings.Join(stringIDs, ","))
url.RawQuery = params.Encode()
resp, err := get(url.String())
err = json.Unmarshal(resp, &canteens)
return
}