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

Function fetchContent

pkg/cmd/repo/read-file/http.go:85–120  ·  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, err := contentsAPIPath(repo, filePath, ref)
87 if err != nil {
88 return nil, err
89 }
90
91 req, err := http.NewRequest("GET", apiPath.String(), nil)
92 if err != nil {
93 return nil, err
94 }
95 // We use the "application/vnd.github.object+json" media type to request a unified object
96 // representation from the Contents API. Without this, the API returns a JSON array for
97 // directories and a JSON object for files.
98 req.Header.Set("Accept", "application/vnd.github.object+json")
99
100 resp, err := httpClient.Do(req)
101 if err != nil {
102 return nil, err
103 }
104 defer resp.Body.Close()
105
106 if resp.StatusCode > 299 {
107 return nil, api.HandleHTTPError(resp)
108 }
109
110 body, err := io.ReadAll(resp.Body)
111 if err != nil {
112 return nil, err
113 }
114
115 var content contentsResponse
116 if err := json.Unmarshal(body, &content); err != nil {
117 return nil, err
118 }
119 return &content, nil
120}
121
122// fetchFile retrieves a single path's metadata and inline content via the REST Contents API.
123//

Callers 1

fetchFileFunction · 0.85

Calls 6

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

Tested by

no test coverage detected