SearchCode executes the query against the API, returning all items that were found.
(ctx context.Context, hc *http.Client, token string, page int, perPage int)
| 130 | |
| 131 | // SearchCode executes the query against the API, returning all items that were found. |
| 132 | func SearchCode(ctx context.Context, hc *http.Client, token string, page int, perPage int) ([]Item, error) { |
| 133 | params := url.Values{} |
| 134 | params.Set("q", query) |
| 135 | params.Set("page", strconv.Itoa(page)) |
| 136 | params.Set("per_page", strconv.Itoa(perPage)) |
| 137 | |
| 138 | req, _ := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/search/code?"+params.Encode(), nil) |
| 139 | SetHeaders(req, token) |
| 140 | resp, err := hc.Do(req) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | defer resp.Body.Close() |
| 145 | if HandleRate(resp) { |
| 146 | return SearchCode(ctx, hc, token, page, perPage) |
| 147 | } |
| 148 | var sr CodeSearchResult |
| 149 | if err = json.NewDecoder(resp.Body).Decode(&sr); err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | if resp.StatusCode != 200 { |
| 153 | if sr.Message != "" { |
| 154 | return nil, fmt.Errorf("search error: %s (HTTP %d)", sr.Message, resp.StatusCode) |
| 155 | } |
| 156 | return nil, fmt.Errorf("search error: HTTP %d", resp.StatusCode) |
| 157 | } |
| 158 | return sr.Items, nil |
| 159 | } |
| 160 | |
| 161 | // GetContent gets the ContentFile from the given URL. |
| 162 | func GetContent(ctx context.Context, hc *http.Client, token string, contentsURL string) (*ContentFile, error) { |
no test coverage detected