GetRawGistFile fetches the full content of a gist file from its raw URL. The bytes are external content, so they are returned as iostreams.Untrusted to force callers to choose between sanitized display and raw round-tripping.
(httpClient *http.Client, rawURL safeurl.SafeURL)
| 253 | // bytes are external content, so they are returned as iostreams.Untrusted to |
| 254 | // force callers to choose between sanitized display and raw round-tripping. |
| 255 | func GetRawGistFile(httpClient *http.Client, rawURL safeurl.SafeURL) (iostreams.Untrusted, error) { |
| 256 | req, err := http.NewRequest("GET", rawURL.String(), nil) |
| 257 | if err != nil { |
| 258 | return iostreams.Untrusted{}, err |
| 259 | } |
| 260 | |
| 261 | resp, err := httpClient.Do(req) |
| 262 | if err != nil { |
| 263 | return iostreams.Untrusted{}, err |
| 264 | } |
| 265 | |
| 266 | defer resp.Body.Close() |
| 267 | |
| 268 | if resp.StatusCode != http.StatusOK { |
| 269 | return iostreams.Untrusted{}, api.HandleHTTPError(resp) |
| 270 | } |
| 271 | |
| 272 | body, err := io.ReadAll(resp.Body) |
| 273 | |
| 274 | if err != nil { |
| 275 | return iostreams.Untrusted{}, err |
| 276 | } |
| 277 | |
| 278 | return iostreams.NewUntrustedBytes(body), nil |
| 279 | } |