public async Task <ActionResult> OAuthCallback(string code, string state) { // verify anti-CSRF state token matches what was sent string expectedState = Session["state"] as string; if (!string.Equals(expectedState, state)) { throw new SecurityException("State mismatch"); } Session.Remove("state"); // exchange authorization code for access token AccessToken accessToken = await _mondoAuthorizationClient.GetAccessTokenAsync(code, Url.Action("OAuthCallback", "Home", null, Request.Url.Scheme)); // fetch transactions etc using (var client = new MondoClient(accessToken.Value, "https://production-api.gmon.io")) { IList <Account> accounts = await client.GetAccountsAsync(); Balance balance = await client.GetBalanceAsync(accounts[0].Id); IList <Transaction> transactions = await client.GetTransactionsAsync(accounts[0].Id, expand : "merchant"); return(View(new AccountSummaryModel { Account = accounts[0], Balance = balance, Transactions = transactions })); } }
private async void Login() { try { if (string.IsNullOrWhiteSpace(_loginPageViewModel.Username) || string.IsNullOrWhiteSpace(_loginPageViewModel.Password)) { _loginPageViewModel.ErrorMessage = "Please enter your username and password."; return; } _loginPageViewModel.ErrorMessage = null; _loginPageViewModel.IsEnabled = false; _loginPageViewModel.IsBusy = true; _loginPageViewModel.StatusText = "Authenticating..."; _loginPageViewModel.AccessToken = await _mondoAuthorizationClient.AuthenticateAsync(_loginPageViewModel.Username, _loginPageViewModel.Password); using (var mondoClient = new MondoClient(_loginPageViewModel.AccessToken.Value, "https://production-api.gmon.io")) { ScheduleAccessTokenRefresh(); _loginPageViewModel.StatusText = "Fetching accounts..."; IList <Account> accounts = await mondoClient.GetAccountsAsync(); _loginPageViewModel.StatusText = "Fetching balance..."; Balance balance = await mondoClient.GetBalanceAsync(accounts[0].Id); _loginPageViewModel.StatusText = "Fetching transactions..."; IList <Transaction> transactions = await mondoClient.GetTransactionsAsync(accounts[0].Id, expand : "merchant"); _accountPageViewModel.AccountName = accounts[0].Description; _accountPageViewModel.Balance = balance.Value / 100m; _accountPageViewModel.SpentToday = Math.Abs(balance.SpendToday / 100m); foreach (Transaction transaction in transactions.OrderByDescending(t => t.Created)) { var transactionViewModel = new TransactionViewModel(); transactionViewModel.Amount = transaction.Amount / 100m; transactionViewModel.ImageUrl = transaction.Merchant?.Logo; transactionViewModel.Description = transaction.Merchant?.Name ?? transaction.Description; _accountPageViewModel.Transactions.Add(transactionViewModel); } _navigationService.NavigateTo <AccountSummaryPage>(_accountPageViewModel); _loginPageViewModel.Password = null; _loginPageViewModel.Username = null; } } catch (Exception ex) { _loginPageViewModel.ErrorMessage = ex.Message; } finally { _loginPageViewModel.StatusText = null; _loginPageViewModel.IsBusy = false; _loginPageViewModel.IsEnabled = true; } }
public async void GetTransactions() { using (var server = TestServer.Create(app => { app.Run(async context => { // workaround for mono bug Assert.That(context.Request.Uri.PathAndQuery, Is.EqualTo("/transactions?account_id=1")); Assert.AreEqual("Bearer testAccessToken", context.Request.Headers["Authorization"]); await context.Response.WriteAsync( @"{ 'transactions': [ { 'account_balance': 13013, 'amount': -510, 'created': '2015-08-22T12:20:18Z', 'currency': 'GBP', 'description': 'THE DE BEAUVOIR DELI C LONDON GBR', 'id': 'tx_00008zIcpb1TB4yeIFXMzx', 'merchant': 'merch_00008zIcpbAKe8shBxXUtl', 'metadata': {}, 'notes': 'Salmon sandwich 🍞', 'is_load': false, 'settled': true, 'category': 'eating_out' }, { 'account_balance': 12334, 'amount': -679, 'created': '2015-08-23T16:15:03Z', 'currency': 'GBP', 'description': 'VUE BSL LTD ISLINGTON GBR', 'id': 'tx_00008zL2INM3xZ41THuRF3', 'merchant': 'merch_00008z6uFVhVBcaZzSQwCX', 'metadata': {}, 'notes': '', 'is_load': false, 'settled': true, 'category': 'eating_out' }, ] }" ); }); })) { using (var client = new MondoClient(server.HttpClient, "testAccessToken")) { var transactions = await client.GetTransactionsAsync("1"); Assert.AreEqual(2, transactions.Count); Assert.AreEqual(13013, transactions[0].AccountBalance); Assert.AreEqual(-510, transactions[0].Amount); Assert.AreEqual(new DateTime(2015, 08, 22, 12, 20, 18, DateTimeKind.Utc), transactions[0].Created); Assert.AreEqual("GBP", transactions[0].Currency); Assert.AreEqual("THE DE BEAUVOIR DELI C LONDON GBR", transactions[0].Description); Assert.AreEqual("tx_00008zIcpb1TB4yeIFXMzx", transactions[0].Id); Assert.AreEqual("merch_00008zIcpbAKe8shBxXUtl", transactions[0].Merchant.Id); Assert.AreEqual(new Dictionary<string, string>(), transactions[0].Metadata); Assert.AreEqual("Salmon sandwich 🍞", transactions[0].Notes); Assert.IsFalse(transactions[0].IsLoad); Assert.AreEqual("eating_out", transactions[0].Category); } } }
public async void GetTransactions() { using (var server = TestServer.Create(app => { app.Run(async context => { // workaround for mono bug Assert.That(context.Request.Uri.PathAndQuery, Is.EqualTo("/transactions?account_id=1")); Assert.AreEqual("Bearer testAccessToken", context.Request.Headers["Authorization"]); await context.Response.WriteAsync( @"{ 'transactions': [ { 'account_balance': 13013, 'amount': -510, 'created': '2015-08-22T12:20:18Z', 'currency': 'GBP', 'description': 'THE DE BEAUVOIR DELI C LONDON GBR', 'id': 'tx_00008zIcpb1TB4yeIFXMzx', 'merchant': 'merch_00008zIcpbAKe8shBxXUtl', 'metadata': {}, 'notes': 'Salmon sandwich 🍞', 'is_load': false, 'settled': true, 'category': 'eating_out' }, { 'account_balance': 12334, 'amount': -679, 'created': '2015-08-23T16:15:03Z', 'currency': 'GBP', 'description': 'VUE BSL LTD ISLINGTON GBR', 'id': 'tx_00008zL2INM3xZ41THuRF3', 'merchant': 'merch_00008z6uFVhVBcaZzSQwCX', 'metadata': {}, 'notes': '', 'is_load': false, 'settled': true, 'category': 'eating_out' }, ] }" ); }); })) { using (var client = new MondoClient(server.HttpClient, "testAccessToken")) { var transactions = await client.GetTransactionsAsync("1"); Assert.AreEqual(2, transactions.Count); Assert.AreEqual(13013, transactions[0].AccountBalance); Assert.AreEqual(-510, transactions[0].Amount); Assert.AreEqual(new DateTime(2015, 08, 22, 12, 20, 18, DateTimeKind.Utc), transactions[0].Created); Assert.AreEqual("GBP", transactions[0].Currency); Assert.AreEqual("THE DE BEAUVOIR DELI C LONDON GBR", transactions[0].Description); Assert.AreEqual("tx_00008zIcpb1TB4yeIFXMzx", transactions[0].Id); Assert.AreEqual("merch_00008zIcpbAKe8shBxXUtl", transactions[0].Merchant.Id); Assert.AreEqual(new Dictionary <string, string>(), transactions[0].Metadata); Assert.AreEqual("Salmon sandwich 🍞", transactions[0].Notes); Assert.IsFalse(transactions[0].IsLoad); Assert.AreEqual("eating_out", transactions[0].Category); } } }