-
Notifications
You must be signed in to change notification settings - Fork 70
/
validations.go
72 lines (59 loc) · 1.95 KB
/
validations.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
package edgeworkers
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// ValidateBundleRequest contains request bundle parameter to validate
ValidateBundleRequest struct {
Bundle
}
// ValidateBundleResponse represents a response object returned by ValidateBundle
ValidateBundleResponse struct {
Errors []ValidationIssue `json:"errors"`
Warnings []ValidationIssue `json:"warnings"`
}
// ValidationIssue represents a single error or warning
ValidationIssue struct {
Type string `json:"type"`
Message string `json:"message"`
}
)
var (
// ErrValidateBundle is returned in case an error occurs on ValidateBundle operation
ErrValidateBundle = errors.New("validate a bundle")
)
// Validate validates ValidateBundleRequest
func (r ValidateBundleRequest) Validate() error {
return validation.Errors{
"Bundle.Reader": validation.Validate(r.Bundle.Reader, validation.NotNil),
}.Filter()
}
func (e *edgeworkers) ValidateBundle(ctx context.Context, params ValidateBundleRequest) (*ValidateBundleResponse, error) {
logger := e.Log(ctx)
logger.Debug("ValidateBundle")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrValidateBundle, ErrStructValidation, err)
}
uri := "/edgeworkers/v1/validations"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, ioutil.NopCloser(params.Bundle))
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrValidateBundle, err)
}
req.Header.Add("Content-Type", "application/gzip")
var result ValidateBundleResponse
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrValidateBundle, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrValidateBundle, e.Error(resp))
}
return &result, nil
}