using System.Net; using System.Net.Http.Headers; using System.Text; using Azaion.CommonSecurity.DTO; using Newtonsoft.Json; namespace Azaion.CommonSecurity.Services; public interface IAzaionApi { ApiCredentials Credentials { get; } User CurrentUser { get; } void UpdateOffsets(UserQueueOffsets offsets); //Stream GetResource(string filename, string folder); } public class AzaionApi(HttpClient client, ICache cache, ApiCredentials credentials) : IAzaionApi { private string _jwtToken = null!; const string APP_JSON = "application/json"; public ApiCredentials Credentials => credentials; public User CurrentUser { get { var user = cache.GetFromCache(SecurityConstants.CURRENT_USER_CACHE_KEY, () => Get("currentUser")); if (user == null) throw new Exception("Can't get current user"); return user; } } public void UpdateOffsets(UserQueueOffsets offsets) { Put($"/users/queue-offsets/{CurrentUser.Email}", offsets); } private HttpResponseMessage Send(HttpRequestMessage request) { if (string.IsNullOrEmpty(_jwtToken)) Authorize(); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _jwtToken); var response = client.Send(request); if (response.StatusCode == HttpStatusCode.Unauthorized) { Authorize(); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _jwtToken); response = client.Send(request); } if (response.IsSuccessStatusCode) return response; var stream = response.Content.ReadAsStream(); var content = new StreamReader(stream).ReadToEnd(); if (response.StatusCode == HttpStatusCode.Conflict) { var result = JsonConvert.DeserializeObject(content); throw new Exception($"Failed: {response.StatusCode}! Error Code: {result?.ErrorCode}. Message: {result?.Message}"); } throw new Exception($"Failed: {response.StatusCode}! Result: {content}"); } private T? Get(string url) { var response = Send(new HttpRequestMessage(HttpMethod.Get, url)); var stream = response.Content.ReadAsStream(); var json = new StreamReader(stream).ReadToEnd(); return JsonConvert.DeserializeObject(json); } private void Put(string url, T obj) { Send(new HttpRequestMessage(HttpMethod.Put, url) { Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, APP_JSON) }); } private void Authorize() { try { if (string.IsNullOrEmpty(credentials.Email) || credentials.Password.Length == 0) throw new Exception("Email or password is empty! Please do EnterCredentials first!"); var payload = new { email = credentials.Email, password = credentials.Password }; var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, APP_JSON); var message = new HttpRequestMessage(HttpMethod.Post, "login") { Content = content }; var response = client.Send(message); if (!response.IsSuccessStatusCode) throw new Exception($"EnterCredentials failed: {response.StatusCode}"); var stream = response.Content.ReadAsStream(); var json = new StreamReader(stream).ReadToEnd(); var result = JsonConvert.DeserializeObject(json); if (string.IsNullOrEmpty(result?.Token)) throw new Exception("JWT Token not found in response"); _jwtToken = result.Token; } catch (Exception e) { Console.WriteLine(e); throw; } } }