-
Notifications
You must be signed in to change notification settings - Fork 0
/
octoclient.go
203 lines (173 loc) · 4.85 KB
/
octoclient.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package octoclient
import (
"bytes"
"context"
"io/ioutil"
"mime/multipart"
"net/http"
"github.com/google/uuid"
)
const (
apiEndpoint = "/service/invoke"
apiEndpointFile = "/service/invoke-file"
method = "POST"
contentType = "application/json"
)
/*
Usage:
- Create object of OctoConfig with clientID/access-token, baseURL of Octopus & others
- Create instance of Octo-Client once using this object
- call the service-invoke using the payload.
- The other features like pathParams will be included in payload
*/
type OctoQueryParam struct {
Key string `json:"key"`
Value string `json:"value"`
}
type OctoURLParam struct {
Key string `json:"key"`
Value string `json:"value"`
}
type OctoHeader struct {
Key string `json:"key"`
Value string `json:"value"`
}
type OctoPayload struct {
ServiceID string `json:"serviceID"`
QueryParams []OctoQueryParam `json:"queryParameters"`
DynamicURLParams []OctoURLParam `json:"dynamicURLParams"`
DynamicHeaders []OctoHeader `json:"dynamicHeaders"`
Data map[string]interface{} `json:"data"`
RequestID string `json:"requestID"` // Acts as unique identifier for each request.
Switch OctoVendorSwitch `json:"vendorSwitch"`
}
type OctoFileField struct {
FieldName string
FilePath string
}
type OctoTextField struct {
FieldName string
FieldValue string
}
type OctoPayloadForm struct {
ServiceID string `json:"serviceID"`
TextFields []OctoTextField `json:"textFields"`
FileFields []OctoFileField `json:"fileFields"`
}
type OctoResponse struct {
Message string `json:"msg"`
RequestID uuid.UUID `json:"requestId"`
Data map[string]interface{} `json:"data"`
}
type OctoClient struct {
HTTPClient *http.Client
baseURL string
token string
authorization string
}
type OctoVendorSwitch struct {
Enabled bool `json:"enabled"`
VendorConfigID string `json:"vendorConfigId"`
}
type Options struct {
// Other options in http.Client will be added here e.g, custom timeout
BaseURL string
Token string // Use AccessToken in place of clientID
Authorization string // Auth Token
}
func New(options Options) *OctoClient {
baseURL := trimTrailingSlash(options.BaseURL)
return &OctoClient{
HTTPClient: &http.Client{},
baseURL: baseURL,
token: options.Token,
authorization: options.Authorization,
}
}
func (o *OctoClient) getHttpClient() http.Client {
return *o.HTTPClient
}
func (o *OctoClient) ServiceInvoke(ctx context.Context, payload OctoPayload) (*OctoResponse, error) {
callingUrl := o.baseURL + apiEndpoint
var response OctoResponse
finalPayload, err := ConvertStructToJSON(payload)
if err != nil {
return nil, err
}
var req *http.Request
if ctx == nil {
req, err = http.NewRequestWithContext(context.TODO(), method, callingUrl, finalPayload)
} else {
req, err = http.NewRequestWithContext(ctx, method, callingUrl, finalPayload)
}
if err != nil {
return nil, err
}
req.Header.Add("clientId", o.token)
req.Header.Add("Content-Type", contentType)
req.Header.Add("Authorization", o.authorization)
res, err := o.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
// TODO: Handling if return type !json
response, err = ConvertByteToStruct(body)
if err != nil {
return nil, err
}
return &response, nil
}
func (o *OctoClient) ServiceInvokeForm(ctx context.Context, payload OctoPayloadForm) (*OctoResponse, error) {
callingUrl := o.baseURL + apiEndpointFile
var requestBody bytes.Buffer
multiPartWriter := multipart.NewWriter(&requestBody)
err := multiPartWriter.WriteField("serviceID", payload.ServiceID)
if err != nil {
return nil, err
}
err = processTextFields(payload.TextFields, multiPartWriter)
if err != nil {
return nil, err
}
err = processFileFields(payload.FileFields, multiPartWriter)
if err != nil {
return nil, err
}
err = multiPartWriter.Close()
if err != nil {
return nil, err
}
var response OctoResponse
var req *http.Request
if ctx == nil {
req, err = http.NewRequestWithContext(context.TODO(), method, callingUrl, &requestBody)
} else {
req, err = http.NewRequestWithContext(ctx, method, callingUrl, &requestBody)
}
if err != nil {
return nil, err
}
req.Header.Add("clientId", o.token)
req.Header.Add("Content-Type", "multipart/form-data; boundary="+multiPartWriter.Boundary())
req.Header.Add("Authorization", o.authorization)
res, err := o.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
// TODO: Handling if return type !json
response, err = ConvertByteToStruct(body)
if err != nil {
return nil, err
}
return &response, nil
}