/
properties.go
90 lines (74 loc) · 2.61 KB
/
properties.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
78
79
80
81
82
83
84
85
86
87
88
89
90
package edgeworkers
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// Properties is an edgeworkers properties API interface
Properties interface {
// ListProperties lists all properties for a given edgeworker ID
//
// See: https://techdocs.akamai.com/edgeworkers/reference/properties#get-properties
ListProperties(context.Context, ListPropertiesRequest) (*ListPropertiesResponse, error)
}
// ListPropertiesRequest contains parameters used to list properties
ListPropertiesRequest struct {
EdgeWorkerID int
ActiveOnly bool
}
// ListPropertiesResponse represents a response object returned by ListPropertiesRequest
ListPropertiesResponse struct {
Properties []Property `json:"properties"`
LimitedAccessToProperties bool `json:"limitedAccessToProperties"`
}
// Property represents a single property object
Property struct {
ID int64 `json:"propertyId"`
Name string `json:"propertyName"`
StagingVersion int `json:"stagingVersion"`
ProductionVersion int `json:"productionVersion"`
LatestVersion int `json:"latestVersion"`
}
)
// Validate validates ListPropertiesRequest
func (r ListPropertiesRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(r.EdgeWorkerID, validation.Required),
}.Filter()
}
var (
// ErrListProperties is returned in case an error occurs on ListProperties operation
ErrListProperties = errors.New("list properties")
)
func (e *edgeworkers) ListProperties(ctx context.Context, params ListPropertiesRequest) (*ListPropertiesResponse, error) {
logger := e.Log(ctx)
logger.Debug("ListProperies")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListProperties, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/edgeworkers/v1/ids/%d/properties", params.EdgeWorkerID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListProperties, err)
}
q := uri.Query()
q.Add("activeOnly", strconv.FormatBool(params.ActiveOnly))
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListProperties, err)
}
var result ListPropertiesResponse
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListProperties, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListProperties, e.Error(resp))
}
return &result, nil
}