-
Notifications
You must be signed in to change notification settings - Fork 1
/
npm.go
82 lines (66 loc) · 2.03 KB
/
npm.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"net/http"
)
type NpmPackage struct {
Name string
Version string
}
type NpmObject struct {
Package NpmPackage
}
type NpmResponse struct {
Objects []NpmObject `json:"objects"`
}
type NpmDownloadCounts struct {
Downloads int `json:"downloads"`
Day string `json:"day"`
}
type NpmDownloadCountResponse struct {
Start string `json:"start"`
End string `json:"end"`
Package string `json:"package"`
Downloads []NpmDownloadCounts `json:"downloads"`
}
func (npm *NpmDownloadCountResponse) GetDownloads() int {
if len(npm.Downloads) < 1 {
logrus.Errorf("Empty download count for %s", npm.Package)
return 0
}
firstDownload := npm.Downloads[0]
return firstDownload.Downloads
}
func GetDownloadCountForCeloNpmPackage(currentPackage NpmPackage, client *http.Client) (NpmDownloadCountResponse, error) {
var result NpmDownloadCountResponse
var url = fmt.Sprintf("https://api.npmjs.org/downloads/range/last-day/%s", currentPackage.Name)
res, err := client.Get(url)
if err != nil {
logrus.Errorf("Error making http request to registry.npmjs.org: %s", err)
return result, err
}
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
logrus.Errorf("Could not decode download count response body from npmjs.org: %s", err)
return result, err
}
logrus.Debugf("npm download count response: %v", result)
return result, nil
}
func GetCeloNPMPackages(client *http.Client) (NpmResponse, error) {
var npmResponse NpmResponse
logrus.Info("Sending request to registry.npmjs.org")
res, err := client.Get("https://registry.npmjs.org/-/v1/search?text=scope:celo&size=100")
if err != nil {
logrus.Errorf("Error making http request to registry.npmjs.org: %s", err)
return npmResponse, err
}
err = json.NewDecoder(res.Body).Decode(&npmResponse)
if err != nil {
logrus.Errorf("Could not decode response body from registry.npmjs.org: %s", err)
return npmResponse, err
}
return npmResponse, nil
}