-
Notifications
You must be signed in to change notification settings - Fork 594
/
cache_reserve.go
83 lines (67 loc) · 2.38 KB
/
cache_reserve.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
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
// CacheReserve is the structure of the API object for the cache reserve
// setting.
type CacheReserve struct {
ID string `json:"id,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Value string `json:"value"`
}
// CacheReserveDetailsResponse is the API response for the cache reserve
// setting.
type CacheReserveDetailsResponse struct {
Result CacheReserve `json:"result"`
Response
}
type zoneCacheReserveSingleResponse struct {
Response
Result CacheReserve `json:"result"`
}
type GetCacheReserveParams struct{}
type UpdateCacheReserveParams struct {
Value string `json:"value"`
}
// GetCacheReserve returns information about the current cache reserve settings.
//
// API reference: https://developers.cloudflare.com/api/operations/zone-cache-settings-get-cache-reserve-setting
func (api *API) GetCacheReserve(ctx context.Context, rc *ResourceContainer, params GetCacheReserveParams) (CacheReserve, error) {
if rc.Level != ZoneRouteLevel {
return CacheReserve{}, ErrRequiredZoneLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/cache/cache_reserve", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return CacheReserve{}, err
}
var cacheReserveDetailsResponse CacheReserveDetailsResponse
err = json.Unmarshal(res, &cacheReserveDetailsResponse)
if err != nil {
return CacheReserve{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return cacheReserveDetailsResponse.Result, nil
}
// UpdateCacheReserve updates the cache reserve setting for a zone
//
// API reference: https://developers.cloudflare.com/api/operations/zone-cache-settings-change-cache-reserve-setting
func (api *API) UpdateCacheReserve(ctx context.Context, rc *ResourceContainer, params UpdateCacheReserveParams) (CacheReserve, error) {
if rc.Level != ZoneRouteLevel {
return CacheReserve{}, ErrRequiredZoneLevelResourceContainer
}
uri := fmt.Sprintf("/%s/%s/cache/cache_reserve", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
if err != nil {
return CacheReserve{}, err
}
response := &zoneCacheReserveSingleResponse{}
err = json.Unmarshal(res, &response)
if err != nil {
return CacheReserve{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response.Result, nil
}