feat : start login after start program, add appsetings
This commit is contained in:
@@ -9,7 +9,7 @@ namespace fubon_api.Controllers;
|
||||
public class FubonSdkAccountController : ControllerBase
|
||||
{
|
||||
private readonly FubonSDK _sdk;
|
||||
private Account _account;
|
||||
private readonly Account _account;
|
||||
private readonly ILogger<FubonSdkAccountController> _logger;
|
||||
|
||||
public FubonSdkAccountController(ILogger<FubonSdkAccountController> logger, FubonSDK sdk, Account account)
|
||||
@@ -19,18 +19,10 @@ public class FubonSdkAccountController : ControllerBase
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost("Login")]
|
||||
public LoginResponse Login([FromBody] LoginRequest request)
|
||||
[HttpGet("Account")]
|
||||
public Account GetAccount()
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new ArgumentException("Invalid login request.");
|
||||
}
|
||||
|
||||
var result = _sdk.Login(request.Id, request.Password, "Your Cert Path", "Your Cert Password");
|
||||
_account = result.data[0];
|
||||
|
||||
return result;
|
||||
return _account;
|
||||
}
|
||||
|
||||
[HttpGet("Inventories")]
|
||||
|
9
Models/FubonSettings.cs
Normal file
9
Models/FubonSettings.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace fubon_api.Models;
|
||||
|
||||
public class FubonSettings
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string CertPath { get; set; }
|
||||
public string CertPassword{ get; set; }
|
||||
}
|
22
Program.cs
22
Program.cs
@@ -1,18 +1,36 @@
|
||||
using fubon_api.Controllers;
|
||||
using fubon_api.Models;
|
||||
using FubonNeo.Sdk;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// appsettings
|
||||
var fubonSettings = builder.Configuration.GetSection("FubonSettings").Get<FubonSettings>();
|
||||
if (fubonSettings == null)
|
||||
{
|
||||
throw new InvalidOperationException("FubonSettings is missing.");
|
||||
}
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSingleton<FubonSDK>();
|
||||
builder.Services.AddSingleton<Account>();
|
||||
builder.Services.AddSingleton<Account>(provider =>
|
||||
{
|
||||
var sdk = provider.GetRequiredService<FubonSDK>();
|
||||
var logger = provider.GetRequiredService<ILogger<Program>>();
|
||||
var loginResponse = sdk.Login(fubonSettings.Id, fubonSettings.Password, fubonSettings.CertPath, fubonSettings.CertPassword);
|
||||
if (loginResponse.isSuccess != true || loginResponse.data[0] == null)
|
||||
{
|
||||
logger.LogError("Login failed.");
|
||||
throw new Exception("Login failed.");
|
||||
}
|
||||
|
||||
return loginResponse.data[0];
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
@@ -4,5 +4,11 @@
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"FubonSettings":{
|
||||
"Id": "UserId",
|
||||
"Password": "Password",
|
||||
"CertPath": "./static",
|
||||
"CertPassword":"CertPassword"
|
||||
}
|
||||
}
|
@@ -5,5 +5,11 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"FubonSettings":{
|
||||
"Id": "UserId",
|
||||
"Password": "Password",
|
||||
"CertPath": "./static",
|
||||
"CertPassword":"CertPassword"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
Reference in New Issue
Block a user