/
group.go
67 lines (55 loc) · 1.7 KB
/
group.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
package papi
import (
"context"
"errors"
"fmt"
"net/http"
)
type (
// Groups contains operations available on Group resource
Groups interface {
// GetGroups provides a read-only list of groups, which may contain properties.
//
// See: https://techdocs.akamai.com/property-mgr/reference/get-groups
GetGroups(context.Context) (*GetGroupsResponse, error)
}
// Group represents a property group resource
Group struct {
GroupID string `json:"groupId"`
GroupName string `json:"groupName"`
ParentGroupID string `json:"parentGroupId,omitempty"`
ContractIDs []string `json:"contractIds"`
}
// GroupItems represents sub-component of the group response
GroupItems struct {
Items []*Group `json:"items"`
}
// GetGroupsResponse represents a collection of groups
// This is the response to the /papi/v1/groups request
GetGroupsResponse struct {
AccountID string `json:"accountId"`
AccountName string `json:"accountName"`
Groups GroupItems `json:"groups"`
}
)
var (
// ErrGetGroups represents error when fetching groups fails
ErrGetGroups = errors.New("fetching groups")
)
func (p *papi) GetGroups(ctx context.Context) (*GetGroupsResponse, error) {
var groups GetGroupsResponse
logger := p.Log(ctx)
logger.Debug("GetGroups")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/papi/v1/groups", nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetGroups, err)
}
resp, err := p.Exec(req, &groups)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetGroups, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetGroups, p.Error(resp))
}
return &groups, nil
}