64 lines
2.2 KiB
C#
64 lines
2.2 KiB
C#
using ChatApi.Infra;
|
|
using ChatApi.Services.Bot.Structs;
|
|
using ChatApi.Settings;
|
|
using System.Text;
|
|
|
|
namespace ChatApi.Services.Bot
|
|
{
|
|
public class ActionCreateCall : IActionCall
|
|
{
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly ChatRHSettings _configuration;
|
|
private dynamic _callRH;
|
|
|
|
public ActionCreateCall(IHttpClientFactory httpClientFactory, ChatRHSettings configuration)
|
|
{
|
|
_httpClientFactory = httpClientFactory;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public Task<Result<string>> Call()
|
|
{
|
|
var httpClient =_httpClientFactory.CreateClient();
|
|
var url = new Uri(new Uri(_configuration.Url), _configuration.Create);
|
|
var request = new HttpRequestMessage(HttpMethod.Post, url);
|
|
request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(_callRH), Encoding.UTF8, "application/json");
|
|
var response = httpClient.SendAsync(request).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var content = response.Content.ReadAsStringAsync().Result;
|
|
return Task.FromResult(Result.Ok<string>(content));
|
|
}
|
|
|
|
var errorContent = "";
|
|
try
|
|
{
|
|
errorContent = response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errorContent = "Náo foi possivel criar sua solicitação. Reinicie.";
|
|
}
|
|
|
|
return Task.FromResult(Result.Fail<string>(errorContent));
|
|
}
|
|
|
|
public Task<Result> Populate(Dictionary<string, string>? knowParameters, Dictionary<int, string> answers)
|
|
{
|
|
if (knowParameters == null) return Task.FromResult(Result.Fail("Nome e/ou email não foram informados."));
|
|
|
|
_callRH = new
|
|
{
|
|
Nome = knowParameters["Nome"],
|
|
Email = knowParameters["Email"],
|
|
WhatsApp = answers[0],
|
|
TipoSolicitacao = answers[1],
|
|
Descricao = answers[2],
|
|
};
|
|
|
|
return Task.FromResult(Result.Ok());
|
|
}
|
|
}
|
|
}
|