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

     1  package botman
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  
     9  	validation "github.com/go-ozzo/ozzo-validation/v4"
    10  )
    11  
    12  type (
    13  	// The ChallengeInterceptionRules interface supports retrieving and updating the challenge interception rules for a
    14  	// configuration
    15  	// Deprecated: this interface will be removed in a future release. Use ChallengeInjectionRules instead.
    16  	ChallengeInterceptionRules interface {
    17  		// GetChallengeInterceptionRules https://techdocs.akamai.com/bot-manager/reference/get-challenge-interception-rules
    18  		// Deprecated: this method will be removed in a future release. Use GetChallengeInjectionRules instead.
    19  		GetChallengeInterceptionRules(ctx context.Context, params GetChallengeInterceptionRulesRequest) (map[string]interface{}, error)
    20  		// UpdateChallengeInterceptionRules https://techdocs.akamai.com/bot-manager/reference/put-challenge-interception-rules
    21  		// Deprecated: this method will be removed in a future release. Use UpdateChallengeInjectionRules instead.
    22  		UpdateChallengeInterceptionRules(ctx context.Context, params UpdateChallengeInterceptionRulesRequest) (map[string]interface{}, error)
    23  	}
    24  
    25  	// GetChallengeInterceptionRulesRequest is used to retrieve challenge interception rules
    26  	// Deprecated: this struct will be removed in a future release. Use GetChallengeInjectionRulesRequest instead.
    27  	GetChallengeInterceptionRulesRequest struct {
    28  		ConfigID int64
    29  		Version  int64
    30  	}
    31  
    32  	// UpdateChallengeInterceptionRulesRequest is used to modify challenge interception rules
    33  	// Deprecated: this struct will be removed in a future release. Use UpdateChallengeInjectionRulesRequest instead.
    34  	UpdateChallengeInterceptionRulesRequest struct {
    35  		ConfigID    int64
    36  		Version     int64
    37  		JsonPayload json.RawMessage
    38  	}
    39  )
    40  
    41  // Validate validates a GetChallengeInterceptionRulesRequest.
    42  func (v GetChallengeInterceptionRulesRequest) Validate() error {
    43  	return validation.Errors{
    44  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    45  		"Version":  validation.Validate(v.Version, validation.Required),
    46  	}.Filter()
    47  }
    48  
    49  // Validate validates an UpdateChallengeInterceptionRulesRequest.
    50  func (v UpdateChallengeInterceptionRulesRequest) Validate() error {
    51  	return validation.Errors{
    52  		"ConfigID":    validation.Validate(v.ConfigID, validation.Required),
    53  		"Version":     validation.Validate(v.Version, validation.Required),
    54  		"JsonPayload": validation.Validate(v.JsonPayload, validation.Required),
    55  	}.Filter()
    56  }
    57  
    58  func (b *botman) GetChallengeInterceptionRules(ctx context.Context, params GetChallengeInterceptionRulesRequest) (map[string]interface{}, error) {
    59  	logger := b.Log(ctx)
    60  	logger.Debug("GetChallengeInterceptionRules")
    61  
    62  	if err := params.Validate(); err != nil {
    63  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    64  	}
    65  
    66  	uri := fmt.Sprintf(
    67  		"/appsec/v1/configs/%d/versions/%d/response-actions/challenge-interception-rules",
    68  		params.ConfigID,
    69  		params.Version)
    70  
    71  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    72  	if err != nil {
    73  		return nil, fmt.Errorf("failed to create GetChallengeInterceptionRules request: %w", err)
    74  	}
    75  
    76  	var result map[string]interface{}
    77  	resp, err := b.Exec(req, &result)
    78  	if err != nil {
    79  		return nil, fmt.Errorf("GetChallengeInterceptionRules request failed: %w", err)
    80  	}
    81  
    82  	if resp.StatusCode != http.StatusOK {
    83  		return nil, b.Error(resp)
    84  	}
    85  
    86  	return result, nil
    87  }
    88  
    89  func (b *botman) UpdateChallengeInterceptionRules(ctx context.Context, params UpdateChallengeInterceptionRulesRequest) (map[string]interface{}, error) {
    90  	logger := b.Log(ctx)
    91  	logger.Debug("UpdateChallengeInterceptionRules")
    92  
    93  	if err := params.Validate(); err != nil {
    94  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    95  	}
    96  
    97  	putURL := fmt.Sprintf(
    98  		"/appsec/v1/configs/%d/versions/%d/response-actions/challenge-interception-rules",
    99  		params.ConfigID,
   100  		params.Version,
   101  	)
   102  
   103  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
   104  	if err != nil {
   105  		return nil, fmt.Errorf("failed to create UpdateChallengeInterceptionRules request: %w", err)
   106  	}
   107  
   108  	var result map[string]interface{}
   109  	resp, err := b.Exec(req, &result, params.JsonPayload)
   110  	if err != nil {
   111  		return nil, fmt.Errorf("UpdateChallengeInterceptionRules request failed: %w", err)
   112  	}
   113  
   114  	if resp.StatusCode != http.StatusOK {
   115  		return nil, b.Error(resp)
   116  	}
   117  
   118  	return result, nil
   119  }