diff --git a/AAIntegration.SimmonsBank.API/Configs/PuppeteerConfig.cs b/AAIntegration.SimmonsBank.API/Configs/PuppeteerConfig.cs index f43ce55..5881e6a 100644 --- a/AAIntegration.SimmonsBank.API/Configs/PuppeteerConfig.cs +++ b/AAIntegration.SimmonsBank.API/Configs/PuppeteerConfig.cs @@ -7,6 +7,7 @@ public class PuppeteerConfig public int TaskCheckIntervalMinutes { get; set; } public string SimmonsBankBaseUrl { get; set; } public int BrowserOperationTimeoutSeconds { get; set; } + public bool Headless { get; set; } } public class PuppeteerConfigConstants @@ -16,6 +17,7 @@ public class PuppeteerConfigConstants public const string TaskCheckIntervalMinutes = "TaskCheckIntervalMinutes"; public const string SimmonsBankBaseUrl = "SimmonsBankBaseUrl"; public const string BrowserOperationTimeoutSeconds = "BrowserOperationTimeoutSeconds"; + public const string Headless = "Headless"; } public class PuppeteerConstants diff --git a/AAIntegration.SimmonsBank.API/Controllers/AccountController.cs b/AAIntegration.SimmonsBank.API/Controllers/AccountController.cs index 6ec6680..bcb047e 100644 --- a/AAIntegration.SimmonsBank.API/Controllers/AccountController.cs +++ b/AAIntegration.SimmonsBank.API/Controllers/AccountController.cs @@ -10,6 +10,8 @@ using System.Collections.Generic; using AAIntegration.SimmonsBank.API.Entities; using Microsoft.AspNetCore.Authorization; using System.Security.Claims; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; [Authorize] [ApiController] @@ -21,30 +23,36 @@ public class AccountsController : ControllerBase private IMapper _mapper; private readonly AppSettings _appSettings; private readonly ILogger _logger; + private IPuppeteerService _puppeteerService; public AccountsController( IAccountService accountService, IUserService userService, IMapper mapper, IOptions appSettings, - ILogger logger) + ILogger logger, + IPuppeteerService puppeteerService) { _accountService = accountService; _userService = userService; _mapper = mapper; _appSettings = appSettings.Value; _logger = logger; + _puppeteerService = puppeteerService; } [HttpGet] - public IActionResult GetAll() + public async Task GetAllAsync() { - List accountDtos = new List(); + List accounts = await _puppeteerService.GetAccounts(_userService.GetUser(User.FindFirstValue(ClaimTypes.NameIdentifier))); + + return Ok(accounts); + /*List accountDtos = new List(); foreach (Account acc in _accountService.GetAll(GetCurrentUserId())) accountDtos.Add(_mapper.Map(acc)); - return Ok(accountDtos); + return Ok(accountDtos);*/ } [HttpGet("{id}")] diff --git a/AAIntegration.SimmonsBank.API/Models/Accounts/AccountDTO.cs b/AAIntegration.SimmonsBank.API/Models/Accounts/AccountDTO.cs index 91278bb..c20e588 100644 --- a/AAIntegration.SimmonsBank.API/Models/Accounts/AccountDTO.cs +++ b/AAIntegration.SimmonsBank.API/Models/Accounts/AccountDTO.cs @@ -7,8 +7,9 @@ namespace AAIntegration.SimmonsBank.API.Models.Accounts; public class AccountDTO { - public int Id { get; set; } + public string Id { get; set; } public string Name { get; set; } - public decimal Balance { get; set; } - public string ExternalAccountNumber { get; set; } + public string Numbers { get; set; } + public decimal? Balance { get; set; } + public decimal? AvailableBalance { get; set; } } \ No newline at end of file diff --git a/AAIntegration.SimmonsBank.API/Services/PuppeteerService.cs b/AAIntegration.SimmonsBank.API/Services/PuppeteerService.cs index 48dd8f2..f158ea0 100644 --- a/AAIntegration.SimmonsBank.API/Services/PuppeteerService.cs +++ b/AAIntegration.SimmonsBank.API/Services/PuppeteerService.cs @@ -23,12 +23,13 @@ using Newtonsoft.Json.Linq; using NuGet.Protocol; using Microsoft.Extensions.Logging; using NuGet.Protocol.Core.Types; +using AAIntegration.SimmonsBank.API.Models.Accounts; public interface IPuppeteerService { Task Login(User user, CancellationToken cancellationToken); Task IsLoggedIn(User user, CancellationToken cancellationToken); - Task GetAccounts(User user, CancellationToken cancellationToken); + Task> GetAccounts(User user); } public class PuppeteerService : IPuppeteerService @@ -222,7 +223,7 @@ public class PuppeteerService : IPuppeteerService } - public async Task GetAccounts(User user, CancellationToken cancellationToken) + public async Task> GetAccounts(User user) { string prefix = $"Task::GetAccounts - {user.Id} - "; @@ -235,7 +236,7 @@ public class PuppeteerService : IPuppeteerService } // Setup Page - IBrowser browser = await GetUserBrowserAsync(user, cancellationToken); + IBrowser browser = await GetUserBrowserAsync(user, new CancellationToken()); await using IPage page = await browser.NewPageAsync(); await page.SetUserAgentAsync(USER_AGENT); await page.SetViewportAsync(new ViewPortOptions { Width = 1200, Height = 720 }); @@ -245,11 +246,14 @@ public class PuppeteerService : IPuppeteerService try { - IResponse response = await page.GoToAsync(url).WaitAsync(TimeSpan.FromSeconds(_config.BrowserOperationTimeoutSeconds), cancellationToken); + IResponse response = await page.GoToAsync(url).WaitAsync(TimeSpan.FromSeconds(_config.BrowserOperationTimeoutSeconds)); _logger.LogInformation(prefix + $"Request response code '{response.Status}'. Url: '{url}'"); if (response.Status == System.Net.HttpStatusCode.OK) - return await response.JsonAsync(); + { + JToken accounts = await response.JsonAsync(); + return accounts.SelectToken("accounts").ToObject>(); + } else _logger.LogError(prefix + $"Received unexpected status code '{response.Status}'"); } @@ -280,7 +284,7 @@ public class PuppeteerService : IPuppeteerService await browserFetcher.DownloadAsync().WaitAsync(TimeSpan.FromSeconds(_config.BrowserOperationTimeoutSeconds * 20), cancellationToken); var options = new LaunchOptions { - Headless = true, + Headless = _config.Headless, IgnoreHTTPSErrors = true, }; diff --git a/AAIntegration.SimmonsBank.API/appsettings.json b/AAIntegration.SimmonsBank.API/appsettings.json index f7948a2..c9ca35f 100644 --- a/AAIntegration.SimmonsBank.API/appsettings.json +++ b/AAIntegration.SimmonsBank.API/appsettings.json @@ -26,6 +26,7 @@ "APIUrl": "https://localhost:7260" }, "Puppeteer": { + "Headless": true, "BrowserOperationTimeoutSeconds": 5, "TaskCheckIntervalMinutes": 1, "KeepAliveIntervalMinutes": 1, diff --git a/README.md b/README.md index e4f0f5a..0c1cda9 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ The type is Transaction Importer, specifically created for interfacing with Simm [Otp.NET library](https://github.com/kspearrin/Otp.NET) +[JToken.SelectToken() quick reference](https://www.newtonsoft.com/json/help/html/SelectToken.htm) + ## Dev Environment Setup On Archlinux install the following to use dotnet: ```sudo pacman -Sy dotnet-sdk dotnet-runtime aspnet-runtime```.