How to implement timeout & retry for Restangular

Issue

Is there any built-in way into Restangular where I can provide the timeout for ajax/$http calls?

And how can I write a wrapper to Retry by Timeout using Restangular for ajax/$http calls. a factory??

Thanks, leena.

Solution

You can set the default timeout with setDefaultHttpFields in your RestangularProvider’s config. Restangular will internally implement the retry logic, you can see this for yourself by playing with e.g. the iOS network conditioner (Settings -> Developer). However, you need to implement the retry logic by yourself if you need to retry eg. when the timeout has been reached.

You can configure the defaults in your Restangular model, as you can also set options for an individual Restangular provider.
For example, here both SessionRestangular and AuthenticationRestangular both share the settings defined in module.config, but only SessionRestangular will send the “token” and “user_id” in the HTTP headers:

module = angular.module('SessionModel', ['restangular'])

module.config (RestangularProvider) ->
  RestangularProvider.setBaseUrl('https://api.provid.er/v1')

  RestangularProvider.setDefaultHttpFields
    timeout: 10000 # Milliseconds


module.factory 'SessionRestangular', (Restangular, sessionService) ->  
  return Restangular.withConfig (RestangularConfigurer) ->   
    RestangularConfigurer.setDefaultHeaders
      'user_id': sessionService.getUserId(),
      'token': sessionService.getToken()


module.factory 'AuthenticationRestangular', (Restangular) ->
  return Restangular

Answered By – Petrus Repo

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.