Fetch fetches the image using the request and returns the response or an error. Unlike Send, it checks the request status and converts it into typed errors. Specifically, it checks for Not Modified, ensures that Partial Content response contains the entire image, and wraps gzip-encoded responses.
()
| 72 | // Specifically, it checks for Not Modified, ensures that Partial Content response |
| 73 | // contains the entire image, and wraps gzip-encoded responses. |
| 74 | func (r *Request) Fetch() (*http.Response, error) { |
| 75 | res, err := r.Send() |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | // If the source image was not modified, close the body and NotModifiedError |
| 81 | if res.StatusCode == http.StatusNotModified { |
| 82 | res.Body.Close() |
| 83 | return nil, newNotModifiedError(res.Header) |
| 84 | } |
| 85 | |
| 86 | // If the source responds with 206, check if the response contains an entire image. |
| 87 | // If not, return an error. |
| 88 | if res.StatusCode == http.StatusPartialContent { |
| 89 | err = checkPartialContentResponse(res) |
| 90 | if err != nil { |
| 91 | res.Body.Close() |
| 92 | return nil, err |
| 93 | } |
| 94 | } else if res.StatusCode != http.StatusOK { |
| 95 | body := extractErraticBody(res) |
| 96 | res.Body.Close() |
| 97 | return nil, newResponseStatusError(res.StatusCode, body) |
| 98 | } |
| 99 | |
| 100 | // If the response is gzip encoded, wrap it in a gzip reader |
| 101 | err = wrapGzipBody(res) |
| 102 | if err != nil { |
| 103 | res.Body.Close() |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | return res, nil |
| 108 | } |
| 109 | |
| 110 | // Cancel cancels the request context |
| 111 | func (r *Request) Cancel() { |
no test coverage detected