forked from hyeomans/zuora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.go
77 lines (58 loc) · 1.8 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package zuora
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/hyeomans/zuora/errors"
)
type ProductsService struct {
config *Config
tokenService *TokenService
actionsService *ActionsService
errorHandler errors.RequestHandler
}
func newProductsService(config *Config, tokenService *TokenService, actionsService *ActionsService, errorHandler errors.RequestHandler) *ProductsService {
return &ProductsService{
config: config,
tokenService: tokenService,
errorHandler: errorHandler,
actionsService: actionsService,
}
}
func (s *ProductsService) Get(ctx context.Context, objectID string) (Product, error) {
token, err := s.tokenService.Token(ctx)
if err != nil {
return Product{}, err
}
documentsURL := fmt.Sprint(s.config.BaseURL, "/v1/object/product/", objectID)
req, err := http.NewRequest(http.MethodGet, documentsURL, nil)
if err != nil {
return Product{}, s.errorHandler.BadRequest(err)
}
req.Header.Add("Authorization", fmt.Sprint("Bearer ", token.AccessToken))
req.Header.Add("Content-Type", "application/json")
if ctx.Value("Zuora-Entity-Ids") != nil {
req.Header.Add("Zuora-Entity-Ids", ctx.Value("Zuora-Entity-Ids").(string))
}
if ctx.Value("Zuora-Track-Id") != nil {
req.Header.Add("Zuora-Track-Id", ctx.Value("Zuora-Track-Id").(string))
}
res, err := s.config.HTTPClient.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return Product{}, s.errorHandler.BadRequest(err)
}
body, err := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
return Product{}, s.errorHandler.InvalidResponse(body, res.StatusCode)
}
jsonResponse := Product{}
if err := json.Unmarshal(body, &jsonResponse); err != nil {
return Product{}, s.errorHandler.BadRequest(err)
}
return jsonResponse, err
}