-
Notifications
You must be signed in to change notification settings - Fork 16
/
http_bundle_client.go
60 lines (49 loc) · 1.53 KB
/
http_bundle_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
package openpolicyagent
import (
"bytes"
"fmt"
"github.com/hexa-org/policy-orchestrator/pkg/compressionsupport"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
)
type HTTPClient interface {
Get(url string) (resp *http.Response, err error)
Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
}
type HTTPBundleClient struct {
BundleServerURL string
HttpClient HTTPClient
}
func (b *HTTPBundleClient) GetDataFromBundle(path string) ([]byte, error) {
get, getErr := b.HttpClient.Get(b.BundleServerURL)
if getErr != nil {
return nil, getErr
}
defer get.Body.Close()
gz, gzipErr := compressionsupport.UnGzip(get.Body)
if gzipErr != nil {
return nil, fmt.Errorf("unable to ungzip: %w", gzipErr)
}
tarErr := compressionsupport.UnTarToPath(bytes.NewReader(gz), path)
if tarErr != nil {
return nil, fmt.Errorf("unable to untar to path: %w", tarErr)
}
return os.ReadFile(filepath.Join(path, "/bundle/data.json"))
}
// todo - ignoring errors for the moment while spiking
func (b *HTTPBundleClient) PostBundle(bundle []byte) (int, error) {
// todo - Log out the errors at minimum.
buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)
formFile, _ := writer.CreateFormFile("bundle", "bundle.tar.gz")
_, _ = formFile.Write(bundle)
_ = writer.Close()
parse, _ := url.Parse(b.BundleServerURL)
contentType := writer.FormDataContentType()
resp, err := b.HttpClient.Post(fmt.Sprintf("%s://%s/bundles", parse.Scheme, parse.Host), contentType, buf)
return resp.StatusCode, err
}