forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_limiting_example_test.go
107 lines (89 loc) · 1.86 KB
/
rate_limiting_example_test.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cloudflare_test
import (
"fmt"
"log"
cloudflare "github.com/cloudflare/cloudflare-go"
)
var exampleNewRateLimit = cloudflare.RateLimit{
Description: "test",
Match: cloudflare.RateLimitTrafficMatcher{
Request: cloudflare.RateLimitRequestMatcher{
URLPattern: "exampledomain.com/test-rate-limit",
},
},
Threshold: 0,
Period: 0,
Action: cloudflare.RateLimitAction{
Mode: "ban",
Timeout: 60,
},
Correlate: &cloudflare.RateLimitCorrelate{
By: "nat",
},
}
func ExampleAPI_CreateRateLimit() {
api, err := cloudflare.New(apiKey, user)
if err != nil {
log.Fatal(err)
}
zoneID, err := api.ZoneIDByName(domain)
if err != nil {
log.Fatal(err)
}
rateLimit, err := api.CreateRateLimit(zoneID, exampleNewRateLimit)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", rateLimit)
}
func ExampleAPI_ListRateLimits() {
api, err := cloudflare.New(apiKey, user)
if err != nil {
log.Fatal(err)
}
zoneID, err := api.ZoneIDByName(domain)
if err != nil {
log.Fatal(err)
}
pageOpts := cloudflare.PaginationOptions{
PerPage: 5,
Page: 1,
}
rateLimits, _, err := api.ListRateLimits(zoneID, pageOpts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", rateLimits)
for _, r := range rateLimits {
fmt.Printf("%+v\n", r)
}
}
func ExampleAPI_RateLimit() {
api, err := cloudflare.New(apiKey, user)
if err != nil {
log.Fatal(err)
}
zoneID, err := api.ZoneIDByName(domain)
if err != nil {
log.Fatal(err)
}
rateLimits, err := api.RateLimit(zoneID, "my_rate_limit_id")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", rateLimits)
}
func ExampleAPI_DeleteRateLimit() {
api, err := cloudflare.New(apiKey, user)
if err != nil {
log.Fatal(err)
}
zoneID, err := api.ZoneIDByName(domain)
if err != nil {
log.Fatal(err)
}
err = api.DeleteRateLimit(zoneID, "my_rate_limit_id")
if err != nil {
log.Fatal(err)
}
}