using CoreCms.Net.IServices;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net.Http;
|
using System.Text;
|
using System.Text.Json;
|
using System.Threading.Tasks;
|
|
namespace CoreCms.Net.Services
|
{
|
public class ApiService : IApiService
|
{
|
private readonly HttpClient _httpClient;
|
|
public ApiService(HttpClient httpClient)
|
{
|
_httpClient = httpClient;
|
}
|
|
public async Task<T> GetAsync<T>(string endpoint)
|
{
|
var response = await _httpClient.GetAsync(endpoint);
|
response.EnsureSuccessStatusCode();
|
var content = await response.Content.ReadAsStringAsync();
|
return JsonSerializer.Deserialize<T>(content);
|
}
|
|
public async Task PostAsync<T>(string endpoint, T data)
|
{
|
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
|
var response = await _httpClient.PostAsync(endpoint, content);
|
response.EnsureSuccessStatusCode();
|
}
|
|
public async Task PutAsync<T>(string endpoint, T data)
|
{
|
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
|
var response = await _httpClient.PutAsync(endpoint, content);
|
response.EnsureSuccessStatusCode();
|
}
|
|
public async Task DeleteAsync(string endpoint)
|
{
|
var response = await _httpClient.DeleteAsync(endpoint);
|
response.EnsureSuccessStatusCode();
|
}
|
}
|
}
|