-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcontract.go
58 lines (46 loc) · 1.39 KB
/
contract.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
package papi
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
)
type (
// Contract represents a property contract resource
Contract struct {
ContractID string `json:"contractId"`
ContractTypeName string `json:"contractTypeName"`
}
// ContractsItems is the response items array
ContractsItems struct {
Items []*Contract `json:"items"`
}
// GetContractsResponse is the response to the /papi/v1/contracts request
GetContractsResponse struct {
AccountID string `json:"accountId"`
Contracts ContractsItems `json:"contracts"`
}
)
var (
// ErrGetContracts represents error when fetching contracts fails
ErrGetContracts = errors.New("fetching contracts")
)
func (p *papi) GetContracts(ctx context.Context) (*GetContractsResponse, error) {
var contracts GetContractsResponse
logger := p.Log(ctx)
logger.Debug("GetContracts")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/papi/v1/contracts", nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetContracts, err)
}
resp, err := p.Exec(req, &contracts)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetContracts, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetContracts, p.Error(resp))
}
return &contracts, nil
}