Skip to content

Commit

Permalink
extract request execution
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanHoogland committed Nov 6, 2023
1 parent 9f81008 commit 35ed628
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions cmd/network.go
Expand Up @@ -228,17 +228,9 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
config.Debug("NewAPIRequest API request URL:", requestURL)

var response *http.Response
if params.Has("password") || params.Has("userdata") {
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
response, err = r.Client().PostForm(requestURL, params)
if err != nil {
return nil, err
}
} else {
response, err = r.Client().Get(requestURL)
if err != nil {
return nil, err
}
response,err = executeRequest(r, requestURL, params)
if (err != nil) {
return nil, err
}
config.Debug("NewAPIRequest response status code:", response.StatusCode)

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

if params.Has("password") || params.Has("userdata") {
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
response, err = r.Client().PostForm(requestURL, params)
if err != nil {
return nil, err
}
} else {
response, err = r.Client().Get(requestURL)
if err != nil {
return nil, err
}
response,err = executeRequest(r, requestURL, params)
if (err != nil) {
return nil, err
}
}

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

return nil, errors.New("failed to decode response")
}

// we can implement further conditions to do POST or GET (or other http commands) here
func executeRequest(r *Request, requestURL string, params url.Values) (*http.Response, error){
if params.Has("password") || params.Has("userdata") {
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
return r.Client().PostForm(requestURL, params)
} else {
return r.Client().Get(requestURL)
}
}

0 comments on commit 35ed628

Please sign in to comment.