forked from mikaukora/rcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
143 lines (127 loc) · 3.24 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cloud
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/robocorp/rcc/common"
"github.com/robocorp/rcc/xviper"
)
type internalClient struct {
endpoint string
client *http.Client
}
type Request struct {
Url string
Headers map[string]string
TransferEncoding string
ContentLength int64
Body io.Reader
Stream io.Writer
}
type Response struct {
Status int
Err error
Body []byte
Elapsed time.Duration
}
type Client interface {
Endpoint() string
NewRequest(string) *Request
Get(request *Request) *Response
Post(request *Request) *Response
Put(request *Request) *Response
Delete(request *Request) *Response
NewClient(endpoint string) (Client, error)
}
func EnsureHttps(endpoint string) (string, error) {
nice := strings.TrimRight(strings.TrimSpace(endpoint), "/")
if strings.HasPrefix(nice, "https://") {
return nice, nil
}
message := fmt.Sprintf("Endpoint '%s' must start with https:// prefix.", nice)
return "", errors.New(message)
}
func NewClient(endpoint string) (Client, error) {
https, err := EnsureHttps(endpoint)
if err != nil {
return nil, err
}
return &internalClient{
endpoint: https,
client: &http.Client{},
}, nil
}
func (it *internalClient) NewClient(endpoint string) (Client, error) {
return NewClient(endpoint)
}
func (it *internalClient) Endpoint() string {
return it.endpoint
}
func (it *internalClient) does(method string, request *Request) *Response {
response := new(Response)
started := time.Now()
defer func() {
response.Elapsed = time.Now().Sub(started)
}()
url := it.Endpoint() + request.Url
httpRequest, err := http.NewRequest(method, url, request.Body)
if err != nil {
response.Status = 9001
response.Err = err
return response
}
if request.ContentLength > 0 {
httpRequest.ContentLength = request.ContentLength
}
if len(request.TransferEncoding) > 0 {
httpRequest.TransferEncoding = []string{request.TransferEncoding}
}
httpRequest.Header.Add("robocorp-installation-id", xviper.TrackingIdentity())
for name, value := range request.Headers {
httpRequest.Header.Add(name, value)
}
httpResponse, err := it.client.Do(httpRequest)
if err != nil {
common.Error("http.Do", err)
response.Status = 9002
response.Err = err
return response
}
defer httpResponse.Body.Close()
response.Status = httpResponse.StatusCode
if request.Stream != nil {
io.Copy(request.Stream, httpResponse.Body)
} else {
response.Body, response.Err = ioutil.ReadAll(httpResponse.Body)
}
if common.DebugFlag {
body := "ignore"
if response.Status > 399 {
body = string(response.Body)
}
common.Debug("%v %v %v => %v (%v)", <-common.Identities, method, url, response.Status, body)
}
return response
}
func (it *internalClient) NewRequest(url string) *Request {
return &Request{
Url: url,
Headers: make(map[string]string),
}
}
func (it *internalClient) Get(request *Request) *Response {
return it.does("GET", request)
}
func (it *internalClient) Post(request *Request) *Response {
return it.does("POST", request)
}
func (it *internalClient) Put(request *Request) *Response {
return it.does("PUT", request)
}
func (it *internalClient) Delete(request *Request) *Response {
return it.does("DELETE", request)
}