github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cps/post_verification_warnings.go (about)

     1  package cps
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  )
     9  
    10  type (
    11  	// PostVerification is a CPS API enabling management of post-verification-warnings
    12  	PostVerification interface {
    13  		// GetChangePostVerificationWarnings gets information about post verification warnings
    14  		//
    15  		// See: https://techdocs.akamai.com/cps/reference/get-change-allowed-input-param
    16  		GetChangePostVerificationWarnings(ctx context.Context, params GetChangeRequest) (*PostVerificationWarnings, error)
    17  		// AcknowledgePostVerificationWarnings sends acknowledgement request to CPS informing that the warnings should be ignored
    18  		//
    19  		// See: https://techdocs.akamai.com/cps/reference/post-change-allowed-input-param
    20  		AcknowledgePostVerificationWarnings(context.Context, AcknowledgementRequest) error
    21  	}
    22  
    23  	// PostVerificationWarnings is a response object containing all warnings encountered during enrollment post-verification
    24  	PostVerificationWarnings struct {
    25  		Warnings string `json:"warnings"`
    26  	}
    27  )
    28  
    29  var (
    30  	// ErrGetChangePostVerificationWarnings is returned when GetChangePostVerificationWarnings fails
    31  	ErrGetChangePostVerificationWarnings = errors.New("get post-verification-warnings")
    32  	// ErrAcknowledgePostVerificationWarnings is returned when AcknowledgePostVerificationWarnings fails
    33  	ErrAcknowledgePostVerificationWarnings = errors.New("acknowledging post-verification-warnings")
    34  )
    35  
    36  func (c *cps) GetChangePostVerificationWarnings(ctx context.Context, params GetChangeRequest) (*PostVerificationWarnings, error) {
    37  	c.Log(ctx).Debug("GetChangePostVerificationWarnings")
    38  
    39  	if err := params.Validate(); err != nil {
    40  		return nil, fmt.Errorf("%s: %w: %s", ErrGetChangePostVerificationWarnings, ErrStructValidation, err)
    41  	}
    42  
    43  	uri := fmt.Sprintf("/cps/v2/enrollments/%d/changes/%d/input/info/post-verification-warnings",
    44  		params.EnrollmentID, params.ChangeID)
    45  
    46  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetChangePostVerificationWarnings, err)
    49  	}
    50  	req.Header.Set("Accept", "application/vnd.akamai.cps.warnings.v1+json")
    51  
    52  	var result PostVerificationWarnings
    53  	resp, err := c.Exec(req, &result)
    54  	if err != nil {
    55  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetChangePostVerificationWarnings, err)
    56  	}
    57  
    58  	if resp.StatusCode != http.StatusOK {
    59  		return nil, fmt.Errorf("%s: %w", ErrGetChangePostVerificationWarnings, c.Error(resp))
    60  	}
    61  
    62  	return &result, nil
    63  }
    64  
    65  func (c *cps) AcknowledgePostVerificationWarnings(ctx context.Context, params AcknowledgementRequest) error {
    66  	c.Log(ctx).Debug("AcknowledgePostVerificationWarnings")
    67  
    68  	if err := params.Validate(); err != nil {
    69  		return fmt.Errorf("%s: %w: %s", ErrAcknowledgePostVerificationWarnings, ErrStructValidation, err)
    70  	}
    71  
    72  	uri := fmt.Sprintf("/cps/v2/enrollments/%d/changes/%d/input/update/post-verification-warnings-ack",
    73  		params.EnrollmentID, params.ChangeID)
    74  
    75  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
    76  	if err != nil {
    77  		return fmt.Errorf("%w: failed to create request: %s", ErrAcknowledgePostVerificationWarnings, err)
    78  	}
    79  	req.Header.Set("Accept", "application/vnd.akamai.cps.change-id.v1+json")
    80  	req.Header.Set("Content-Type", "application/vnd.akamai.cps.acknowledgement.v1+json; charset=utf-8")
    81  
    82  	resp, err := c.Exec(req, nil, params.Acknowledgement)
    83  	if err != nil {
    84  		return fmt.Errorf("%w: request failed: %s", ErrAcknowledgePostVerificationWarnings, err)
    85  	}
    86  
    87  	if resp.StatusCode != http.StatusOK {
    88  		return fmt.Errorf("%s: %w", ErrAcknowledgePostVerificationWarnings, c.Error(resp))
    89  	}
    90  
    91  	return nil
    92  }