/// <summary>
 /// Constructor
 /// </summary>
 public PhoneHybridMainPage()
 {
     _instance = this;
     _syncContext = SynchronizationContext.Current;
     _bootConfig = BootConfig.GetBootConfig();
     _loginOptions = new LoginOptions("https://test.salesforce.com" /* FIXME once we have a server picker */,
         _bootConfig.ClientId, _bootConfig.CallbackURL, _bootConfig.Scopes);
     _clientManager = new ClientManager(_loginOptions);
 }
        /// <summary>
        /// Constructor
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            _clientManager = new ClientManager(Config.LoginOptions);
            _buttons = new Button[] { btnVersions, btnResources, btnDescribeGlobal, btnDescribe, btnMetadata, btnCreate, btnRetrieve, btnUpdate, btnUpsert, btnDelete, btnQuery, btnSearch, btnManual, btnLogout };

            foreach (Button button in _buttons)
            {
                button.Click += OnAnyButtonClicked;
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
            _viewModel = DataContext as RestActionViewModel;
            _viewModel.PropertyChanged += OnViewModelPropertyChanged;
            _viewModel.SyncContext = SynchronizationContext.Current;

            _clientManager = new ClientManager(Config.LoginOptions);
            _buttons = new Button[] { btnVersions, btnResources, btnDescribeGlobal, btnDescribe, btnMetadata, btnCreate, btnRetrieve, btnUpdate, btnUpsert, btnDelete, btnQuery, btnSearch, btnManual, btnLogout };

            foreach (Button button in _buttons)
            {
                button.Click += OnAnyButtonClicked;
            }

            SwitchToRestAction(RestAction.VERSIONS);
        }
 /// <summary>
 ///     Create and persist Account for newly authenticated user
 /// </summary>
 /// <param name="loginOptions"></param>
 /// <param name="authResponse"></param>
 public static async Task<Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse)
 {
     PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - create account object", LoggingLevel.Verbose);
     var account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl,
         loginOptions.Scopes,
         authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken);
     account.CommunityId = authResponse.CommunityId;
     account.CommunityUrl = authResponse.CommunityUrl;
     var cm = new ClientManager();
     cm.PeekRestClient();
     IdentityResponse identity = null;
     try
     {
         identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken);
     }
     catch (JsonException ex)
     {
         PlatformAdapter.SendToCustomLogger(
             "AccountManager.CreateNewAccount - Exception occurred when retrieving account identity:",
             LoggingLevel.Critical);
         PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical);
         Debug.WriteLine("Error retrieving account identity");
     }
     if (identity != null)
     {
         account.UserId = identity.UserId;
         account.UserName = identity.UserName;
         account.Policy = identity.MobilePolicy;
         AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account);
     }
     PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - done", LoggingLevel.Verbose);
     return account;
 }
        /// <summary>
        ///     Create and persist Account for newly authenticated user
        /// </summary>
        /// <param name="loginOptions"></param>
        /// <param name="authResponse"></param>
        public static async Task<Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse)
        {
            LoggingService.Log("Create account object", LoggingLevel.Verbose);

            var account = new Account(
                loginOptions.LoginUrl, 
                loginOptions.ClientId, 
                loginOptions.CallbackUrl,
                loginOptions.Scopes,
                authResponse.InstanceUrl, 
                authResponse.IdentityUrl, 
                authResponse.AccessToken, 
                authResponse.RefreshToken)
            {
                CommunityId = authResponse.CommunityId,
                CommunityUrl = authResponse.CommunityUrl
            };

            var cm = new ClientManager();
            cm.PeekRestClient();
            IdentityResponse identity = null;
            try
            {
                identity = await OAuth2.CallIdentityServiceAsync(authResponse.IdentityUrl, authResponse.AccessToken);
            }
            catch (JsonException ex)
            {
                LoggingService.Log("Exception occurred when retrieving account identity:",
                    LoggingLevel.Critical);
                LoggingService.Log(ex, LoggingLevel.Critical);
                Debug.WriteLine("Error retrieving account identity");
            }

            if (identity != null)
            {
                account.UserId = identity.UserId;
                account.UserName = identity.UserName;
                account.Policy = identity.MobilePolicy;
                account.OrganizationId = identity.OrganizationId;

                await AuthStorageHelper.PersistCurrentAccountAsync(account);

                LoggedInAccount = account;
            }
            LoggingService.Log("Finished creating account", LoggingLevel.Verbose);
            return account;
        }
        /// <summary>
        /// Create and persist Account for newly authenticated user
        /// </summary>
        /// <param name="loginOptions"></param>
        /// <param name="authResponse"></param>
        public async static Task<bool> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse)
        {
            Account account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes,
                authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken);
            var cm = new ClientManager();
            var client = cm.PeekRestClient();
            IdentityResponse identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken);

            if (identity != null)
            {
                account.UserId = identity.UserId;
                account.UserName = identity.UserName;
                AuthStorage.PersistCredentials(account);
                return true;
            }
            return false;
        }
 /// <summary>
 /// Execute the command: send the request to the server 
 /// and sets the ReturnedRestResponse property of the view-model upon receiving the response back from the server
 /// </summary>
 /// <param name="parameter"></param>
 public void Execute(object parameter)
 {
     ClientManager cm = new ClientManager(Config.LoginOptions);
     RestClient rc = cm.GetRestClient();
     if (rc != null)
     {
         RestRequest request = BuildRestRequest();
         rc.SendAsync(request, (response) => { _vm.ReturnedRestResponse = response; });
     }
 }
        protected void CreateClientManager(bool reset)
        {
            if (GlobalClientManager == null || reset)
            {
                ServerConfiguration = InitializeConfig();
                GlobalClientManager = new ClientManager();
            }

            GlobalClientManager.GetRestClient();
        }
 public static void ResetClientManager()
 {
     GlobalClientManager = new ClientManager();
 }