-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathbot_detection.go
63 lines (51 loc) · 1.7 KB
/
bot_detection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package botman
import (
"context"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
)
type (
// The BotDetection interface supports retrieving bot detections
BotDetection interface {
// GetBotDetectionList todo: add link
GetBotDetectionList(ctx context.Context, params GetBotDetectionListRequest) (*GetBotDetectionListResponse, error)
}
// GetBotDetectionListRequest is used to retrieve the akamai bot category actions for a policy.
GetBotDetectionListRequest struct {
DetectionName string
}
// GetBotDetectionListResponse is returned from a call to GetBotDetectionList.
GetBotDetectionListResponse struct {
Detections []map[string]interface{} `json:"detections"`
}
)
func (b *botman) GetBotDetectionList(ctx context.Context, params GetBotDetectionListRequest) (*GetBotDetectionListResponse, error) {
logger := b.Log(ctx)
logger.Debug("GetBotDetectionList")
uri := "/appsec/v1/bot-detections"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GetBotDetectionList request: %w", err)
}
var result GetBotDetectionListResponse
resp, err := b.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("GetBotDetectionList request failed: %w", err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, b.Error(resp)
}
var filteredResult GetBotDetectionListResponse
if params.DetectionName != "" {
for _, val := range result.Detections {
if val["detectionName"].(string) == params.DetectionName {
filteredResult.Detections = append(filteredResult.Detections, val)
}
}
} else {
filteredResult = result
}
return &filteredResult, nil
}