@page "/rest-discovery" @using DataConnection.REST.Configuration @using DataConnection.REST.Implementations @using DataConnection.REST.Models @using System.Net.Http @inject IHttpClientFactory HttpClientFactory

REST Service Discovery

@if (SelectedServiceType == "SAPB1") {
SAP Business One Connection Details
} @if (!string.IsNullOrEmpty(StatusMessage)) { } @if (DiscoveredEntities.Any()) {

Discovered Entities

} @code { private string SelectedServiceType { get; set; } = ""; private SapB1ConnectionOptions SapB1Options { get; set; } = new SapB1ConnectionOptions(); private List DiscoveredEntities { get; set; } = new List(); private bool IsLoading { get; set; } = false; private string? StatusMessage { get; set; } private string StatusMessageClass => !string.IsNullOrEmpty(StatusMessage) && StatusMessage.StartsWith("Error") ? "alert-danger" : "alert-success"; // Helper class to hold SAP B1 specific inputs private class SapB1ConnectionOptions { public string? BaseUrl { get; set; } public string? CompanyDb { get; set; } public string? Username { get; set; } public string? Password { get; set; } public bool IgnoreSslErrors { get; set; } } private async Task HandleConnectClick() { IsLoading = true; StatusMessage = null; DiscoveredEntities.Clear(); StateHasChanged(); // Update UI to show loading if (SelectedServiceType == "SAPB1") { if (string.IsNullOrWhiteSpace(SapB1Options.BaseUrl) || string.IsNullOrWhiteSpace(SapB1Options.CompanyDb) || string.IsNullOrWhiteSpace(SapB1Options.Username) || string.IsNullOrWhiteSpace(SapB1Options.Password)) { StatusMessage = "Error: Please fill in all SAP Business One connection details."; IsLoading = false; StateHasChanged(); return; } try { var restOptions = new RestServiceOptions { BaseUrl = SapB1Options.BaseUrl, IgnoreSslErrors = SapB1Options.IgnoreSslErrors // Username/Password are not set here as SapB1ServiceClient handles them in LoginAsync }; // SapB1ServiceClient manages its own HttpClient internally for cookie handling // We don't inject HttpClient directly into it via constructor in this setup. // The BaseRestServiceClient constructor receives an HttpClient, but SapB1ServiceClient // creates its own specific one in CreateConfiguredHttpClient. // We pass the factory-created client to the base, but the SapB1 specific logic uses its own. // This seems slightly complex, might need refactoring later, but follows current implementation. // Let's simplify: Create the client directly here for now. var client = new SapB1ServiceClient(restOptions); StatusMessage = "Attempting login..."; StateHasChanged(); bool loggedIn = await client.LoginAsync(SapB1Options.CompanyDb, SapB1Options.Username, SapB1Options.Password); if (loggedIn) { StatusMessage = "Login successful. Discovering entities..."; StateHasChanged(); DiscoveredEntities = await client.DiscoverEntitiesAsync(); if (DiscoveredEntities.Any()) { StatusMessage = $"Discovery complete. Found {DiscoveredEntities.Count} entities."; } else { StatusMessage = "Login successful, but failed to discover entities or no entities found."; } // Optional: Logout after discovery if desired // await client.LogoutAsync(); } else { StatusMessage = "Error: SAP B1 Login failed. Check credentials and Service Layer status."; } } catch (Exception ex) { // Log the full exception details somewhere appropriate Console.WriteLine($"Error during SAP B1 connection/discovery: {ex}"); StatusMessage = $"Error: An exception occurred: {ex.Message}"; } finally { IsLoading = false; StateHasChanged(); // Update UI after completion/error } } else { StatusMessage = "Error: Please select a service type."; IsLoading = false; StateHasChanged(); } } }