-
Notifications
You must be signed in to change notification settings - Fork 1
/
detectify.go
70 lines (58 loc) · 1.32 KB
/
detectify.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
const (
BaseEndpoint = "api.detectify.com/rest/v2"
APIKeyHeaderName = "X-Detectify-Key"
)
// Provider is a struct containing an http Client so that we can communicate with Detectify's public API
type Provider struct {
client http.Client
}
// NewProvider initializes and returns a new Provider instance with the specified http Client timeout
func NewProvider(timeout time.Duration) *Provider {
return &Provider{
client: http.Client{
Timeout: timeout,
},
}
}
// StartScan starts a scan on the specified scan profile token
func (p *Provider) StartScan() (string, error) {
path := fmt.Sprintf("scans/%s/", ScanProfileToken)
response, err := p.sendRequest(path)
if err != nil {
return "", err
}
return response, nil
}
func (p *Provider) sendRequest(path string) (string, error) {
url := fmt.Sprintf("https://%s/%s", BaseEndpoint, path)
req, err := http.NewRequest(
http.MethodPost,
url,
nil,
)
if err != nil {
log.Println(err)
return "", err
}
req.Header.Set(APIKeyHeaderName, ApiKey)
response, err := p.client.Do(req)
if err != nil {
log.Println(err)
return "", err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Println(err)
return "", err
}
return string(body), nil
}