Debian Codegraph @

fastly/purge.go

raw symbols SCIP
1 package fastly
2
3 import "fmt"
4
5 // Purge is a response from a purge request.
6 type Purge struct {
7 // Status is the status of the purge, usually "ok".
8 Status string `mapstructure:"status"`
9
10 // ID is the unique ID of the purge request.
11 ID string `mapstructure:"id"`
12 }
13
14 // PurgeInput is used as input to the Purge function.
15 type PurgeInput struct {
16 // URL is the URL to purge (required).
17 URL string
18
19 // Soft performs a soft purge.
20 Soft bool
21 }
22
23 // Purge instantly purges an individual URL.
24 func (c *Client) Purge(i *PurgeInput) (*Purge, error) {
25 if i.URL == "" {
26 return nil, ErrMissingURL
27 }
28
29 req, err := c.RawRequest("PURGE", i.URL, nil)
30 if err != nil {
31 return nil, err
32 }
33
34 if i.Soft {
35 req.Header.Set("Fastly-Soft-Purge", "1")
36 }
37
38 resp, err := checkResp(c.HTTPClient.Do(req))
39 if err != nil {
40 return nil, err
41 }
42
43 var r *Purge
44 if err := decodeJSON(&r, resp.Body); err != nil {
45 return nil, err
46 }
47 return r, nil
48 }
49
50 // PurgeKeyInput is used as input to the Purge function.
51 type PurgeKeyInput struct {
52 // Service is the ID of the service (required).
53 Service string
54
55 // Key is the key to purge (required).
56 Key string
57
58 // Soft performs a soft purge.
59 Soft bool
60 }
61
62 // PurgeKey instantly purges a particular service of items tagged with a key.
63 func (c *Client) PurgeKey(i *PurgeKeyInput) (*Purge, error) {
64 if i.Service == "" {
65 return nil, ErrMissingService
66 }
67
68 if i.Key == "" {
69 return nil, ErrMissingKey
70 }
71
72 path := fmt.Sprintf("/service/%s/purge/%s", i.Service, i.Key)
73 req, err := c.RawRequest("POST", path, nil)
74 if err != nil {
75 return nil, err
76 }
77
78 if i.Soft {
79 req.Header.Set("Fastly-Soft-Purge", "1")
80 }
81
82 resp, err := checkResp(c.HTTPClient.Do(req))
83 if err != nil {
84 return nil, err
85 }
86
87 var r *Purge
88 if err := decodeJSON(&r, resp.Body); err != nil {
89 return nil, err
90 }
91 return r, nil
92 }
93
94 // PurgeAllInput is used as input to the Purge function.
95 type PurgeAllInput struct {
96 // Service is the ID of the service (required).
97 Service string
98
99 // Soft performs a soft purge.
100 Soft bool
101 }
102
103 // PurgeAll instantly purges everything from a service.
104 func (c *Client) PurgeAll(i *PurgeAllInput) (*Purge, error) {
105 if i.Service == "" {
106 return nil, ErrMissingService
107 }
108
109 path := fmt.Sprintf("/service/%s/purge_all", i.Service)
110 req, err := c.RawRequest("POST", path, nil)
111 if err != nil {
112 return nil, err
113 }
114
115 if i.Soft {
116 req.Header.Set("Fastly-Soft-Purge", "1")
117 }
118
119 resp, err := checkResp(c.HTTPClient.Do(req))
120 if err != nil {
121 return nil, err
122 }
123
124 var r *Purge
125 if err := decodeJSON(&r, resp.Body); err != nil {
126 return nil, err
127 }
128 return r, nil
129
130 }