[성현모] SystemX.Core Http PUT 기능 추가

This commit is contained in:
SHM
2026-02-02 09:23:24 +09:00
parent 73166a0285
commit cfc36e80d4
3 changed files with 179 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemX.Core.Services
{
public class CommonService
{
public CommonService()
{
}
}//end class
}

View File

@ -0,0 +1,163 @@
using Azure.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
namespace SystemX.Core.Services
{
public class HttpService : CommonService
{
public HttpService()
: base()
{
}
/// <summary>
/// PostJsonAsync
/// </summary>
/// <param name="url">https://127.0.0.1:443</param>
/// <param name="timeOutSeconds">Range 5~30 secconds</param>
public virtual async Task<RESPONSE?> PostJsonAsync<REQUEST, RESPONSE>(string url, REQUEST request, short timeOutSeconds = 5) where REQUEST : class where RESPONSE : class
{
RESPONSE? response = default(RESPONSE);
Guid guid = Guid.NewGuid();
using (HttpClient httpClient = new HttpClient(GetClientHandler()))
{
int retry = 0;
while (true)
{
await Task.Delay(1);
try
{
var timeOutSec = SetTimeout(timeOutSeconds);
httpClient.Timeout = new TimeSpan(0, 0, timeOutSec);
httpClient.BaseAddress = new Uri($"{url}");
LogXnet.WriteLine($"[POST] Request({guid})::{url}{Environment.NewLine}{request?.ToJson()}", LogXLabel.HTTP);
DateTime requestTime = DateTime.Now;
var res = await httpClient.PostAsJsonAsync(url, request);
response = await res.Content.ReadFromJsonAsync<RESPONSE>();
LogXnet.WriteLine($"[POST] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}{Environment.NewLine}{response?.ToJson()}", LogXLabel.HTTP);
break;
}
catch (Exception e)
{
LogXnet.WriteLine(e);
retry += 1;
}
if (retry > 1)
break;
}
}
return response;
}
/// <summary>
/// GetJsonAsnyc
/// </summary>
/// <param name="url">https://127.0.0.1:443</param>
/// <param name="timeOutSeconds">Range 5~30 secconds</param>
public virtual async Task<RESPONSE?> GetJsonAsync<RESPONSE>(string url, short timeOutSeconds = 10) where RESPONSE : class
{
RESPONSE? response = default(RESPONSE);
Guid guid = Guid.NewGuid();
using (HttpClient httpClient = new HttpClient(GetClientHandler()))
{
try
{
var timeOutSec = SetTimeout(timeOutSeconds);
httpClient.Timeout = new TimeSpan(0, 0, timeOutSec);
httpClient.BaseAddress = new Uri($"{url}");
LogXnet.WriteLine($"[GET] Request({guid})::{url}", LogXLabel.HTTP);
DateTime requestTime = DateTime.Now;
response = await httpClient.GetFromJsonAsync<RESPONSE>(url);
LogXnet.WriteLine($"[GET] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}", LogXLabel.HTTP);
}
catch (Exception e)
{
LogXnet.WriteLine(e);
}
}
return response;
}
/// <summary>
/// PutJsonAsnyc
/// </summary>
/// <param name="url">https://127.0.0.1:443</param>
/// <param name="timeOutSeconds">Range 5~30 secconds</param>
public virtual async Task<RESPONSE?> PutJsonAsync<REQUEST, RESPONSE>(string url, REQUEST request, short timeOutSeconds = 5) where REQUEST : class where RESPONSE : class
{
RESPONSE? response = default(RESPONSE);
Guid guid = Guid.NewGuid();
using (HttpClient httpClient = new HttpClient(GetClientHandler()))
{
int retry = 0;
while (true)
{
await Task.Delay(1);
try
{
var timeOutSec = SetTimeout(timeOutSeconds);
httpClient.Timeout = new TimeSpan(0, 0, timeOutSec);
httpClient.BaseAddress = new Uri($"{url}");
LogXnet.WriteLine($"[PUT] Request({guid})::{url}{Environment.NewLine}{request?.ToJson()}", LogXLabel.HTTP);
DateTime requestTime = DateTime.Now;
var res = await httpClient.PutAsJsonAsync(url, request);
response = await res.Content.ReadFromJsonAsync<RESPONSE>();
LogXnet.WriteLine($"[PUT] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}{Environment.NewLine}{response?.ToJson()}", LogXLabel.HTTP);
break;
}
catch (Exception e)
{
LogXnet.WriteLine(e);
retry += 1;
}
if (retry > 1)
break;
}
}
return response;
}
protected HttpClientHandler GetClientHandler()
{
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
{
return true;
};
return clientHandler;
}
protected short SetTimeout(short timeOutSeconds)
{
short timeoutMin = 5;
short timeoutMax = 30;
timeOutSeconds = Math.Clamp(timeOutSeconds, timeoutMin, timeoutMax);
return timeOutSeconds;
}
}
}