// SaveUserToDataStore sends a user to the datastore service to be saved
// 		userWasSaved, err := SaveUserToDataStore(user)
SaveUserToDataStore sends a user to the datastore service to be saved
userWasSaved, err := SaveUserToDataStore(user)
// SaveUserToDataStore sends a user to the datastore service to be saved // userWasSaved err : = SaveUserToDataStore ( user )
func (control Cntr) SaveUserToDataStore(saveUser dtos.SaveUserDTO) (bool, error) {
	targetURL := fmt.Sprintf("http://%s/api/saveuser", os.Getenv("DATASTORE_INSTANCE"))
	jsonObj, err := json.Marshal(saveUser)
	if err != nil {
		return false, commonUtil.MakeErr(err)
	}
	req, err := http.NewRequest("POST", targetURL, bytes.NewBuffer(jsonObj))
	if err != nil {
		return false, err
	}
	req.Close = true
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authentication", os.Getenv("AUTH_KEY"))
	client := &http.Client{}
	maxRetryCount := 3
	successfulRequest := false
	res, err := client.Do(req)
	if err != nil || res.StatusCode != http.StatusOK {
		logMsg := fmt.Sprintf("error from first call to saveuser (%s), retrying now", targetURL)
		configuration.Logger.Info(logMsg,
			zap.String("errorMsg", fmt.Sprint(err)),
			zap.String("request", fmt.Sprintf("%+v", res)),
			zap.String("response", fmt.Sprintf("%+v", req)))
		for i := 0; i < maxRetryCount; i++ {
			req, err := http.NewRequest("POST", targetURL, bytes.NewBuffer(jsonObj))
			if err != nil {
				return false, err
			}
			req.Close = true
			req.Header.Set("Content-Type", "application/json")
			req.Header.Set("Authentication", os.Getenv("AUTH_KEY"))
			res, err = client.Do(req)
			if err == nil && res.StatusCode == http.StatusOK {
				successfulRequest = true
				defer res.Body.Close()
				break
			}
			exponentialBackOffSleepTime := math.Pow(2, float64(i)) * 16
			configuration.Logger.Sugar().Infof("failed to call %s for %s %d times. Sleeping for %d ms", targetURL, saveUser.User.AccDetails.SteamID, i, exponentialBackOffSleepTime)
			time.Sleep(time.Duration(exponentialBackOffSleepTime) * time.Millisecond)
		}
	} else {
		successfulRequest = true
	}
	if !successfulRequest {
		failedAllRetriesErr := fmt.Errorf("failed all retries to %s for steamID: %s", targetURL, saveUser.User.AccDetails.SteamID)
		return false, commonUtil.MakeErr(failedAllRetriesErr)
	}
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return false, commonUtil.MakeErr(err, "failed to readAll for saveUser body")
	}
	APIRes := dtos.GetUserDTO{}
	err = json.Unmarshal(body, &APIRes)
	if err != nil {
		return false, commonUtil.MakeErr(err, fmt.Sprintf("failed to unmarshal SaveUser object: %+v", string(body)))
	}
	return true, nil
}
func ( control Cntr ) SaveUserToDataStore ( saveUser dtos . SaveUserDTO ) ( bool , error ) { targetURL := fmt . Sprintf ( " " , os . Getenv ( " " ) ) jsonObj , err := json . Marshal ( saveUser ) if err != nil { return false , commonUtil . MakeErr ( err ) } req , err := http . NewRequest ( " " , targetURL , bytes . NewBuffer ( jsonObj ) ) if err != nil { return false , err } req . Close = true req . Header . Set ( " " , " " ) req . Header . Set ( " " , os . Getenv ( " " ) ) client := & http . Client { } maxRetryCount := 3 successfulRequest := false res , err := client . Do ( req ) if err != nil || res . StatusCode != http . StatusOK { logMsg := fmt . Sprintf ( " " , targetURL ) configuration . Logger . Info ( logMsg , zap . String ( " " , fmt . Sprint ( err ) ) , zap . String ( " " , fmt . Sprintf ( " " , res ) ) , zap . String ( " " , fmt . Sprintf ( " " , req ) ) ) for i := 0 ; i < maxRetryCount ; i ++ { req , err := http . NewRequest ( " " , targetURL , bytes . NewBuffer ( jsonObj ) ) if err != nil { return false , err } req . Close = true req . Header . Set ( " " , " " ) req . Header . Set ( " " , os . Getenv ( " " ) ) res , err = client . Do ( req ) if err == nil && res . StatusCode == http . StatusOK { successfulRequest = true defer res . Body . Close ( ) break } exponentialBackOffSleepTime := math . Pow ( 2 , float64 ( i ) ) * 16 configuration . Logger . Sugar ( ) . Infof ( " " , targetURL , saveUser . User . AccDetails . SteamID , i , exponentialBackOffSleepTime ) time . Sleep ( time . Duration ( exponentialBackOffSleepTime ) * time . Millisecond ) } } else { successfulRequest = true } if ! successfulRequest { failedAllRetriesErr := fmt . Errorf ( " " , targetURL , saveUser . User . AccDetails . SteamID ) return false , commonUtil . MakeErr ( failedAllRetriesErr ) } body , err := ioutil . ReadAll ( res . Body ) if err != nil { return false , commonUtil . MakeErr ( err , " " ) } APIRes := dtos . GetUserDTO { } err = json . Unmarshal ( body , & APIRes ) if err != nil { return false , commonUtil . MakeErr ( err , fmt . Sprintf ( " " , string ( body ) ) ) } return true , nil }
SaveUserToDataStore sends a user to the datastore service to be saved
userWasSaved, err := SaveUserToDataStore(user)
SaveUserToDataStore sends a user to the datastore service to be saved userWasSaved err : = SaveUserToDataStore ( user )
// Failed after all retries