Files
PineSript/Buyer/ApiKeyInfo.cs
2026-04-26 19:00:54 +09:00

54 lines
1.9 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Serilog;
namespace Buyer
{
/// <summary>Bitget API 키 정보</summary>
public class ApiKeyInfo
{
public string AccessKey { get; set; } = "";
public string SecretKey { get; set; } = "";
public string PassphraseKey { get; set; } = "";
public string Name { get; set; } = "";
public int Id { get; set; }
}
/// <summary>API 키 JSON 파일 최상위 구조 (SelectAPI 필드 래퍼)</summary>
internal class ApiKeyFile
{
public ApiKeyInfo SelectAPI { get; set; } = new();
}
/// <summary>JSON 파일에서 API 키를 로드합니다.</summary>
public static class ApiKeyLoader
{
public static ApiKeyInfo Load(string filePath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException($"API 키 파일을 찾을 수 없습니다: {filePath}");
string json = File.ReadAllText(filePath);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var file = JsonSerializer.Deserialize<ApiKeyFile>(json, options)
?? throw new InvalidDataException("API 키 파일 파싱 실패");
var key = file.SelectAPI;
if (string.IsNullOrWhiteSpace(key.AccessKey))
throw new InvalidDataException("AccessKey가 비어있습니다");
if (string.IsNullOrWhiteSpace(key.SecretKey))
throw new InvalidDataException("SecretKey가 비어있습니다");
if (string.IsNullOrWhiteSpace(key.PassphraseKey))
throw new InvalidDataException("PassphraseKey가 비어있습니다");
Log.Information("[ApiKey] 로드 완료: Name={Name} Id={Id}", key.Name, key.Id);
return key;
}
}
}