MCPcopy Create free account
hub / github.com/cli/cli / fetchContent

Function fetchContent

pkg/cmd/repo/read-file/http.go:84–113  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

82// It requests the unified object media type so directories, files, symlinks, and
83// submodules all come back as a single JSON object distinguished by the type field.
84func fetchContent(httpClient *http.Client, repo ghrepo.Interface, filePath, ref string) (*contentsResponse, error) {
85 apiPath, err := contentsAPIPath(repo, filePath, ref)
86 if err != nil {
87 return nil, err
88 }
89
90 // We use the "application/vnd.github.object+json" media type to request a unified object
91 // representation from the Contents API. Without this, the API returns a JSON array for
92 // directories and a JSON object for files.
93 // TODO(api-client-rollout)
94 // This line of code is part of a mechanical roll out of the api client.
95 // As a follow up, consider whether the api client can be injected to this call site, rather than constructed
96 resp, err := api.NewClientFromHTTP(httpClient).Request(repo.RepoHost(), http.MethodGet, apiPath.String(), nil,
97 api.WithHeader("Accept", "application/vnd.github.object+json"))
98 if err != nil {
99 return nil, err
100 }
101 defer resp.Body.Close()
102
103 body, err := io.ReadAll(resp.Body)
104 if err != nil {
105 return nil, err
106 }
107
108 var content contentsResponse
109 if err := json.Unmarshal(body, &content); err != nil {
110 return nil, err
111 }
112 return &content, nil
113}
114
115// fetchFile retrieves a single path's metadata and inline content via the REST Contents API.
116//

Callers 1

fetchFileFunction · 0.85

Calls 7

NewClientFromHTTPFunction · 0.92
WithHeaderFunction · 0.92
contentsAPIPathFunction · 0.85
RequestMethod · 0.80
RepoHostMethod · 0.65
StringMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected