forked from pivotal-cf/go-pivnet
/
products.go
59 lines (48 loc) · 1005 Bytes
/
products.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
package pivnet
import (
"fmt"
"net/http"
"github.com/pivotal-cf/go-pivnet/logger"
)
type ProductsService struct {
client Client
l logger.Logger
}
type Product struct {
ID int `json:"id,omitempty" yaml:"id,omitempty"`
Slug string `json:"slug,omitempty" yaml:"slug,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
}
type ProductsResponse struct {
Products []Product `json:"products,omitempty"`
}
func (p ProductsService) List() ([]Product, error) {
url := "/products"
var response ProductsResponse
_, _, err := p.client.MakeRequest(
"GET",
url,
http.StatusOK,
nil,
&response,
)
if err != nil {
return []Product{}, err
}
return response.Products, nil
}
func (p ProductsService) Get(slug string) (Product, error) {
url := fmt.Sprintf("/products/%s", slug)
var response Product
_, _, err := p.client.MakeRequest(
"GET",
url,
http.StatusOK,
nil,
&response,
)
if err != nil {
return Product{}, err
}
return response, nil
}