public async Task GetAsync_ReturnsSuccessfulResult_WhenStatusCodeOk() { // arrange _messageHandler.OnSendAsync((request, cancellationToken) => Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK))); // act var result = await _restApiClient.GetAsync(ApiRequest.To("https://someurl.com")).ConfigureAwait(false); // assert Assert.True(result.IsSuccess); Assert.Equal(HttpMethod.Get, _messageHandler.LatestRequestMessage.Method); }
public static Task <IRestApiResult <TResult> > GetAsync <TResult>(this IRestApiClient apiClient, Priority priority, string path, int retryCount, Func <int, TimeSpan> sleepDurationProvider, CancellationToken cancellationToken = default(CancellationToken)) { return(PollyDecorator( () => apiClient.GetAsync <TResult>(priority, path, cancellationToken), retryCount, sleepDurationProvider)); }
public static Task <IRestApiResult <TResult> > GetAsync <TResult>(this IRestApiClient apiClient, Priority priority, string path, int retryCount, Func <int, TimeSpan> sleepDurationProvider, IList <KeyValuePair <string, string> > headers = null, IHttpResponseResolver httpResponseResolver = null, CancellationToken cancellationToken = default(CancellationToken)) { return(PollyDecorator( () => apiClient.GetAsync <TResult>(priority, path, headers, httpResponseResolver, cancellationToken), retryCount, sleepDurationProvider)); }
public async Task <TResponse> Get <TResponse>(List <KeyValuePair <string, string> > RequestHeader) where TResponse : new() => await httpClient.GetAsync <TResponse>(RequestHeader);
public Task <JobInfoDto[]> GetJobs() { return(_restApiClient.GetAsync <JobInfoDto[]>($"{_hostServerUrl}/api/job")); }
private async ValueTask <(bool success, CanPlayResponse response)> CanPlayAsyncInternal( Guid guid, string concentratorId, string controllerId, int cardType, string cardId, bool shouldPay, int transactionId, int endpointRssi, int concentratorRssi) { Concentrator getConcentrator() { var repo = _serviceProvider.GetService <ICardSystemRepository <Concentrator> >(); return(repo.AsNoTracking().FirstOrDefault(x => x.DeviceId == concentratorId)); }; ControllerConfigRelay getController() { var repo = _serviceProvider.GetService <ICardSystemRepository <ControllerConfigRelay> >(); return(repo.AsNoTracking() .Include(x => x.EGM) .ThenInclude(x => x.AllowedCurrencies) .FirstOrDefault(x => x.SerialNumber == controllerId && x.OwnerId == _ownerId)); }; Card getCard() { Card entity = null; ICardSystemRepository <Card> repo = null; string cardDBId = CardCacheServiceV2.Instance.GetIdByExternalId(cardId); if (!string.IsNullOrWhiteSpace(cardDBId)) { entity = CardCacheServiceV2.Instance.GetCard(_serviceProvider, cardDBId); if (entity != null) { return(entity); } repo = _serviceProvider.GetService <ICardSystemRepository <Card> >(); entity = repo.AsNoTracking() .Include(x => x.CardMode) .Include(x => x.User) .FirstOrDefault(x => x.Id == cardDBId); if (entity != null) { CardCacheServiceV2.Instance.AddCard(_serviceProvider, entity); CardCacheServiceV2.Instance.AddNewMap(entity); } return(entity); } repo = _serviceProvider.GetService <ICardSystemRepository <Card> >(); entity = repo.AsNoTracking() .Include(x => x.CardMode) .Include(x => x.User) .FirstOrDefault(x => x.ExternalId == cardId); if (entity != null) { CardCacheServiceV2.Instance.AddCard(_serviceProvider, entity); CardCacheServiceV2.Instance.AddNewMap(entity); } return(entity); }; IEnumerable <CurrencyTemplate> getCurrencyTemplates() { var repo = _serviceProvider.GetService <ICardSystemRepository <CurrencyTemplate> >(); return(repo.AsNoTracking() .Where(x => x.OwnerId == _ownerId) .ToList()); } var concentrator = getConcentrator(); if (concentrator == null) { var response = new CanPlayResponse { TransactionId = transactionId, Time = DateTime.UtcNow, ResponseCode = 101, ConcentratorId = concentratorId, ControllerId = controllerId, CardType = cardType, CardId = cardId, CardNumber = string.Empty, ServiceId = string.Empty, ServiceName = string.Empty, Permission = false, RelayType = string.Empty, RelayPulse = 0, RelayOnTime = 0, RelayOffTime = 0, RelayDisplayTime = 0, DisplayLine1 = "Err 101", DisplayLine2 = "Concentrator N/A" }; _logger.ErrorLog($"[CardService.CanPlayAsyncInternal][-OUT-] [{guid}] TransactionId {transactionId} | ConcentratorId {concentratorId} | ControllerId {controllerId} | CardType {cardType} | CardId {cardId} | ShouldPay {shouldPay} | EndpointRssi: {endpointRssi} | ConcentratorRssi {concentratorRssi} | {JsonSerializer.Serialize(response, new JsonSerializerOptions { WriteIndented = false })}", GetIpAddress()); return(false, response); } var controller = getController(); if (controller == null) { var response = new CanPlayResponse { TransactionId = transactionId, Time = DateTime.UtcNow, ResponseCode = 102, ConcentratorId = concentratorId, ControllerId = controllerId, CardType = cardType, CardId = cardId, CardNumber = string.Empty, ServiceId = string.Empty, ServiceName = string.Empty, Permission = false, RelayType = string.Empty, RelayPulse = 0, RelayOnTime = 0, RelayOffTime = 0, RelayDisplayTime = 0, DisplayLine1 = "Err 102", DisplayLine2 = "Controller N/A" }; _logger.ErrorLog($"[CardService.CanPlayAsyncInternal][-OUT-] [{guid}] TransactionId {transactionId} | ConcentratorId {concentratorId} | ControllerId {controllerId} | CardType {cardType} | CardId {cardId} | ShouldPay {shouldPay} | EndpointRssi: {endpointRssi} | ConcentratorRssi {concentratorRssi} | {JsonSerializer.Serialize(response, new JsonSerializerOptions { WriteIndented = false })}", GetIpAddress()); return(false, response); } var card = getCard(); if (card == null) { var response = new CanPlayResponse { TransactionId = transactionId, Time = DateTime.UtcNow, ResponseCode = 103, ConcentratorId = concentratorId, ControllerId = controllerId, CardType = cardType, CardId = cardId, CardNumber = string.Empty, ServiceId = string.Empty, ServiceName = string.Empty, Permission = false, RelayType = string.Empty, RelayPulse = 0, RelayOnTime = 0, RelayOffTime = 0, RelayDisplayTime = 0, DisplayLine1 = "Err 103", DisplayLine2 = "Card N/A" }; _logger.ErrorLog($"[CardService.CanPlayAsyncInternal][-OUT-] [{guid}] TransactionId {transactionId} | ConcentratorId {concentratorId} | ControllerId {controllerId} | CardType {cardType} | CardId {cardId} | ShouldPay {shouldPay} | EndpointRssi: {endpointRssi} | ConcentratorRssi {concentratorRssi} | {JsonSerializer.Serialize(response, new JsonSerializerOptions { WriteIndented = false })}", GetIpAddress()); return(false, response); } var currencyTemplates = getCurrencyTemplates(); IEnumerable <ScheduleDay> getScheduleDays() { IEnumerable <ScheduleDay> egmPriceModifiers = null; var repo = _serviceProvider.GetService <ICardSystemRepository <ScheduleDay> >(); if (!string.IsNullOrWhiteSpace(controller.EGM.Id)) { egmPriceModifiers = repo.AsNoTracking() .Include(x => x.Hours) .Where(x => x.EGMId == controller.EGM.Id) .AsEnumerable(); } if (egmPriceModifiers != null && !egmPriceModifiers.Any()) { egmPriceModifiers = repo .AsNoTracking() .Include(x => x.Hours) .Where(x => x.GameCenterId == controller.EGM.GameCenterId) .AsEnumerable(); } return(egmPriceModifiers ?? new List <ScheduleDay>()); } var scheduleDays = getScheduleDays(); // ако се изиква плащане този метод променя баланса на картата var canPlayDto = _serviceProvider.GetService <ICanPlayService>().CanPlayV2( _serviceProvider, concentrator, controller, card, scheduleDays.ToList(), shouldPay, currencyTemplates); if (canPlayDto.HasError) { var errors = canPlayDto.Message.Split('|'); var response = new CanPlayResponse { TransactionId = transactionId, Time = DateTime.UtcNow, ResponseCode = int.Parse(errors[0]), ConcentratorId = concentratorId, ControllerId = controllerId, CardType = cardType, CardId = cardId, CardNumber = string.Empty, ServiceId = string.Empty, ServiceName = string.Empty, Permission = false, RelayType = string.Empty, RelayPulse = 0, RelayOnTime = 0, RelayOffTime = 0, RelayDisplayTime = 0, DisplayLine1 = $"Err {errors[0]}", DisplayLine2 = errors[1] }; _logger.ErrorLog($"[CardService.CanPlayAsyncInternal][-OUT-] [{guid}] TransactionId {transactionId} | ConcentratorId {concentratorId} | ControllerId {controllerId} | CardType {cardType} | CardId {cardId} | ShouldPay {shouldPay} | EndpointRssi: {endpointRssi} | ConcentratorRssi {concentratorRssi} | {JsonSerializer.Serialize(response, new JsonSerializerOptions { WriteIndented = false })}", GetIpAddress()); return(false, response); } EGM gameDto = null; if (canPlayDto.CanPlay) { gameDto = controller.EGM; } var canPlayResponse = new CanPlayResponse { TransactionId = transactionId, Time = DateTime.UtcNow, ResponseCode = 0, ConcentratorId = concentrator.DeviceId, ControllerId = controller.SerialNumber, CardType = card.Type, CardId = card.ExternalId, CardNumber = card.Number, ServiceId = canPlayDto.GameId, ServiceName = canPlayDto.GameName, Permission = canPlayDto.CanPlay, RelayType = gameDto?.RelayMode, RelayPulse = gameDto?.RelayPulse ?? 0, RelayOnTime = gameDto?.RelayOnTime ?? 0, RelayOffTime = gameDto?.RelayOffTime ?? 0, RelayDisplayTime = gameDto?.RelayDisplayTime ?? 0, DisplayLine1 = $"{canPlayDto.PricePerGame:F2}".Trim().Trim(',').Trim(), DisplayLine2 = $"{canPlayDto.RemainingBalance:F2}" }; // само ако се изисква плащане логира плащането в транзакшън лога и обновява кеша на картата в api.icardmanager.eu if (shouldPay) { canPlayDto.GameId = gameDto?.Id; string relayInfo = $" | Relay ontime: {gameDto?.RelayOnTime}, Relay offtime: {gameDto?.RelayOffTime}, Relay pulse: {gameDto?.RelayPulse}, Relay display time: {gameDto?.RelayDisplayTime}, Relay mode: {gameDto?.RelayMode}, Game Serial Number: '{gameDto?.SerialNumber}'"; CreateCanPlayLog( concentrator.DeviceId, controller.SerialNumber, $"{canPlayDto.GameCenterName}{relayInfo}", canPlayDto.GameId, canPlayDto.GameName, canPlayDto.CurrencyInfo, card.ExternalId, card.Type, canPlayDto.PricePerGame, canPlayDto.PaidFromBonusBalance, canPlayDto.CanPlay, gameDto?.GameCenterId, gameDto != null ? gameDto.OwnerId : "system" ); try { var apiResponse = await _restApiClient.GetAsync <string>(_configuration.GetSection("AppSettings:ApiServiceURL").Get <string>(), $"api/{_ownerId}/card/{card.Id}/reloadcache"); if (apiResponse.hasError) { _logger.ErrorLog($"[CardService.CanPlayAsyncInternal][-API-] [{guid}] TransactionId {transactionId} | ConcentratorId {concentratorId} | ControllerId {controllerId} | CardType {cardType} | CardId {cardId} | ShouldPay {shouldPay} | EndpointRssi: {endpointRssi} | ConcentratorRssi {concentratorRssi} | {apiResponse.errorMessage}", GetIpAddress()); } } catch (Exception ex) { _logger.LogException($"CardService.CanPlayAsyncInternal throws: {ex}"); } } _logger.InfoLog($"[CardService.CanPlayAsyncInternal][-OUT-] [{guid}] TransactionId {transactionId} | ConcentratorId {concentratorId} | ControllerId {controllerId} | CardType {cardType} | CardId {cardId} | ShouldPay {shouldPay} | EndpointRssi: {endpointRssi} | ConcentratorRssi {concentratorRssi} | DB Result {JsonSerializer.Serialize(canPlayResponse, new JsonSerializerOptions { WriteIndented = false })}", GetIpAddress()); return(true, canPlayResponse); }