Skip to content

Commit e495242

Browse files
kevin-liiKevin Lisureshanaparti
authored
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>
1 parent 5b36954 commit e495242

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

apirunner/apirunner.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"net/http"
3131
"net/url"
3232
"os"
33+
"regexp"
3334
"strconv"
3435
"strings"
3536
"time"
@@ -124,11 +125,16 @@ func executeAPIandCalculate(profileName string, apiURL string, command string, p
124125
var avgTime float64
125126
var totalTime float64
126127
var count float64
128+
getRequestList := map[string]struct{}{"isaccountallowedtocreateofferingswithtags": {}, "readyforshutdown": {}, "cloudianisenabled": {}, "quotabalance": {},
129+
"quotasummary": {}, "quotatarifflist": {}, "quotaisenabled": {}, "quotastatement": {}, "verifyoauthcodeandgetuser": {}}
130+
_, isInGetRequestList := getRequestList[command]
131+
isGetRequest, _ := regexp.MatchString("^(get|list|query|find)(\\w+)+$", command)
132+
127133
if iterations != 1 {
128134
log.Infof("Calling API %s for %d number of iterations with parameters %s", command, iterations, params)
129135
for i := 1; i <= iterations; i++ {
130136
log.Infof("Started with iteration %d for the command %s", i, command)
131-
elapsedTime, apicount, result := executeAPI(apiURL, params)
137+
elapsedTime, apicount, result := executeAPI(apiURL, params, !(isGetRequest || isInGetRequestList))
132138
count = apicount
133139
if elapsedTime < minTime {
134140
minTime = elapsedTime
@@ -145,7 +151,7 @@ func executeAPIandCalculate(profileName string, apiURL string, command string, p
145151
log.Infof("count [%.f] : Time in seconds [Min - %.2f] [Max - %.2f] [Avg - %.2f]\n", count, minTime, maxTime, avgTime)
146152
saveData(apiURL, count, minTime, maxTime, avgTime, page, pagesize, keyword, profileName, command, dbProfile, reportAppend)
147153
} else {
148-
elapsedTime, apicount, _ := executeAPI(apiURL, params)
154+
elapsedTime, apicount, _ := executeAPI(apiURL, params, !(isGetRequest || isInGetRequestList))
149155
log.Infof("Elapsed time [%.2f seconds] for the count [%.0f]", elapsedTime, apicount)
150156
saveData(apiURL, count, elapsedTime, elapsedTime, elapsedTime, page, pagesize, keyword, profileName, command, dbProfile, reportAppend)
151157
}
@@ -251,12 +257,24 @@ func saveData(apiURL string, count float64, minTime float64, maxTime float64, av
251257
log.Info(message)
252258
}
253259

254-
func executeAPI(apiURL string, params url.Values) (float64, float64, bool) {
260+
func executeAPI(apiURL string, params url.Values, postRequest bool) (float64, float64, bool) {
255261
// Send the API request and calculate the time
256-
apiURL = fmt.Sprintf("%s?%s", apiURL, params.Encode())
262+
var resp *http.Response
263+
var err error
257264
log.Infof("Running the API %s", apiURL)
258265
start := time.Now()
259-
resp, err := http.Get(apiURL)
266+
if postRequest {
267+
dataBody := strings.NewReader(params.Encode())
268+
resp, err = http.Post(
269+
apiURL,
270+
"application/x-www-form-urlencoded",
271+
dataBody,
272+
)
273+
} else {
274+
apiURL = fmt.Sprintf("%s?%s", apiURL, params.Encode())
275+
resp, err = http.Get(apiURL)
276+
}
277+
260278
APIscount++
261279
if err != nil {
262280
log.Infof("Error sending API request: %s with error %s\n", apiURL, err)

0 commit comments

Comments
 (0)