-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
86 lines (72 loc) · 1.69 KB
/
client.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
83
84
85
86
package cveapi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
var errNotFound = errors.New("Not found CVE")
// Client for interacting with CVEAPI.
type Client struct {
BaseURL *url.URL
httpClient *http.Client
}
// NewClient is constructor
func NewClient() *Client {
u, _ := url.Parse("https://v1.cveapi.com")
return &Client{
BaseURL: u,
httpClient: &http.Client{},
}
}
// sendRequest sends a HTTP request to the CVEAPI.
func (c *Client) sendRequest(method, path string, body io.Reader) (*http.Response, error) {
// Compose URL
rel := &url.URL{Path: path}
targetURL := c.BaseURL.ResolveReference(rel)
// Write body
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
// New HTTP GET request
req, err := http.NewRequest(method, targetURL.String(), body)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
// log.Printf("Doing request: %s", targetURL.String())
return (c.httpClient).Do(req)
}
// GetCVEData call api get detailed information on specific security issues, specified by CVE number.
func (c *Client) GetCVEData(cve string) (*Response, error) {
path := fmt.Sprintf("/%s.json", cve)
httpResp, err := c.sendRequest("GET", path, nil)
if err != nil {
return nil, err
}
result := &Response{}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
return nil, errNotFound
}
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result, nil
}