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