92 lines
3.5 KiB
C#
92 lines
3.5 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace DomvsChatBot.Comm
|
|
{
|
|
public class ApiService
|
|
{
|
|
private readonly IHttpClientFactory _clientFactory;
|
|
private readonly IConfigurationManager _configurationManager;
|
|
private readonly string _baseUrl;
|
|
|
|
public ApiService(IHttpClientFactory clientFactory, IConfigurationManager configurationManager)
|
|
{
|
|
_clientFactory = clientFactory;
|
|
_configurationManager = configurationManager;
|
|
_baseUrl = _configurationManager.GetSection("BaseUrlApi").Value;
|
|
}
|
|
|
|
public async Task<string> GetSecret(string id, string company, string name)
|
|
{
|
|
var httpClient = _clientFactory.CreateClient();
|
|
var content = "";
|
|
var secret = "";
|
|
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/login/newclient"))
|
|
{
|
|
//requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
var sendNew = new
|
|
{
|
|
localId = id,
|
|
companyTenant = company,
|
|
name = name
|
|
};
|
|
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(sendNew), Encoding.UTF8, "application/json");
|
|
|
|
var responseGet = await httpClient.SendAsync(requestMessage);
|
|
content = responseGet.Content.ReadAsStringAsync().Result;
|
|
|
|
dynamic returned = JObject.Parse(content);
|
|
secret = returned.secret;
|
|
}
|
|
|
|
return secret;
|
|
}
|
|
|
|
public async Task<string> GetToken(string clientId, string clientName, string clientSecret)
|
|
{
|
|
var httpClient = _clientFactory.CreateClient();
|
|
var content = "";
|
|
var token = "";
|
|
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/login/token"))
|
|
{
|
|
//requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
var sendNew = new
|
|
{
|
|
clientId = clientId,
|
|
clientName = clientName,
|
|
clientSecret = clientSecret
|
|
};
|
|
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(sendNew), Encoding.UTF8, "application/json");
|
|
|
|
var responseGet = await httpClient.SendAsync(requestMessage);
|
|
content = responseGet.Content.ReadAsStringAsync().Result;
|
|
|
|
dynamic returned = JObject.Parse(content);
|
|
token = returned.token;
|
|
}
|
|
|
|
return token;
|
|
}
|
|
|
|
public async Task<string> GetMessage(string token, string message, string sessionId)
|
|
{
|
|
var httpClient = _clientFactory.CreateClient();
|
|
var content = "";
|
|
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{_baseUrl}/chat/response?SessionId={HttpUtility.UrlEncode(sessionId)}&Message={HttpUtility.UrlEncode(message)}"))
|
|
{
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
var responseGet = await httpClient.SendAsync(requestMessage);
|
|
content = responseGet.Content.ReadAsStringAsync().Result;
|
|
}
|
|
|
|
return content;
|
|
}
|
|
}
|
|
}
|