github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/validations.go (about) 1 package edgeworkers 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 10 validation "github.com/go-ozzo/ozzo-validation/v4" 11 ) 12 13 type ( 14 // Validations is an edgeworkers validations API interface 15 Validations interface { 16 // ValidateBundle given bundle validates it and returns a list of errors and/or warnings 17 // 18 // See: https://techdocs.akamai.com/edgeworkers/reference/post-validations 19 ValidateBundle(context.Context, ValidateBundleRequest) (*ValidateBundleResponse, error) 20 } 21 22 // ValidateBundleRequest contains request bundle parameter to validate 23 ValidateBundleRequest struct { 24 Bundle 25 } 26 27 // ValidateBundleResponse represents a response object returned by ValidateBundle 28 ValidateBundleResponse struct { 29 Errors []ValidationIssue `json:"errors"` 30 Warnings []ValidationIssue `json:"warnings"` 31 } 32 33 // ValidationIssue represents a single error or warning 34 ValidationIssue struct { 35 Type string `json:"type"` 36 Message string `json:"message"` 37 } 38 ) 39 40 var ( 41 // ErrValidateBundle is returned in case an error occurs on ValidateBundle operation 42 ErrValidateBundle = errors.New("validate a bundle") 43 ) 44 45 // Validate validates ValidateBundleRequest 46 func (r ValidateBundleRequest) Validate() error { 47 return validation.Errors{ 48 "Bundle.Reader": validation.Validate(r.Bundle.Reader, validation.NotNil), 49 }.Filter() 50 } 51 52 func (e *edgeworkers) ValidateBundle(ctx context.Context, params ValidateBundleRequest) (*ValidateBundleResponse, error) { 53 logger := e.Log(ctx) 54 logger.Debug("ValidateBundle") 55 56 if err := params.Validate(); err != nil { 57 return nil, fmt.Errorf("%s: %w: %s", ErrValidateBundle, ErrStructValidation, err) 58 } 59 60 uri := "/edgeworkers/v1/validations" 61 req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, ioutil.NopCloser(params.Bundle)) 62 if err != nil { 63 return nil, fmt.Errorf("%w: failed to create request: %s", ErrValidateBundle, err) 64 } 65 req.Header.Add("Content-Type", "application/gzip") 66 67 var result ValidateBundleResponse 68 resp, err := e.Exec(req, &result) 69 if err != nil { 70 return nil, fmt.Errorf("%w: request failed: %s", ErrValidateBundle, err) 71 } 72 73 if resp.StatusCode != http.StatusOK { 74 return nil, fmt.Errorf("%s: %w", ErrValidateBundle, e.Error(resp)) 75 } 76 77 return &result, nil 78 }