38 lines
1.5 KiB
C#
38 lines
1.5 KiB
C#
using Newtonsoft.Json;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.IO;
|
|
|
|
namespace PipefyAddCompanies.Core.Services
|
|
{
|
|
public class HttpService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly string _apiUrl = string.Empty;
|
|
private readonly string _token = string.Empty;
|
|
|
|
public HttpService()
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.Build();
|
|
|
|
_apiUrl = config["Pipefy:ApiUrl"] ?? throw new ArgumentNullException(nameof(_apiUrl)); // Garantir que o endereço da API não seja nulo
|
|
_token = config["Pipefy:Token"] ?? throw new ArgumentNullException(nameof(_token)); // Garantir que a chave de API não seja nula
|
|
_httpClient = new HttpClient();
|
|
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _token);
|
|
}
|
|
internal async Task<dynamic?> FazerRequisicaoApiAsync(string strQuery)
|
|
{
|
|
var content = new StringContent(strQuery, Encoding.UTF8, "application/json");
|
|
var response = await _httpClient.PostAsync(_apiUrl, content);
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
|
return JsonConvert.DeserializeObject<dynamic>(jsonResponse);
|
|
}
|
|
}
|
|
}
|