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

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