/
cli_backend.go
94 lines (79 loc) · 1.92 KB
/
cli_backend.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
87
88
89
90
91
92
93
94
package controller
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/mantil-io/mantil/cli/log"
)
const cliBackendURL = "https://ytg5gfkg5k.execute-api.eu-central-1.amazonaws.com"
var backend cliBackend
type cliBackend struct{}
func (b cliBackend) Report() cliBackendReport {
return cliBackendReport{
endpoint: apiEndpoint{url: cliBackendURL + "/report"},
}
}
type cliBackendReport struct {
endpoint apiEndpoint
}
func (r cliBackendReport) URL(req UploadURLRequest) (UploadURLResponse, error) {
var rsp UploadURLResponse
return rsp, r.endpoint.Call("url", req, &rsp)
}
func (r cliBackendReport) Confirm(req ConfirmRequest) error {
return r.endpoint.Call("confirm", req, nil)
}
type apiEndpoint struct {
url string
}
func (a apiEndpoint) Call(method string, req, rsp interface{}) error {
buf, _ := json.Marshal(req)
url := a.url
url = a.url + "/" + method
httpRsp, err := http.Post(url, "application/json", bytes.NewBuffer(buf))
if err != nil {
return log.Wrap(err)
}
if err != nil {
return log.Wrap(err)
}
defer httpRsp.Body.Close()
if httpRsp.StatusCode == http.StatusNoContent {
return nil
}
if httpRsp.StatusCode != http.StatusOK {
if apiErr := httpRsp.Header.Get("X-Api-Error"); apiErr != "" {
return log.Wrapf(apiErr)
}
return log.Wrapf("request failed with status code %d", httpRsp.StatusCode)
}
if rsp != nil {
buf, err := ioutil.ReadAll(httpRsp.Body)
if err != nil {
return log.Wrap(err)
}
switch v := rsp.(type) {
case []byte:
rsp = buf
case *string:
*v = string(buf)
default:
if err := json.Unmarshal(buf, rsp); err != nil {
return log.Wrap(err)
}
}
}
return nil
}
type UploadURLRequest struct {
WorkspaceID string `json:"workspaceId"`
Message string `json:"message"`
}
type UploadURLResponse struct {
ReportID string `json:"reportId"`
URL string `json:"url"`
}
type ConfirmRequest struct {
ReportID string `json:"reportId"`
}