/// <summary>
 /// Catches any exceptions when making a request and returns an appropriate data error to return in the callback
 /// </summary>
 public static async Task SafeCallApi(Func <Task> action, IBaseCallback callback)
 {
     try
     {
         await action( );
     }
     catch (ApiException e)
     {
         callback.OnDataError(e.StatusCode.ToDataError( ));
     }
     catch (Exception)
     {
         callback.OnDataError(Enums.DataError.Connection);
     }
 }
        public async Task StartSignInProcess( )
        {
            View.ShowSigningIn(true);

#if DEBUG   // Sign in instantly, the local user instance url should already be set.
            await GetTokenAndSignIn( );
#else       // Call the global api for getting the user instance url.
            ContactUserGetResponse contactUserGetResponse = await GetContactUserInstance( );

            if (contactUserGetResponse.IsRequestSuccessful)
            {
                View.SetInstanceUrl(contactUserGetResponse.InstanceUrl);
                await GetTokenAndSignIn( );
            }
            else
            {
                _callback.OnDataError(contactUserGetResponse.DataError);
            }
#endif
        }
 public virtual void HandleResponseForSignIn(BaseResponse response, Action successAction, IBaseCallback callback)
 {
     if (response.IsRequestSuccessful)
     {
         successAction( );
     }
     else
     {
         callback.OnDataError(response.DataError);
     }
 }
 public virtual async Task HandleResponse(BaseResponse response, Action successAction, IBaseCallback callback, IBasePresenter basePresenter)
 {
     if (response.IsRequestSuccessful)
     {
         successAction( );
     }
     else if (response.DataError == Enums.DataError.Connection && RequestHandler.IsDeviceConnectedToInternet( ))
     {
         await CheckIsInstanceUrlChanged(callback, basePresenter);
     }
     else
     {
         callback.OnDataError(response.DataError);
     }
 }
        private async Task CheckIsInstanceUrlChanged(IBaseCallback callback, IBasePresenter basePresenter)
        {
            string storedInstanceUrl = RequestHandler.GetInstanceUrl( );

            ContactUserGet         contactUser        = new ContactUserGet(RequestHandler.GetUserEmail( ), RequestHandler);
            ContactUserGetResponse contactUserRespose = await SafeCallApi(() => _globalSignInApi.GetContactUserInstance(contactUser));

            if (contactUserRespose.IsRequestSuccessful && contactUserRespose.InstanceUrl != storedInstanceUrl)
            {
                //set new url and call injectapi method to use new url
                //RequestHandler.SetInstanceUrl( "http://192.168.1.66:57204/" );
                RequestHandler.SetInstanceUrl(contactUserRespose.InstanceUrl);
                _injectionHandler.InjectApi( );
                await basePresenter.Start( );
            }
            else
            {
                callback.OnDataError(contactUserRespose.DataError);
            }
        }