EmployeeService
user_7238188
csharp
2 years ago
6.6 kB
6
Indexable
// Ignore Spelling: api using BUMA.LineUp.Services.General.Application.Interfaces; using BUMA.LineUp.Services.General.Domain.Constantas; using BUMA.LineUp.Services.General.Domain.Interfaces.ServiceEmployee; using BUMA.LineUp.Services.General.Domain.Models.Configuration; using BUMA.LineUp.Services.General.Domain.Models.Request; using BUMA.LineUp.Services.General.Domain.Models.Request.Notifications; using BUMA.LineUp.Services.General.Domain.Models.Response; using BUMA.LineUp.Services.General.Domain.Models.Response.ServiceEmployee; using CSharpFunctionalExtensions; using MediatR; using Microsoft.ApplicationInsights; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System.Net; using System.Reflection; using System.Text; /*using Newtonsoft.Json;*/ using System.Text.Json; namespace BUMA.LineUp.Services.General.Application.Services { public class EmployeeService : IServiceEmployeeRepository { private readonly IHttpRequestService _httpRequestService; private readonly ApiUrls _apiUrls; private readonly HttpClient _httpClient; private readonly JsonSerializerOptions _serializeOptions; private readonly ILogger<EmployeeService> _logger; private readonly TelemetryClient _telemetryClient; private readonly IMediator _mediator; private readonly IOptions<ApiUrls> _appConfiguration; private readonly IHttpClientFactory _httpClientFactory; public EmployeeService( IHttpRequestService httpRequestService, IOptions<ApiUrls> apiUrls, HttpClient httpClient, ILogger<EmployeeService> logger, TelemetryClient telemetryClient, IMediator mediator, IOptions<ApiUrls> appConfiguration, IHttpClientFactory httpClientFactory ) { _apiUrls = apiUrls.Value; _httpRequestService = httpRequestService; _serializeOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; _httpClient = httpClient; _logger = logger; _telemetryClient = telemetryClient; _mediator = mediator; _appConfiguration = appConfiguration; _httpClientFactory = httpClientFactory; } public async Task<List<ServiceEmployeeResponse>> GetServiceEmployeeAsync(DefaultGetRequest request, CancellationToken cancellationToken) { List<ServiceEmployeeResponse> result = new List<ServiceEmployeeResponse>(); if (string.IsNullOrEmpty(_apiUrls.ServiceResourceApi)) return result; var apiUrl = new Uri(string.Concat(_apiUrls.ServiceResourceApi, ServiceResourceApi.getMasterEmployee)); var requestObject = request.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .ToDictionary(prop => prop.Name, prop => prop.GetValue(request, null)); var requestApi = await _httpRequestService.PostAsync(apiUrl, requestObject, cancellationToken).ConfigureAwait(false); if (requestApi == null || requestApi.Result == null || requestApi.Result.IsError) return result; result = JsonSerializer.Deserialize<List<ServiceEmployeeResponse>>(JsonSerializer.Serialize(requestApi.Result.Content)); return result; } public async Task<ApiResponse> GetServiceFCmAsync(List<long>? request, CancellationToken cancellationToken = default) { try { _logger.LogInformation("memulai {eventName}", nameof(GetServiceFCmAsync)); var result = new ApiResponse { StatusCode = (int)HttpStatusCode.OK, Title = "Success" }; var apiUrl = _apiUrls.ServiceFmToken + ServiceFcmToken.getFCmToken; var requestObject = new List<long> { }; _logger.LogInformation("Start API" + apiUrl); var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, apiUrl); httpRequestMessage.Headers.Add("Accept", "application/json"); var requestBody = JsonSerializer.Serialize(request, _serializeOptions); _logger.LogInformation("Request Body " + requestBody); httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); var httpClient = _httpClientFactory.CreateClient(); var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, cancellationToken); httpResponseMessage.EnsureSuccessStatusCode(); _logger.LogInformation("Response " + httpResponseMessage); var responseContent = await httpResponseMessage.Content.ReadAsStringAsync(cancellationToken); _logger.LogInformation("Resultx Conten " + responseContent); var responseObject = JsonSerializer.Deserialize<ApiResponse>(responseContent, _serializeOptions); _logger.LogInformation("Resultx Objek " + responseObject); if (responseObject == null) { var response = new ApiResponse { Title = "Error", StatusCode = (int)HttpStatusCode.InternalServerError, Result = new ServiceResult { Message = "Lineup General Callback is Empty", IsError = true } }; return response; } if (responseObject == null || responseObject.Result == null || responseObject.Result.IsError) return result; return JsonSerializer.Deserialize<ApiResponse>(JsonSerializer.Serialize(responseObject)); } catch (Exception ex) { _logger.LogError(ex, $"Error in {nameof(GetServiceFCmAsync)}"); var response = new ApiResponse { Title = "Error", StatusCode = (int)HttpStatusCode.InternalServerError, Result = new ServiceResult { Message = ex.Message, IsError = true } }; return response; } } } }
Editor is loading...
Leave a Comment