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