fetchContent retrieves the raw Contents API response for a single path. It requests the unified object media type so directories, files, symlinks, and submodules all come back as a single JSON object distinguished by the type field.
(httpClient *http.Client, repo ghrepo.Interface, filePath, ref string)
| 83 | // It requests the unified object media type so directories, files, symlinks, and |
| 84 | // submodules all come back as a single JSON object distinguished by the type field. |
| 85 | func fetchContent(httpClient *http.Client, repo ghrepo.Interface, filePath, ref string) (*contentsResponse, error) { |
| 86 | apiPath := contentsAPIPath(repo, filePath, ref) |
| 87 | |
| 88 | req, err := http.NewRequest("GET", apiPath, nil) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | // We use the "application/vnd.github.object+json" media type to request a unified object |
| 93 | // representation from the Contents API. Without this, the API returns a JSON array for |
| 94 | // directories and a JSON object for files. |
| 95 | req.Header.Set("Accept", "application/vnd.github.object+json") |
| 96 | |
| 97 | resp, err := httpClient.Do(req) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | defer resp.Body.Close() |
| 102 | |
| 103 | if resp.StatusCode > 299 { |
| 104 | return nil, api.HandleHTTPError(resp) |
| 105 | } |
| 106 | |
| 107 | body, err := io.ReadAll(resp.Body) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | var content contentsResponse |
| 113 | if err := json.Unmarshal(body, &content); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | return &content, nil |
| 117 | } |
| 118 | |
| 119 | // fetchFile retrieves a single path's metadata and inline content via the REST Contents API. |
| 120 | // |
no test coverage detected