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)
| 256 | // bytes are external content, so they are returned as iostreams.Untrusted to |
| 257 | // force callers to choose between sanitized display and raw round-tripping. |
| 258 | func GetRawGistFile(httpClient *http.Client, rawURL safeurl.SafeURL) (iostreams.Untrusted, error) { |
| 259 | req, err := http.NewRequest("GET", rawURL.String(), nil) |
| 260 | if err != nil { |
| 261 | return iostreams.Untrusted{}, err |
| 262 | } |
| 263 | |
| 264 | resp, err := httpClient.Do(req) |
| 265 | if err != nil { |
| 266 | return iostreams.Untrusted{}, err |
| 267 | } |
| 268 | |
| 269 | defer resp.Body.Close() |
| 270 | |
| 271 | if resp.StatusCode != http.StatusOK { |
| 272 | return iostreams.Untrusted{}, api.HandleHTTPError(resp) |
| 273 | } |
| 274 | |
| 275 | body, err := io.ReadAll(resp.Body) |
| 276 | |
| 277 | if err != nil { |
| 278 | return iostreams.Untrusted{}, err |
| 279 | } |
| 280 | |
| 281 | return iostreams.NewUntrustedBytes(body), nil |
| 282 | } |