Skip to content

Commit 35ed628

Browse files
committed
extract request execution
1 parent 9f81008 commit 35ed628

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

cmd/network.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,9 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
228228
config.Debug("NewAPIRequest API request URL:", requestURL)
229229

230230
var response *http.Response
231-
if params.Has("password") || params.Has("userdata") {
232-
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
233-
response, err = r.Client().PostForm(requestURL, params)
234-
if err != nil {
235-
return nil, err
236-
}
237-
} else {
238-
response, err = r.Client().Get(requestURL)
239-
if err != nil {
240-
return nil, err
241-
}
231+
response,err = executeRequest(r, requestURL, params)
232+
if (err != nil) {
233+
return nil, err
242234
}
243235
config.Debug("NewAPIRequest response status code:", response.StatusCode)
244236

@@ -253,17 +245,9 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
253245
requestURL = fmt.Sprintf("%s?%s", r.Config.ActiveProfile.URL, encodeRequestParams(params))
254246
config.Debug("NewAPIRequest API request URL:", requestURL)
255247

256-
if params.Has("password") || params.Has("userdata") {
257-
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
258-
response, err = r.Client().PostForm(requestURL, params)
259-
if err != nil {
260-
return nil, err
261-
}
262-
} else {
263-
response, err = r.Client().Get(requestURL)
264-
if err != nil {
265-
return nil, err
266-
}
248+
response,err = executeRequest(r, requestURL, params)
249+
if (err != nil) {
250+
return nil, err
267251
}
268252
}
269253

@@ -289,3 +273,13 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
289273

290274
return nil, errors.New("failed to decode response")
291275
}
276+
277+
// we can implement further conditions to do POST or GET (or other http commands) here
278+
func executeRequest(r *Request, requestURL string, params url.Values) (*http.Response, error){
279+
if params.Has("password") || params.Has("userdata") {
280+
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
281+
return r.Client().PostForm(requestURL, params)
282+
} else {
283+
return r.Client().Get(requestURL)
284+
}
285+
}

0 commit comments

Comments
 (0)