-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
42 lines (36 loc) · 871 Bytes
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
var indexClient = &http.Client{
Timeout: 3 * time.Second,
}
func fetchFromIndexSince(since time.Time) ([]moduleVersion, error) {
timestamp := since.Format(time.RFC3339)
url := "https://index.golang.org/index?limit=" + strconv.Itoa(limit) + "&since=" + timestamp
resp, err := indexClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected http status %d", resp.StatusCode)
}
scanner := bufio.NewScanner(resp.Body)
var moduleVersions []moduleVersion
for scanner.Scan() {
var mv moduleVersion
line := scanner.Bytes()
err = json.Unmarshal(line, &mv)
if err != nil {
return nil, err
}
moduleVersions = append(moduleVersions, mv)
}
return moduleVersions, nil
}