Changing executeAPI to utilize POST request (#18)
* Changing executeAPI to utilize POST request
* Adding changes to accomadate GET requestS
* Code Formatting Fix
* Adding last changes
* Update apirunner/apirunner.go
* Apply suggestions from code review
* Update apirunner/apirunner.go
---------
Co-authored-by: Kevin Li <kli74@apple.com>
Co-authored-by: Suresh Kumar Anaparti <sureshkumar.anaparti@gmail.com>
diff --git a/apirunner/apirunner.go b/apirunner/apirunner.go
index 8a3987c..5767b7c 100644
--- a/apirunner/apirunner.go
+++ b/apirunner/apirunner.go
@@ -30,6 +30,7 @@
"net/http"
"net/url"
"os"
+ "regexp"
"strconv"
"strings"
"time"
@@ -124,11 +125,16 @@
var avgTime float64
var totalTime float64
var count float64
+ getRequestList := map[string]struct{}{"isaccountallowedtocreateofferingswithtags": {}, "readyforshutdown": {}, "cloudianisenabled": {}, "quotabalance": {},
+ "quotasummary": {}, "quotatarifflist": {}, "quotaisenabled": {}, "quotastatement": {}, "verifyoauthcodeandgetuser": {}}
+ _, isInGetRequestList := getRequestList[command]
+ isGetRequest, _ := regexp.MatchString("^(get|list|query|find)(\\w+)+$", command)
+
if iterations != 1 {
log.Infof("Calling API %s for %d number of iterations with parameters %s", command, iterations, params)
for i := 1; i <= iterations; i++ {
log.Infof("Started with iteration %d for the command %s", i, command)
- elapsedTime, apicount, result := executeAPI(apiURL, params)
+ elapsedTime, apicount, result := executeAPI(apiURL, params, !(isGetRequest || isInGetRequestList))
count = apicount
if elapsedTime < minTime {
minTime = elapsedTime
@@ -145,7 +151,7 @@
log.Infof("count [%.f] : Time in seconds [Min - %.2f] [Max - %.2f] [Avg - %.2f]\n", count, minTime, maxTime, avgTime)
saveData(apiURL, count, minTime, maxTime, avgTime, page, pagesize, keyword, profileName, command, dbProfile, reportAppend)
} else {
- elapsedTime, apicount, _ := executeAPI(apiURL, params)
+ elapsedTime, apicount, _ := executeAPI(apiURL, params, !(isGetRequest || isInGetRequestList))
log.Infof("Elapsed time [%.2f seconds] for the count [%.0f]", elapsedTime, apicount)
saveData(apiURL, count, elapsedTime, elapsedTime, elapsedTime, page, pagesize, keyword, profileName, command, dbProfile, reportAppend)
}
@@ -251,12 +257,24 @@
log.Info(message)
}
-func executeAPI(apiURL string, params url.Values) (float64, float64, bool) {
+func executeAPI(apiURL string, params url.Values, postRequest bool) (float64, float64, bool) {
// Send the API request and calculate the time
- apiURL = fmt.Sprintf("%s?%s", apiURL, params.Encode())
+ var resp *http.Response
+ var err error
log.Infof("Running the API %s", apiURL)
start := time.Now()
- resp, err := http.Get(apiURL)
+ if postRequest {
+ dataBody := strings.NewReader(params.Encode())
+ resp, err = http.Post(
+ apiURL,
+ "application/x-www-form-urlencoded",
+ dataBody,
+ )
+ } else {
+ apiURL = fmt.Sprintf("%s?%s", apiURL, params.Encode())
+ resp, err = http.Get(apiURL)
+ }
+
APIscount++
if err != nil {
log.Infof("Error sending API request: %s with error %s\n", apiURL, err)