github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/custom_bot_category.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 CustomBotCategory interface supports creating, retrieving, modifying and removing custom bot categories for a
    14  	// configuration.
    15  	CustomBotCategory interface {
    16  		// GetCustomBotCategoryList https://techdocs.akamai.com/bot-manager/reference/get-custom-bot-categories
    17  		GetCustomBotCategoryList(ctx context.Context, params GetCustomBotCategoryListRequest) (*GetCustomBotCategoryListResponse, error)
    18  
    19  		// GetCustomBotCategory https://techdocs.akamai.com/bot-manager/reference/get-custom-bot-category
    20  		GetCustomBotCategory(ctx context.Context, params GetCustomBotCategoryRequest) (map[string]interface{}, error)
    21  
    22  		// CreateCustomBotCategory https://techdocs.akamai.com/bot-manager/reference/post-custom-bot-category
    23  		CreateCustomBotCategory(ctx context.Context, params CreateCustomBotCategoryRequest) (map[string]interface{}, error)
    24  
    25  		// UpdateCustomBotCategory https://techdocs.akamai.com/bot-manager/reference/put-custom-bot-category
    26  		UpdateCustomBotCategory(ctx context.Context, params UpdateCustomBotCategoryRequest) (map[string]interface{}, error)
    27  
    28  		// RemoveCustomBotCategory https://techdocs.akamai.com/bot-manager/reference/delete-custom-bot-category
    29  		RemoveCustomBotCategory(ctx context.Context, params RemoveCustomBotCategoryRequest) error
    30  	}
    31  
    32  	// GetCustomBotCategoryListRequest is used to retrieve custom bot categories for a configuration.
    33  	GetCustomBotCategoryListRequest struct {
    34  		ConfigID   int64
    35  		Version    int64
    36  		CategoryID string
    37  	}
    38  
    39  	// GetCustomBotCategoryListResponse is used to retrieve custom bot categories for a configuration.
    40  	GetCustomBotCategoryListResponse struct {
    41  		Categories []map[string]interface{} `json:"categories"`
    42  	}
    43  
    44  	// GetCustomBotCategoryRequest is used to retrieve a specific custom bot category
    45  	GetCustomBotCategoryRequest struct {
    46  		ConfigID   int64
    47  		Version    int64
    48  		CategoryID string
    49  	}
    50  
    51  	// CreateCustomBotCategoryRequest is used to create a new custom bot category for a specific configuration.
    52  	CreateCustomBotCategoryRequest struct {
    53  		ConfigID    int64
    54  		Version     int64
    55  		JsonPayload json.RawMessage
    56  	}
    57  
    58  	// UpdateCustomBotCategoryRequest is used to update an existing custom bot category
    59  	UpdateCustomBotCategoryRequest struct {
    60  		ConfigID    int64
    61  		Version     int64
    62  		CategoryID  string
    63  		JsonPayload json.RawMessage
    64  	}
    65  
    66  	// RemoveCustomBotCategoryRequest is used to remove an existing custom bot category
    67  	RemoveCustomBotCategoryRequest struct {
    68  		ConfigID   int64
    69  		Version    int64
    70  		CategoryID string
    71  	}
    72  )
    73  
    74  // Validate validates a GetCustomBotCategoryRequest.
    75  func (v GetCustomBotCategoryRequest) Validate() error {
    76  	return validation.Errors{
    77  		"ConfigID":   validation.Validate(v.ConfigID, validation.Required),
    78  		"Version":    validation.Validate(v.Version, validation.Required),
    79  		"CategoryID": validation.Validate(v.CategoryID, validation.Required),
    80  	}.Filter()
    81  }
    82  
    83  // Validate validates a GetCustomBotCategoryListRequest.
    84  func (v GetCustomBotCategoryListRequest) Validate() error {
    85  	return validation.Errors{
    86  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    87  		"Version":  validation.Validate(v.Version, validation.Required),
    88  	}.Filter()
    89  }
    90  
    91  // Validate validates a CreateCustomBotCategoryRequest.
    92  func (v CreateCustomBotCategoryRequest) Validate() error {
    93  	return validation.Errors{
    94  		"ConfigID":    validation.Validate(v.ConfigID, validation.Required),
    95  		"Version":     validation.Validate(v.Version, validation.Required),
    96  		"JsonPayload": validation.Validate(v.JsonPayload, validation.Required),
    97  	}.Filter()
    98  }
    99  
   100  // Validate validates an UpdateCustomBotCategoryRequest.
   101  func (v UpdateCustomBotCategoryRequest) Validate() error {
   102  	return validation.Errors{
   103  		"ConfigID":    validation.Validate(v.ConfigID, validation.Required),
   104  		"Version":     validation.Validate(v.Version, validation.Required),
   105  		"CategoryID":  validation.Validate(v.CategoryID, validation.Required),
   106  		"JsonPayload": validation.Validate(v.JsonPayload, validation.Required),
   107  	}.Filter()
   108  }
   109  
   110  // Validate validates a RemoveCustomBotCategoryRequest.
   111  func (v RemoveCustomBotCategoryRequest) Validate() error {
   112  	return validation.Errors{
   113  		"ConfigID":   validation.Validate(v.ConfigID, validation.Required),
   114  		"Version":    validation.Validate(v.Version, validation.Required),
   115  		"CategoryID": validation.Validate(v.CategoryID, validation.Required),
   116  	}.Filter()
   117  }
   118  
   119  func (b *botman) GetCustomBotCategory(ctx context.Context, params GetCustomBotCategoryRequest) (map[string]interface{}, error) {
   120  	logger := b.Log(ctx)
   121  	logger.Debug("GetCustomBotCategory")
   122  
   123  	if err := params.Validate(); err != nil {
   124  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   125  	}
   126  
   127  	uri := fmt.Sprintf(
   128  		"/appsec/v1/configs/%d/versions/%d/custom-bot-categories/%s",
   129  		params.ConfigID,
   130  		params.Version,
   131  		params.CategoryID)
   132  
   133  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   134  	if err != nil {
   135  		return nil, fmt.Errorf("failed to create GetCustomBotCategory request: %w", err)
   136  	}
   137  
   138  	var result map[string]interface{}
   139  	resp, err := b.Exec(req, &result)
   140  	if err != nil {
   141  		return nil, fmt.Errorf("GetCustomBotCategory request failed: %w", err)
   142  	}
   143  
   144  	if resp.StatusCode != http.StatusOK {
   145  		return nil, b.Error(resp)
   146  	}
   147  
   148  	return result, nil
   149  }
   150  
   151  func (b *botman) GetCustomBotCategoryList(ctx context.Context, params GetCustomBotCategoryListRequest) (*GetCustomBotCategoryListResponse, error) {
   152  	logger := b.Log(ctx)
   153  	logger.Debug("GetCustomBotCategoryList")
   154  
   155  	if err := params.Validate(); err != nil {
   156  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   157  	}
   158  
   159  	uri := fmt.Sprintf(
   160  		"/appsec/v1/configs/%d/versions/%d/custom-bot-categories",
   161  		params.ConfigID,
   162  		params.Version,
   163  	)
   164  
   165  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   166  	if err != nil {
   167  		return nil, fmt.Errorf("failed to create GetlustomDenyList request: %w", err)
   168  	}
   169  
   170  	var result GetCustomBotCategoryListResponse
   171  	resp, err := b.Exec(req, &result)
   172  	if err != nil {
   173  		return nil, fmt.Errorf("GetCustomBotCategoryList request failed: %w", err)
   174  	}
   175  
   176  	if resp.StatusCode != http.StatusOK {
   177  		return nil, b.Error(resp)
   178  	}
   179  
   180  	var filteredResult GetCustomBotCategoryListResponse
   181  	if params.CategoryID != "" {
   182  		for _, val := range result.Categories {
   183  			if val["categoryId"].(string) == params.CategoryID {
   184  				filteredResult.Categories = append(filteredResult.Categories, val)
   185  			}
   186  		}
   187  	} else {
   188  		filteredResult = result
   189  	}
   190  	return &filteredResult, nil
   191  }
   192  
   193  func (b *botman) UpdateCustomBotCategory(ctx context.Context, params UpdateCustomBotCategoryRequest) (map[string]interface{}, error) {
   194  	logger := b.Log(ctx)
   195  	logger.Debug("UpdateCustomBotCategory")
   196  
   197  	if err := params.Validate(); err != nil {
   198  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   199  	}
   200  
   201  	putURL := fmt.Sprintf(
   202  		"/appsec/v1/configs/%d/versions/%d/custom-bot-categories/%s",
   203  		params.ConfigID,
   204  		params.Version,
   205  		params.CategoryID,
   206  	)
   207  
   208  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
   209  	if err != nil {
   210  		return nil, fmt.Errorf("failed to create UpdateCustomBotCategory request: %w", err)
   211  	}
   212  
   213  	var result map[string]interface{}
   214  	resp, err := b.Exec(req, &result, params.JsonPayload)
   215  	if err != nil {
   216  		return nil, fmt.Errorf("UpdateCustomBotCategory request failed: %w", err)
   217  	}
   218  
   219  	if resp.StatusCode != http.StatusOK {
   220  		return nil, b.Error(resp)
   221  	}
   222  
   223  	return result, nil
   224  }
   225  
   226  func (b *botman) CreateCustomBotCategory(ctx context.Context, params CreateCustomBotCategoryRequest) (map[string]interface{}, error) {
   227  	logger := b.Log(ctx)
   228  	logger.Debug("CreateCustomBotCategory")
   229  
   230  	if err := params.Validate(); err != nil {
   231  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   232  	}
   233  
   234  	uri := fmt.Sprintf(
   235  		"/appsec/v1/configs/%d/versions/%d/custom-bot-categories",
   236  		params.ConfigID,
   237  		params.Version,
   238  	)
   239  
   240  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
   241  	if err != nil {
   242  		return nil, fmt.Errorf("failed to create CreateCustomBotCategory request: %w", err)
   243  	}
   244  
   245  	var result map[string]interface{}
   246  	resp, err := b.Exec(req, &result, params.JsonPayload)
   247  	if err != nil {
   248  		return nil, fmt.Errorf("CreateCustomBotCategory request failed: %w", err)
   249  	}
   250  
   251  	if resp.StatusCode != http.StatusCreated {
   252  		return nil, b.Error(resp)
   253  	}
   254  
   255  	return result, nil
   256  }
   257  
   258  func (b *botman) RemoveCustomBotCategory(ctx context.Context, params RemoveCustomBotCategoryRequest) error {
   259  	logger := b.Log(ctx)
   260  	logger.Debug("RemoveCustomBotCategory")
   261  
   262  	if err := params.Validate(); err != nil {
   263  		return fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   264  	}
   265  
   266  	uri := fmt.Sprintf("/appsec/v1/configs/%d/versions/%d/custom-bot-categories/%s",
   267  		params.ConfigID,
   268  		params.Version,
   269  		params.CategoryID)
   270  
   271  	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
   272  	if err != nil {
   273  		return fmt.Errorf("failed to create RemoveCustomBotCategory request: %w", err)
   274  	}
   275  
   276  	var result map[string]interface{}
   277  	resp, err := b.Exec(req, &result)
   278  	if err != nil {
   279  		return fmt.Errorf("RemoveCustomBotCategory request failed: %w", err)
   280  	}
   281  
   282  	if resp.StatusCode != http.StatusNoContent {
   283  		return b.Error(resp)
   284  	}
   285  
   286  	return nil
   287  }