forked from argoproj/argo-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
52 lines (45 loc) · 1.42 KB
/
url.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
package store
import (
"crypto/tls"
"errors"
"io/ioutil"
"net/http"
"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
log "github.com/sirupsen/logrus"
)
// URLReader implements the ArtifactReader interface for urls
type URLReader struct {
urlArtifact *v1alpha1.URLArtifact
}
// NewURLReader creates a new ArtifactReader for workflows at URL endpoints.
func NewURLReader(urlArtifact *v1alpha1.URLArtifact) (ArtifactReader, error) {
if urlArtifact == nil {
return nil, errors.New("URLArtifact cannot be empty")
}
return &URLReader{urlArtifact}, nil
}
func (reader *URLReader) Read() ([]byte, error) {
log.Debugf("reading urlArtifact from %s", reader.urlArtifact.Path)
insecureSkipVerify := !reader.urlArtifact.VerifyCert
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify},
},
}
resp, err := client.Get(reader.urlArtifact.Path)
if err != nil {
log.Warnf("failed to read url %s: %s", reader.urlArtifact.Path, err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Warnf("failed to read %s. status code: %d", reader.urlArtifact.Path, resp.StatusCode)
return nil, errors.New("status code " + string(resp.StatusCode))
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warnf("failed to read url body for %s: %s", reader.urlArtifact.Path, err)
return nil, err
}
return content, nil
}