-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
97 lines (85 loc) · 2.12 KB
/
client.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
package shopgun
import (
"fmt"
"log"
"net/http"
)
// Client is a shopgun client
type Client struct {
basePath string
c *http.Client
apiKey string
secret string
sr SessionResponse
Log log.Logger
}
// Pagination is what it says
type Pagination struct {
Offset uint64
Limit uint64
}
// DefaultPagination returns a default pagination setting
func DefaultPagination() Pagination {
return Pagination{Limit: 24, Offset: 0}
}
// New returns a new shopgun client
func New(apiKey, secret string) *Client {
if apiKey == "" {
panic("Api key not set")
}
if secret == "" {
panic("Secret not set")
}
client := &http.Client{}
return &Client{
basePath: "https://api.etilbudsavis.dk/v2",
c: client,
apiKey: apiKey,
secret: secret,
}
}
// DealerSearch performs a search for dealers and returns a list of matches
func (c *Client) DealerSearch(query string, pagination Pagination) ([]*Dealer, error) {
p := map[string]string{
"query": query,
"limit": fmt.Sprint(pagination.Limit),
"offset": fmt.Sprint(pagination.Offset),
}
path := buildPath("/dealers/search", p)
dealers := []*Dealer{}
err := c.doGet(path, &dealers)
if err != nil {
return nil, err
}
return dealers, nil
}
// Catalogs returns a list of catalogs from dealer id's given
func (c *Client) Catalogs(dealerIDs []string, pagination Pagination) ([]*Catalog, error) {
p := map[string]string{
"dealer_ids": stringListArgs(dealerIDs),
"limit": fmt.Sprint(pagination.Limit),
"offset": fmt.Sprint(pagination.Offset),
}
path := buildPath("/catalogs", p)
cats := []*Catalog{}
err := c.doGet(path, &cats)
if err != nil {
return nil, err
}
return cats, nil
}
// Offers returns a list of offers from catalog id's given
func (c *Client) Offers(catalogIDs []string, pagination Pagination) ([]*Offer, error) {
p := map[string]string{
"catalog_ids": stringListArgs(catalogIDs),
"limit": fmt.Sprint(pagination.Limit),
"offset": fmt.Sprint(pagination.Offset),
}
path := buildPath("/offers", p)
offers := []*Offer{}
err := c.doGet(path, &offers)
if err != nil {
return nil, err
}
return offers, nil
}