| 63 | } |
| 64 | |
| 65 | func (s searcher) Code(query Query) (CodeResult, error) { |
| 66 | result := CodeResult{} |
| 67 | |
| 68 | // We will request either the query limit if it's less than 1 page, or our max page size. |
| 69 | // This number doesn't change to keep a valid offset. |
| 70 | // |
| 71 | // For example, say we want 150 items out of 500. |
| 72 | // We request page #1 for 100 items and get items 0 to 99. |
| 73 | // Then we request page #2 for 100 items, we get items 100 to 199 and only keep 100 to 149. |
| 74 | // If we were to request page #2 for 50 items, we would instead get items 50 to 99. |
| 75 | numItemsToRetrieve := query.Limit |
| 76 | query.Limit = min(numItemsToRetrieve, maxPerPage) |
| 77 | query.Page = 1 |
| 78 | |
| 79 | for numItemsToRetrieve > 0 { |
| 80 | page := CodeResult{} |
| 81 | link, err := s.search(query, &page) |
| 82 | if err != nil { |
| 83 | return result, err |
| 84 | } |
| 85 | |
| 86 | // If we're going to reach the requested limit, only add that many items, |
| 87 | // otherwise add all the results. |
| 88 | numItemsToAdd := min(len(page.Items), numItemsToRetrieve) |
| 89 | result.IncompleteResults = page.IncompleteResults |
| 90 | // The API returns how many items match the query in every response. |
| 91 | // With the example above, this would be 500. |
| 92 | result.Total = page.Total |
| 93 | result.Items = append(result.Items, page.Items[:numItemsToAdd]...) |
| 94 | numItemsToRetrieve = numItemsToRetrieve - numItemsToAdd |
| 95 | |
| 96 | query.Page = nextPage(link) |
| 97 | if query.Page == 0 { |
| 98 | break |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return result, nil |
| 103 | } |
| 104 | |
| 105 | func (s searcher) Commits(query Query) (CommitsResult, error) { |
| 106 | result := CommitsResult{} |