github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/custom_code.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 CustomCode interface supports retrieving and updating custom code
    14  	CustomCode interface {
    15  		GetCustomCode(ctx context.Context, params GetCustomCodeRequest) (map[string]interface{}, error)
    16  
    17  		UpdateCustomCode(ctx context.Context, params UpdateCustomCodeRequest) (map[string]interface{}, error)
    18  	}
    19  
    20  	// GetCustomCodeRequest is used to retrieve custom code
    21  	GetCustomCodeRequest struct {
    22  		ConfigID int64
    23  		Version  int64
    24  	}
    25  
    26  	// UpdateCustomCodeRequest is used to modify custom code
    27  	UpdateCustomCodeRequest struct {
    28  		ConfigID    int64
    29  		Version     int64
    30  		JsonPayload json.RawMessage
    31  	}
    32  )
    33  
    34  // Validate validates a GetCustomCodeRequest.
    35  func (v GetCustomCodeRequest) Validate() error {
    36  	return validation.Errors{
    37  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    38  		"Version":  validation.Validate(v.Version, validation.Required),
    39  	}.Filter()
    40  }
    41  
    42  // Validate validates an UpdateCustomCodeRequest.
    43  func (v UpdateCustomCodeRequest) Validate() error {
    44  	return validation.Errors{
    45  		"ConfigID":    validation.Validate(v.ConfigID, validation.Required),
    46  		"Version":     validation.Validate(v.Version, validation.Required),
    47  		"JsonPayload": validation.Validate(v.JsonPayload, validation.Required),
    48  	}.Filter()
    49  }
    50  
    51  func (b *botman) GetCustomCode(ctx context.Context, params GetCustomCodeRequest) (map[string]interface{}, error) {
    52  	logger := b.Log(ctx)
    53  	logger.Debug("GetCustomCode")
    54  
    55  	if err := params.Validate(); err != nil {
    56  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    57  	}
    58  
    59  	uri := fmt.Sprintf(
    60  		"/appsec/v1/configs/%d/versions/%d/advanced-settings/transactional-endpoint-protection/custom-code",
    61  		params.ConfigID,
    62  		params.Version)
    63  
    64  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    65  	if err != nil {
    66  		return nil, fmt.Errorf("failed to create GetCustomCode request: %w", err)
    67  	}
    68  
    69  	var result map[string]interface{}
    70  	resp, err := b.Exec(req, &result)
    71  	if err != nil {
    72  		return nil, fmt.Errorf("GetCustomCode request failed: %w", err)
    73  	}
    74  
    75  	if resp.StatusCode != http.StatusOK {
    76  		return nil, b.Error(resp)
    77  	}
    78  
    79  	return result, nil
    80  }
    81  
    82  func (b *botman) UpdateCustomCode(ctx context.Context, params UpdateCustomCodeRequest) (map[string]interface{}, error) {
    83  	logger := b.Log(ctx)
    84  	logger.Debug("UpdateCustomCode")
    85  
    86  	if err := params.Validate(); err != nil {
    87  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    88  	}
    89  
    90  	putURL := fmt.Sprintf(
    91  		"/appsec/v1/configs/%d/versions/%d/advanced-settings/transactional-endpoint-protection/custom-code",
    92  		params.ConfigID,
    93  		params.Version,
    94  	)
    95  
    96  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
    97  	if err != nil {
    98  		return nil, fmt.Errorf("failed to create UpdateCustomCode request: %w", err)
    99  	}
   100  
   101  	var result map[string]interface{}
   102  	resp, err := b.Exec(req, &result, params.JsonPayload)
   103  	if err != nil {
   104  		return nil, fmt.Errorf("UpdateCustomCode request failed: %w", err)
   105  	}
   106  
   107  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
   108  		return nil, b.Error(resp)
   109  	}
   110  
   111  	return result, nil
   112  }