@@ -419,26 +419,6 @@ func (nsxtsess *NsxtSession) newNsxtRequest(verb string, url string, payload io.
419419	return  req , errorResult 
420420}
421421
422- // Helper routines for REST calls. 
423- 
424- func  (nsxtsess  * NsxtSession ) collectCookiesFromResp (resp  * http.Response ) {
425- 	// collect cookies from the resp 
426- 	var  csrfToken  string 
427- 	var  sessionID  string 
428- 	for  _ , cookie  :=  range  resp .Cookies () {
429- 		if  cookie .Name  ==  "csrftoken"  {
430- 			csrfToken  =  cookie .Value 
431- 		}
432- 		if  cookie .Name  ==  "sessionid"  ||  cookie .Name  ==  "nsxt-sessionid"  {
433- 			sessionID  =  cookie .Value 
434- 		}
435- 	}
436- 	if  csrfToken  !=  ""  &&  sessionID  !=  ""  {
437- 		nsxtsess .csrfToken  =  csrfToken 
438- 		nsxtsess .sessionid  =  sessionID 
439- 	}
440- }
441- 
442422// RestRequest exports restRequest from the SDK 
443423// Returns http.Response for accessing the whole http Response struct including headers and response body 
444424func  (nsxtsess  * NsxtSession ) RestRequest (verb  string , uri  string , payload  interface {}, lastError  error ,
@@ -454,10 +434,7 @@ func (nsxtsess *NsxtSession) restRequest(verb string, uri string, payload interf
454434	url  :=  nsxtsess .prefix  +  uri 
455435
456436	// If optional retryNum arg is provided, then count which retry number this is 
457- 	retry  :=  0 
458- 	if  len (retryNum ) >  0  {
459- 		retry  =  retryNum [0 ]
460- 	}
437+ 	maxAPIRetries  :=  nsxtsess .maxAPIRetries 
461438
462439	var  payloadIO  io.Reader 
463440	if  payload  !=  nil  {
@@ -487,23 +464,18 @@ func (nsxtsess *NsxtSession) restRequest(verb string, uri string, payload interf
487464		retryReq  =  true 
488465	}
489466
490- 	if  ! retryReq  {
491- 		glog .Infof ("Req for %s uri %v  RespCode %v" , verb , url , resp .StatusCode )
492- 		errorResult .HTTPStatusCode  =  resp .StatusCode 
493- 
494- 		if  uri  ==  "login"  {
495- 			nsxtsess .collectCookiesFromResp (resp )
496- 		}
497- 		if  resp .StatusCode  ==  401  &&  uri  !=  "login"  {
498- 			resp .Body .Close ()
499- 			glog .Infof ("Retrying url %s; retry %d due to Status Code %d" , url , retry , resp .StatusCode )
500- 			err  :=  nsxtsess .initiateSession ()
501- 			if  err  !=  nil  {
502- 				return  nil , err 
467+ 	if  retryReq  {
468+ 		for  i  :=  0 ; i  <  maxAPIRetries ; i ++  {
469+ 			glog .Infof ("Retrying url %s; retry %d due to Status Code %d" , url , i , resp .StatusCode )
470+ 			glog .Infof ("Req for %s uri %v  RespCode %v" , verb , url , resp .StatusCode )
471+ 			errorResult .HTTPStatusCode  =  resp .StatusCode 
472+ 			if  resp .StatusCode  !=  200  {
473+ 				resp , err  :=  nsxtsess .client .Do (req )
474+ 				if  err  ==  nil  &&  resp  !=  nil  {
475+ 					break 
476+ 				}
503477			}
504- 		} else  if  resp .StatusCode  ==  419  ||  (resp .StatusCode  >=  500  &&  resp .StatusCode  <  599 ) {
505- 			resp .Body .Close ()
506- 			glog .Infof ("Retrying url: %s; retry: %d due to Status Code %d" , url , retry , resp .StatusCode )
478+ 			time .Sleep (time .Duration (nsxtsess .apiRetryInterval ) *  time .Millisecond )
507479		}
508480	}
509481	return  resp , nil 
0 commit comments