156 lines
4.2 KiB
C#
156 lines
4.2 KiB
C#
using Newtonsoft.Json;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace AccountManagement.Domain.InternalApiCaller;
|
|
|
|
public class ApiResult<T>
|
|
{
|
|
public bool Success { get; set; }
|
|
public T Result { get; set; }
|
|
public string Error { get; set; }
|
|
}
|
|
|
|
public static class InternalApiCaller
|
|
{
|
|
private static string _baseUrl = "";
|
|
|
|
public static void SetBaseUrl(string baseUrl)
|
|
{
|
|
_baseUrl = baseUrl.TrimEnd('/'); // حذف / اضافی
|
|
}
|
|
/// <summary>
|
|
///api post متد
|
|
/// </summary>
|
|
/// <typeparam name="TRequest"></typeparam>
|
|
/// <typeparam name="TResponse"></typeparam>
|
|
/// <param name="url"></param>
|
|
/// <param name="internalKey"></param>
|
|
/// <param name="body"></param>
|
|
/// <returns></returns>
|
|
public static ApiResult<TResponse> PostAsync<TRequest, TResponse>(
|
|
string url,
|
|
string internalKey,
|
|
TRequest body
|
|
)
|
|
{
|
|
try
|
|
{
|
|
var client = new HttpClient();
|
|
|
|
// ساخت URL نهایی
|
|
var finalUrl = $"{_baseUrl}/{url.TrimStart('/')}";
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, finalUrl);
|
|
|
|
request.Headers.Add("X-INTERNAL-KEY", internalKey);
|
|
|
|
var json = JsonConvert.SerializeObject(body);
|
|
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
var response = client.SendAsync(request).GetAwaiter().GetResult();
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
return new ApiResult<TResponse>
|
|
{
|
|
Success = false,
|
|
Error = $"HTTP Error: {response.StatusCode}"
|
|
};
|
|
}
|
|
|
|
var text = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
var deserialized = JsonConvert.DeserializeObject<TResponse>(text);
|
|
|
|
return new ApiResult<TResponse>
|
|
{
|
|
Success = true,
|
|
Result = deserialized
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ApiResult<TResponse>
|
|
{
|
|
Success = false,
|
|
Error = ex.Message
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Api Get متد
|
|
/// </summary>
|
|
/// <typeparam name="TResponse"></typeparam>
|
|
/// <param name="url"></param>
|
|
/// <param name="internalKey"></param>
|
|
/// <param name="parameters"></param>
|
|
/// <returns></returns>
|
|
public static ApiResult<TResponse> GetAsync<TResponse>(
|
|
string url,
|
|
string internalKey,
|
|
Dictionary<string, object> parameters = null
|
|
)
|
|
{
|
|
try
|
|
{
|
|
if (parameters != null && parameters.Any())
|
|
{
|
|
var query = string.Join("&",
|
|
parameters
|
|
.Where(p => p.Value != null)
|
|
.Select(p => $"{p.Key}={p.Value}")
|
|
);
|
|
|
|
url += url.Contains("?") ? "&" + query : "?" + query;
|
|
}
|
|
|
|
// ساخت URL نهایی
|
|
var finalUrl = $"{_baseUrl}/{url.TrimStart('/')}";
|
|
|
|
var client = new HttpClient();
|
|
var request = new HttpRequestMessage(HttpMethod.Get, finalUrl);
|
|
|
|
request.Headers.Add("X-INTERNAL-KEY", internalKey);
|
|
|
|
var response = client.SendAsync(request).GetAwaiter().GetResult();
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
return new ApiResult<TResponse>
|
|
{
|
|
Success = false,
|
|
Error = $"HTTP Error: {response.StatusCode}"
|
|
};
|
|
}
|
|
|
|
var text = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
var deserialized = JsonConvert.DeserializeObject<TResponse>(text);
|
|
|
|
return new ApiResult<TResponse>
|
|
{
|
|
Success = true,
|
|
Result = deserialized
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ApiResult<TResponse>
|
|
{
|
|
Success = false,
|
|
Error = ex.Message
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|