MCPcopy Index your code
hub / github.com/cli/cli / fetchContent

Function fetchContent

pkg/cmd/repo/read-file/http.go:85–117  ·  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

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.
85func 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//

Callers 1

fetchFileFunction · 0.85

Calls 5

HandleHTTPErrorFunction · 0.92
contentsAPIPathFunction · 0.85
SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected