-
Notifications
You must be signed in to change notification settings - Fork 70
/
ruleformats.go
51 lines (40 loc) · 1.23 KB
/
ruleformats.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
package papi
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
)
type (
// GetRuleFormatsResponse contains the response body of GET /rule-formats request
GetRuleFormatsResponse struct {
RuleFormats RuleFormatItems `json:"ruleFormats"`
}
// RuleFormatItems contains a list of rule formats
RuleFormatItems struct {
Items []string `json:"items"`
}
)
var (
// ErrGetRuleFormats represents error when fetching rule formats fails
ErrGetRuleFormats = errors.New("fetching rule formats")
)
func (p *papi) GetRuleFormats(ctx context.Context) (*GetRuleFormatsResponse, error) {
var ruleFormats GetRuleFormatsResponse
logger := p.Log(ctx)
logger.Debug("GetRuleFormats")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/papi/v1/rule-formats", nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetRuleFormats, err)
}
resp, err := p.Exec(req, &ruleFormats)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetRuleFormats, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetRuleFormats, p.Error(resp))
}
return &ruleFormats, nil
}