/
search.go
69 lines (58 loc) · 1.49 KB
/
search.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
package lepton
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// SearchPackages searches packages against pkghub
func SearchPackages(q string) (*PackageList, error) {
// we can ignore this error as the url being parsed here is const so wouldn't error
pkghub, _ := url.Parse(PkghubBaseURL + "/api/v1/search")
query := pkghub.Query()
query.Add("q", q)
pkghub.RawQuery = query.Encode()
req, err := BaseHTTPRequest("GET", pkghub.String(), nil)
if err != nil {
return nil, err
}
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
pkgList := PackageList{}
err = json.NewDecoder(response.Body).Decode(&pkgList)
if err != nil {
return nil, err
}
return &pkgList, nil
}
// SearchPackagesWithArch searches packages against pkghub with the
// specified arch.
func SearchPackagesWithArch(q string, arch string) (*PackageList, error) {
// we can ignore this error as the url being parsed here is const so
// wouldn't error
pkghub, _ := url.Parse(PkghubBaseURL + "/api/v1/search")
query := pkghub.Query()
query.Add("q", q)
if arch == "amd64" {
arch = "x86_64"
}
query.Add("arch", arch)
pkghub.RawQuery = query.Encode()
req, err := BaseHTTPRequest("GET", pkghub.String(), nil)
if err != nil {
return nil, err
}
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
pkgList := PackageList{}
err = json.NewDecoder(response.Body).Decode(&pkgList)
if err != nil {
fmt.Println(err)
return nil, err
}
return &pkgList, nil
}