github.com/fastly/go-fastly@v1.18.0/fastly/gzip.go (about) 1 package fastly 2 3 import ( 4 "fmt" 5 "net/url" 6 "sort" 7 "time" 8 ) 9 10 // Gzip represents an Gzip logging response from the Fastly API. 11 type Gzip struct { 12 ServiceID string `mapstructure:"service_id"` 13 Version int `mapstructure:"version"` 14 15 Name string `mapstructure:"name"` 16 ContentTypes string `mapstructure:"content_types"` 17 Extensions string `mapstructure:"extensions"` 18 CacheCondition string `mapstructure:"cache_condition"` 19 CreatedAt *time.Time `mapstructure:"created_at"` 20 UpdatedAt *time.Time `mapstructure:"updated_at"` 21 DeletedAt *time.Time `mapstructure:"deleted_at"` 22 } 23 24 // gzipsByName is a sortable list of gzips. 25 type gzipsByName []*Gzip 26 27 // Len, Swap, and Less implement the sortable interface. 28 func (s gzipsByName) Len() int { return len(s) } 29 func (s gzipsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 30 func (s gzipsByName) Less(i, j int) bool { 31 return s[i].Name < s[j].Name 32 } 33 34 // ListGzipsInput is used as input to the ListGzips function. 35 type ListGzipsInput struct { 36 // Service is the ID of the service (required). 37 Service string 38 39 // Version is the specific configuration version (required). 40 Version int 41 } 42 43 // ListGzips returns the list of gzips for the configuration version. 44 func (c *Client) ListGzips(i *ListGzipsInput) ([]*Gzip, error) { 45 if i.Service == "" { 46 return nil, ErrMissingService 47 } 48 49 if i.Version == 0 { 50 return nil, ErrMissingVersion 51 } 52 53 path := fmt.Sprintf("/service/%s/version/%d/gzip", i.Service, i.Version) 54 resp, err := c.Get(path, nil) 55 if err != nil { 56 return nil, err 57 } 58 59 var gzips []*Gzip 60 if err := decodeBodyMap(resp.Body, &gzips); err != nil { 61 return nil, err 62 } 63 sort.Stable(gzipsByName(gzips)) 64 return gzips, nil 65 } 66 67 // CreateGzipInput is used as input to the CreateGzip function. 68 type CreateGzipInput struct { 69 // Service is the ID of the service. Version is the specific configuration 70 // version. Both fields are required. 71 Service string 72 Version int 73 74 Name string `form:"name,omitempty"` 75 ContentTypes string `form:"content_types"` 76 Extensions string `form:"extensions"` 77 CacheCondition string `form:"cache_condition,omitempty"` 78 } 79 80 // CreateGzip creates a new Fastly Gzip. 81 func (c *Client) CreateGzip(i *CreateGzipInput) (*Gzip, error) { 82 if i.Service == "" { 83 return nil, ErrMissingService 84 } 85 86 if i.Version == 0 { 87 return nil, ErrMissingVersion 88 } 89 90 path := fmt.Sprintf("/service/%s/version/%d/gzip", i.Service, i.Version) 91 resp, err := c.PostForm(path, i, nil) 92 if err != nil { 93 return nil, err 94 } 95 96 var gzip *Gzip 97 if err := decodeBodyMap(resp.Body, &gzip); err != nil { 98 return nil, err 99 } 100 return gzip, nil 101 } 102 103 // GetGzipInput is used as input to the GetGzip function. 104 type GetGzipInput struct { 105 // Service is the ID of the service. Version is the specific configuration 106 // version. Both fields are required. 107 Service string 108 Version int 109 110 // Name is the name of the Gzip to fetch. 111 Name string 112 } 113 114 // GetGzip gets the Gzip configuration with the given parameters. 115 func (c *Client) GetGzip(i *GetGzipInput) (*Gzip, error) { 116 if i.Service == "" { 117 return nil, ErrMissingService 118 } 119 120 if i.Version == 0 { 121 return nil, ErrMissingVersion 122 } 123 124 if i.Name == "" { 125 return nil, ErrMissingName 126 } 127 128 path := fmt.Sprintf("/service/%s/version/%d/gzip/%s", i.Service, i.Version, url.PathEscape(i.Name)) 129 resp, err := c.Get(path, nil) 130 if err != nil { 131 return nil, err 132 } 133 134 var b *Gzip 135 if err := decodeBodyMap(resp.Body, &b); err != nil { 136 return nil, err 137 } 138 return b, nil 139 } 140 141 // UpdateGzipInput is used as input to the UpdateGzip function. 142 type UpdateGzipInput struct { 143 // Service is the ID of the service. Version is the specific configuration 144 // version. Both fields are required. 145 Service string 146 Version int 147 148 // Name is the name of the Gzip to update. 149 Name string 150 151 NewName string `form:"name,omitempty"` 152 ContentTypes string `form:"content_types,omitempty"` 153 Extensions string `form:"extensions,omitempty"` 154 CacheCondition string `form:"cache_condition,omitempty"` 155 } 156 157 // UpdateGzip updates a specific Gzip. 158 func (c *Client) UpdateGzip(i *UpdateGzipInput) (*Gzip, error) { 159 if i.Service == "" { 160 return nil, ErrMissingService 161 } 162 163 if i.Version == 0 { 164 return nil, ErrMissingVersion 165 } 166 167 if i.Name == "" { 168 return nil, ErrMissingName 169 } 170 171 path := fmt.Sprintf("/service/%s/version/%d/gzip/%s", i.Service, i.Version, url.PathEscape(i.Name)) 172 resp, err := c.PutForm(path, i, nil) 173 if err != nil { 174 return nil, err 175 } 176 177 var b *Gzip 178 if err := decodeBodyMap(resp.Body, &b); err != nil { 179 return nil, err 180 } 181 return b, nil 182 } 183 184 // DeleteGzipInput is the input parameter to DeleteGzip. 185 type DeleteGzipInput struct { 186 // Service is the ID of the service. Version is the specific configuration 187 // version. Both fields are required. 188 Service string 189 Version int 190 191 // Name is the name of the Gzip to delete (required). 192 Name string 193 } 194 195 // DeleteGzip deletes the given Gzip version. 196 func (c *Client) DeleteGzip(i *DeleteGzipInput) error { 197 if i.Service == "" { 198 return ErrMissingService 199 } 200 201 if i.Version == 0 { 202 return ErrMissingVersion 203 } 204 205 if i.Name == "" { 206 return ErrMissingName 207 } 208 209 path := fmt.Sprintf("/service/%s/version/%d/gzip/%s", i.Service, i.Version, url.PathEscape(i.Name)) 210 resp, err := c.Delete(path, nil) 211 if err != nil { 212 return err 213 } 214 215 var r *statusResp 216 if err := decodeBodyMap(resp.Body, &r); err != nil { 217 return err 218 } 219 if !r.Ok() { 220 return ErrNotOK 221 } 222 return nil 223 }