From 899a563aac4a754f49f4dbe8743a29acd1d9a3c8 Mon Sep 17 00:00:00 2001 From: "Seonkyu.kim" Date: Tue, 27 May 2025 13:24:47 +0900 Subject: [PATCH] =?UTF-8?q?[=E2=9C=A8]=20=EC=BF=A0=ED=82=A4=20=EB=8F=99?= =?UTF-8?q?=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 쿠키 추가, 불러오기 2. API 읽어오기 3. Header 값 쿠키 저장 하기 --- App.razor.cs | 33 ++++++++++++++++++------------- Program.cs | 1 + Program/Models/APIResponse.cs | 32 ++++++++++++++++++++++++++++++ Program/Models/AppModel.cs | 6 ++++++ Program/Services/APIService.cs | 19 ++++++++++++++++++ Program/Services/CookieService.cs | 24 ++++++++++++++++++++++ wwwroot/index.html | 9 ++++----- 7 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 Program/Models/APIResponse.cs create mode 100644 Program/Models/AppModel.cs create mode 100644 Program/Services/CookieService.cs diff --git a/App.razor.cs b/App.razor.cs index 9e6d168..71af80f 100644 --- a/App.razor.cs +++ b/App.razor.cs @@ -3,35 +3,40 @@ using System.Net.Http.Json; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; +using Front.Program.Models; +using Front.Program.Services; + namespace Front; public partial class App : ComponentBase { - [Inject] HttpClient Http { get; set; } = default!; - [Inject] IJSRuntime JS { get; set; } = default!; + // [Inject] private HttpClient Http { get; set; } = default; + [Inject] private APIService API { get; set; } = default!; + [Inject] private CookieService Cookie { get; set; } = default!; + protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { - // Web_AM_Connect_Key 쿠키를 가져오는 JavaScript 함수를 호출합니다. - var header = await JS.InvokeAsync("blazorGetCookie", "Web_AM_Connect_Key"); + var cookie = await Cookie.GetCookieAsync("Web_AM_Connect_Key"); // 값 없으면 API 호출 - if (string.IsNullOrEmpty(header)) + if (string.IsNullOrEmpty(cookie)) { - var response = await Http.GetFromJsonAsync("/api/v1/in/app?type=W&specific=Web_Connect&project=AcaMate"); - Console.WriteLine($"COOKIE: {response.header}"); - if (!string.IsNullOrEmpty(response?.header)) + var response = await API.GetJsonAsync( + "/api/v1/in/app", + new AppHeader() + { + type = "W", + specific = "Web_Connect", + project = "AcaMate" + }); + if (!string.IsNullOrEmpty(response.data.header)) { - Console.WriteLine($"NO?"); - await JS.InvokeVoidAsync("blazorSetCookie", "Web_AM_Connect_Key", response.header, 1); + await Cookie.SetCookieAsync("Web_AM_Connect_Key", response.data.header); } } } } - public class ApiResult - { - public string header { get; set; } - } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 439c3b7..1b19831 100644 --- a/Program.cs +++ b/Program.cs @@ -25,5 +25,6 @@ builder.Services.AddScoped(sp => //new HttpClient // SCOPED 으로 등록된 서비스는 DI 컨테이너에 등록된 서비스의 인스턴스를 사용합니다. builder.Services.AddScoped(); +builder.Services.AddScoped(); await builder.Build().RunAsync(); diff --git a/Program/Models/APIResponse.cs b/Program/Models/APIResponse.cs new file mode 100644 index 0000000..316e1f9 --- /dev/null +++ b/Program/Models/APIResponse.cs @@ -0,0 +1,32 @@ +using System.Text.Json; + +namespace Front.Program.Models +{ + public class APIResponseStatus + { + public Status status { get; set; } + public T? data { get; set; } + + public string JsonToString() + { + return JsonSerializer.Serialize(this); + } + } + + public class Status + { + public string code { get; set; } + public string message { get; set; } + + } + + public class AppHeader + { + public string type { get; set; } + public string specific { get; set; } + public string project { get; set; } + } + + + +} \ No newline at end of file diff --git a/Program/Models/AppModel.cs b/Program/Models/AppModel.cs new file mode 100644 index 0000000..7a68a63 --- /dev/null +++ b/Program/Models/AppModel.cs @@ -0,0 +1,6 @@ +namespace Front.Program.Models; + +public class APIHeader +{ + public string header { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/Program/Services/APIService.cs b/Program/Services/APIService.cs index 4b4952d..51f3845 100644 --- a/Program/Services/APIService.cs +++ b/Program/Services/APIService.cs @@ -1,12 +1,31 @@ +using System.Net.Http.Json; +using Front.Program.Models; + namespace Front.Program.Services; public class APIService { private readonly HttpClient _http; + public APIService(HttpClient http) { _http = http; } + + private string ChangeToString(T data) + { + if (data == null) return string.Empty; + var properties = typeof(T).GetProperties(); + var value = properties.Select(p => $"{p.Name}={p.GetValue(data)}"); + return string.Join("&", value); + } + + public async Task?> GetJsonAsync(string url, TRequest value) + { + string parameter = ChangeToString(value); + var response = await _http.GetFromJsonAsync>($"{url}?{parameter}"); + return response; + } } \ No newline at end of file diff --git a/Program/Services/CookieService.cs b/Program/Services/CookieService.cs new file mode 100644 index 0000000..ecd419c --- /dev/null +++ b/Program/Services/CookieService.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; + +namespace Front.Program.Services; + +public class CookieService +{ + private readonly IJSRuntime _js; + + public CookieService(IJSRuntime js) + { + _js = js; + } + + public async Task SetCookieAsync(string key, string value) + { + await _js.InvokeVoidAsync("setCookie", key, value, 1); + } + + public async Task GetCookieAsync(string key) + { + return await _js.InvokeAsync("getCookie", key); + } +} \ No newline at end of file diff --git a/wwwroot/index.html b/wwwroot/index.html index 6dcc12c..e794aae 100644 --- a/wwwroot/index.html +++ b/wwwroot/index.html @@ -34,6 +34,7 @@ + +