Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 4 | "encoding/json" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 5 | "fmt" |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 6 | "io" |
| 7 | "io/ioutil" |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 8 | "strconv" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 9 | "strings" |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 10 | "time" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 11 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 12 | "github.com/gophercloud/gophercloud" |
| 13 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // Object is a structure that holds information related to a storage object. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 17 | type Object struct { |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 18 | // Bytes is the total number of bytes that comprise the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 19 | Bytes int64 `json:"bytes"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 20 | |
| 21 | // ContentType is the content type of the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 22 | ContentType string `json:"content_type"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 23 | |
| 24 | // Hash represents the MD5 checksum value of the object's content. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 25 | Hash string `json:"hash"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 26 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 27 | // LastModified is the time the object was last modified, represented |
| 28 | // as a string. |
| 29 | LastModified time.Time `json:"-"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 30 | |
| 31 | // Name is the unique name for the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 32 | Name string `json:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 33 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 34 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 35 | func (r *Object) UnmarshalJSON(b []byte) error { |
| 36 | type tmp Object |
| 37 | var s *struct { |
| 38 | tmp |
| 39 | LastModified gophercloud.JSONRFC3339MilliNoZ `json:"last_modified"` |
| 40 | } |
| 41 | |
| 42 | err := json.Unmarshal(b, &s) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | *r = Object(s.tmp) |
| 48 | |
| 49 | r.LastModified = time.Time(s.LastModified) |
| 50 | |
| 51 | return nil |
| 52 | |
| 53 | } |
| 54 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 55 | // ObjectPage is a single page of objects that is returned from a call to the |
| 56 | // List function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 57 | type ObjectPage struct { |
| 58 | pagination.MarkerPageBase |
| 59 | } |
| 60 | |
| 61 | // IsEmpty returns true if a ListResult contains no object names. |
| 62 | func (r ObjectPage) IsEmpty() (bool, error) { |
| 63 | names, err := ExtractNames(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 64 | return len(names) == 0, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // LastMarker returns the last object name in a ListResult. |
| 68 | func (r ObjectPage) LastMarker() (string, error) { |
| 69 | names, err := ExtractNames(r) |
| 70 | if err != nil { |
| 71 | return "", err |
| 72 | } |
| 73 | if len(names) == 0 { |
| 74 | return "", nil |
| 75 | } |
| 76 | return names[len(names)-1], nil |
| 77 | } |
| 78 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 79 | // ExtractInfo is a function that takes a page of objects and returns their full information. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 80 | func ExtractInfo(r pagination.Page) ([]Object, error) { |
| 81 | var s []Object |
| 82 | err := (r.(ObjectPage)).ExtractInto(&s) |
| 83 | return s, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // ExtractNames is a function that takes a page of objects and returns only their names. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 87 | func ExtractNames(r pagination.Page) ([]string, error) { |
| 88 | casted := r.(ObjectPage) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 89 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 90 | switch { |
| 91 | case strings.HasPrefix(ct, "application/json"): |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 92 | parsed, err := ExtractInfo(r) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | names := make([]string, 0, len(parsed)) |
| 98 | for _, object := range parsed { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 99 | names = append(names, object.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 100 | } |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 101 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 102 | return names, nil |
| 103 | case strings.HasPrefix(ct, "text/plain"): |
| 104 | names := make([]string, 0, 50) |
| 105 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 106 | body := string(r.(ObjectPage).Body.([]uint8)) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 107 | for _, name := range strings.Split(body, "\n") { |
| 108 | if len(name) > 0 { |
| 109 | names = append(names, name) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return names, nil |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 114 | case strings.HasPrefix(ct, "text/html"): |
| 115 | return []string{}, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 116 | default: |
| 117 | return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct) |
| 118 | } |
| 119 | } |
| 120 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 121 | // DownloadHeader represents the headers returned in the response from a Download request. |
| 122 | type DownloadHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 123 | AcceptRanges string `json:"Accept-Ranges"` |
| 124 | ContentDisposition string `json:"Content-Disposition"` |
| 125 | ContentEncoding string `json:"Content-Encoding"` |
| 126 | ContentLength int64 `json:"-"` |
| 127 | ContentType string `json:"Content-Type"` |
| 128 | Date time.Time `json:"-"` |
| 129 | DeleteAt time.Time `json:"-"` |
| 130 | ETag string `json:"Etag"` |
| 131 | LastModified time.Time `json:"-"` |
| 132 | ObjectManifest string `json:"X-Object-Manifest"` |
| 133 | StaticLargeObject bool `json:"X-Static-Large-Object"` |
| 134 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 135 | } |
| 136 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 137 | func (r *DownloadHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 138 | type tmp DownloadHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 139 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 140 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 141 | ContentLength string `json:"Content-Length"` |
| 142 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 143 | DeleteAt gophercloud.JSONUnix `json:"X-Delete-At"` |
| 144 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 145 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 146 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 151 | *r = DownloadHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 152 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 153 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 154 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 155 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 156 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 157 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | } |
| 162 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 163 | r.Date = time.Time(s.Date) |
| 164 | r.DeleteAt = time.Time(s.DeleteAt) |
| 165 | r.LastModified = time.Time(s.LastModified) |
| 166 | |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 167 | return nil |
| 168 | } |
| 169 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 170 | // DownloadResult is a *http.Response that is returned from a call to the Download function. |
| 171 | type DownloadResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 172 | gophercloud.HeaderResult |
Jamie Hannaford | ee11555 | 2014-10-27 16:11:05 +0100 | [diff] [blame] | 173 | Body io.ReadCloser |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 176 | // Extract will return a struct of headers returned from a call to Download. To obtain |
| 177 | // a map of headers, call the ExtractHeader method on the DownloadResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 178 | func (r DownloadResult) Extract() (*DownloadHeader, error) { |
| 179 | var s *DownloadHeader |
| 180 | err := r.ExtractInto(&s) |
| 181 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 184 | // ExtractContent is a function that takes a DownloadResult's io.Reader body |
| 185 | // and reads all available data into a slice of bytes. Please be aware that due |
| 186 | // the nature of io.Reader is forward-only - meaning that it can only be read |
| 187 | // once and not rewound. You can recreate a reader from the output of this |
| 188 | // function by using bytes.NewReader(downloadBytes) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 189 | func (r *DownloadResult) ExtractContent() ([]byte, error) { |
| 190 | if r.Err != nil { |
| 191 | return nil, r.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 192 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 193 | defer r.Body.Close() |
| 194 | body, err := ioutil.ReadAll(r.Body) |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 195 | if err != nil { |
| 196 | return nil, err |
| 197 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 198 | r.Body.Close() |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 199 | return body, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 202 | // GetHeader represents the headers returned in the response from a Get request. |
| 203 | type GetHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 204 | ContentDisposition string `json:"Content-Disposition"` |
| 205 | ContentEncoding string `json:"Content-Encoding"` |
| 206 | ContentLength int64 `json:"-"` |
| 207 | ContentType string `json:"Content-Type"` |
| 208 | Date time.Time `json:"-"` |
| 209 | DeleteAt time.Time `json:"-"` |
| 210 | ETag string `json:"Etag"` |
| 211 | LastModified time.Time `json:"-"` |
| 212 | ObjectManifest string `json:"X-Object-Manifest"` |
| 213 | StaticLargeObject bool `json:"X-Static-Large-Object"` |
| 214 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 215 | } |
| 216 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 217 | func (r *GetHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 218 | type tmp GetHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 219 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 220 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 221 | ContentLength string `json:"Content-Length"` |
| 222 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 223 | DeleteAt gophercloud.JSONUnix `json:"X-Delete-At"` |
| 224 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 225 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 226 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 227 | if err != nil { |
| 228 | return err |
| 229 | } |
| 230 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 231 | *r = GetHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 232 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 233 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 234 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 235 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 236 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 237 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 238 | if err != nil { |
| 239 | return err |
| 240 | } |
| 241 | } |
| 242 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 243 | r.Date = time.Time(s.Date) |
| 244 | r.DeleteAt = time.Time(s.DeleteAt) |
| 245 | r.LastModified = time.Time(s.LastModified) |
| 246 | |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 247 | return nil |
| 248 | } |
| 249 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 250 | // GetResult is a *http.Response that is returned from a call to the Get function. |
| 251 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 252 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 255 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 256 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 257 | func (r GetResult) Extract() (*GetHeader, error) { |
| 258 | var s *GetHeader |
| 259 | err := r.ExtractInto(&s) |
| 260 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 263 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 264 | // and returns the custom metadata associated with the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 265 | func (r GetResult) ExtractMetadata() (map[string]string, error) { |
| 266 | if r.Err != nil { |
| 267 | return nil, r.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 268 | } |
| 269 | metadata := make(map[string]string) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 270 | for k, v := range r.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 271 | if strings.HasPrefix(k, "X-Object-Meta-") { |
| 272 | key := strings.TrimPrefix(k, "X-Object-Meta-") |
| 273 | metadata[key] = v[0] |
| 274 | } |
| 275 | } |
| 276 | return metadata, nil |
| 277 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 278 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 279 | // CreateHeader represents the headers returned in the response from a Create request. |
| 280 | type CreateHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 281 | ContentLength int64 `json:"-"` |
| 282 | ContentType string `json:"Content-Type"` |
| 283 | Date time.Time `json:"-"` |
| 284 | ETag string `json:"Etag"` |
| 285 | LastModified time.Time `json:"-"` |
| 286 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 287 | } |
| 288 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 289 | func (r *CreateHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 290 | type tmp CreateHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 291 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 292 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 293 | ContentLength string `json:"Content-Length"` |
| 294 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 295 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 296 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 297 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 298 | if err != nil { |
| 299 | return err |
| 300 | } |
| 301 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 302 | *r = CreateHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 303 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 304 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 305 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 306 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 307 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 308 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | } |
| 313 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 314 | r.Date = time.Time(s.Date) |
| 315 | r.LastModified = time.Time(s.LastModified) |
| 316 | |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 317 | return nil |
| 318 | } |
| 319 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 320 | // CreateResult represents the result of a create operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 321 | type CreateResult struct { |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 322 | checksum string |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 323 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 326 | // Extract will return a struct of headers returned from a call to Create. To obtain |
| 327 | // a map of headers, call the ExtractHeader method on the CreateResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 328 | func (r CreateResult) Extract() (*CreateHeader, error) { |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 329 | //if r.Header.Get("ETag") != fmt.Sprintf("%x", localChecksum) { |
| 330 | // return nil, ErrWrongChecksum{} |
| 331 | //} |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 332 | var s *CreateHeader |
| 333 | err := r.ExtractInto(&s) |
| 334 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // UpdateHeader represents the headers returned in the response from a Update request. |
| 338 | type UpdateHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 339 | ContentLength int64 `json:"-"` |
| 340 | ContentType string `json:"Content-Type"` |
| 341 | Date time.Time `json:"-"` |
| 342 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 343 | } |
| 344 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 345 | func (r *UpdateHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 346 | type tmp UpdateHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 347 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 348 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 349 | ContentLength string `json:"Content-Length"` |
| 350 | Date gophercloud.JSONRFC1123 `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 351 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 352 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 353 | if err != nil { |
| 354 | return err |
| 355 | } |
| 356 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 357 | *r = UpdateHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 358 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 359 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 360 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 361 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 362 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 363 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 364 | if err != nil { |
| 365 | return err |
| 366 | } |
| 367 | } |
| 368 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 369 | r.Date = time.Time(s.Date) |
| 370 | |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 371 | return nil |
| 372 | } |
| 373 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 374 | // UpdateResult represents the result of an update operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 375 | type UpdateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 376 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 377 | } |
| 378 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 379 | // Extract will return a struct of headers returned from a call to Update. To obtain |
| 380 | // a map of headers, call the ExtractHeader method on the UpdateResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 381 | func (r UpdateResult) Extract() (*UpdateHeader, error) { |
| 382 | var s *UpdateHeader |
| 383 | err := r.ExtractInto(&s) |
| 384 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // DeleteHeader represents the headers returned in the response from a Delete request. |
| 388 | type DeleteHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 389 | ContentLength int64 `json:"Content-Length"` |
| 390 | ContentType string `json:"Content-Type"` |
| 391 | Date time.Time `json:"-"` |
| 392 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 393 | } |
| 394 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 395 | func (r *DeleteHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 396 | type tmp DeleteHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 397 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 398 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 399 | ContentLength string `json:"Content-Length"` |
| 400 | Date gophercloud.JSONRFC1123 `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 401 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 402 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 403 | if err != nil { |
| 404 | return err |
| 405 | } |
| 406 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 407 | *r = DeleteHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 408 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 409 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 410 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 411 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 412 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 413 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 414 | if err != nil { |
| 415 | return err |
| 416 | } |
| 417 | } |
| 418 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 419 | r.Date = time.Time(s.Date) |
| 420 | |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 421 | return nil |
| 422 | } |
| 423 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 424 | // DeleteResult represents the result of a delete operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 425 | type DeleteResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 426 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 429 | // Extract will return a struct of headers returned from a call to Delete. To obtain |
| 430 | // a map of headers, call the ExtractHeader method on the DeleteResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 431 | func (r DeleteResult) Extract() (*DeleteHeader, error) { |
| 432 | var s *DeleteHeader |
| 433 | err := r.ExtractInto(&s) |
| 434 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // CopyHeader represents the headers returned in the response from a Copy request. |
| 438 | type CopyHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 439 | ContentLength int64 `json:"-"` |
| 440 | ContentType string `json:"Content-Type"` |
| 441 | CopiedFrom string `json:"X-Copied-From"` |
| 442 | CopiedFromLastModified time.Time `json:"-"` |
| 443 | Date time.Time `json:"-"` |
| 444 | ETag string `json:"Etag"` |
| 445 | LastModified time.Time `json:"-"` |
| 446 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 447 | } |
| 448 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 449 | func (r *CopyHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 450 | type tmp CopyHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 451 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 452 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 453 | ContentLength string `json:"Content-Length"` |
| 454 | CopiedFromLastModified gophercloud.JSONRFC1123 `json:"X-Copied-From-Last-Modified"` |
| 455 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 456 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 457 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 458 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 459 | if err != nil { |
| 460 | return err |
| 461 | } |
| 462 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 463 | *r = CopyHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 464 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 465 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 466 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 467 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 468 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 469 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 470 | if err != nil { |
| 471 | return err |
| 472 | } |
| 473 | } |
| 474 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 475 | r.Date = time.Time(s.Date) |
| 476 | r.CopiedFromLastModified = time.Time(s.CopiedFromLastModified) |
| 477 | r.LastModified = time.Time(s.LastModified) |
| 478 | |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 479 | return nil |
| 480 | } |
| 481 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 482 | // CopyResult represents the result of a copy operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 483 | type CopyResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 484 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 485 | } |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 486 | |
| 487 | // Extract will return a struct of headers returned from a call to Copy. To obtain |
| 488 | // a map of headers, call the ExtractHeader method on the CopyResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 489 | func (r CopyResult) Extract() (*CopyHeader, error) { |
| 490 | var s *CopyHeader |
| 491 | err := r.ExtractInto(&s) |
| 492 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 493 | } |