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. The rawURL is supplied by the API and points at gist.githubusercontent.com
(httpClient *http.Client, hostname string, rawURL safeurl.SafeURL)
| 255 | // The rawURL is supplied by the API and points at gist.githubusercontent.com rather than the |
| 256 | // API host, so it is requested as an absolute URL. The hostname only configures the client. |
| 257 | func GetRawGistFile(httpClient *http.Client, hostname string, rawURL safeurl.SafeURL) (iostreams.Untrusted, error) { |
| 258 | // TODO(api-client-rollout) |
| 259 | // This line of code is part of a mechanical roll out of the api client. |
| 260 | // As a follow up, consider whether the api client can be injected to this call site, rather than constructed |
| 261 | resp, err := api.NewClientFromHTTP(httpClient).Request(hostname, http.MethodGet, rawURL.String(), nil) |
| 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.UnexpectedStatusError(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 | } |