-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f047e55
commit 5e5c2b2
Showing
3 changed files
with
92 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// 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 updateCacheReserveRequest struct { | ||
Value string `json:"value"` | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.