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

     1  package botman
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  type (
    10  	// The AkamaiBotCategory interface supports retrieving akamai bot categories
    11  	AkamaiBotCategory interface {
    12  		// GetAkamaiBotCategoryList https://techdocs.akamai.com/bot-manager/reference/get-akamai-bot-categories
    13  		GetAkamaiBotCategoryList(ctx context.Context, params GetAkamaiBotCategoryListRequest) (*GetAkamaiBotCategoryListResponse, error)
    14  	}
    15  
    16  	// GetAkamaiBotCategoryListRequest is used to retrieve the akamai bot category actions for a policy.
    17  	GetAkamaiBotCategoryListRequest struct {
    18  		CategoryName string
    19  	}
    20  
    21  	// GetAkamaiBotCategoryListResponse is returned from a call to GetAkamaiBotCategoryList.
    22  	GetAkamaiBotCategoryListResponse struct {
    23  		Categories []map[string]interface{} `json:"categories"`
    24  	}
    25  )
    26  
    27  func (b *botman) GetAkamaiBotCategoryList(ctx context.Context, params GetAkamaiBotCategoryListRequest) (*GetAkamaiBotCategoryListResponse, error) {
    28  	logger := b.Log(ctx)
    29  	logger.Debug("GetAkamaiBotCategoryList")
    30  
    31  	uri := "/appsec/v1/akamai-bot-categories"
    32  
    33  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    34  	if err != nil {
    35  		return nil, fmt.Errorf("failed to create GetAkamaiBotCategoryList request: %w", err)
    36  	}
    37  
    38  	var result GetAkamaiBotCategoryListResponse
    39  	resp, err := b.Exec(req, &result)
    40  	if err != nil {
    41  		return nil, fmt.Errorf("GetAkamaiBotCategoryList request failed: %w", err)
    42  	}
    43  
    44  	if resp.StatusCode != http.StatusOK {
    45  		return nil, b.Error(resp)
    46  	}
    47  
    48  	var filteredResult GetAkamaiBotCategoryListResponse
    49  	if params.CategoryName != "" {
    50  		for _, val := range result.Categories {
    51  			if val["categoryName"].(string) == params.CategoryName {
    52  				filteredResult.Categories = append(filteredResult.Categories, val)
    53  			}
    54  		}
    55  	} else {
    56  		filteredResult = result
    57  	}
    58  	return &filteredResult, nil
    59  }