public static KnownRouteDTO ToKnownRouteDTO(this HttpResponseMessage msg) { if (msg == null) { return(null); } var content = msg.Content?.ReadAsByteArrayAsync().GetAwaiter().GetResult(); var result = new KnownRouteDTO { Id = Guid.NewGuid(), MethodName = msg.RequestMessage.Method.Method, RelativeUrl = $"{msg.RequestMessage.RequestUri.PathAndQuery}{msg.RequestMessage.RequestUri.Fragment}", Action = msg.RequestMessage.GetActionHeaderValue(), Replies = new List <KnownRouteReplyDTO> { new KnownRouteReplyDTO { Id = Guid.NewGuid(), StatusCode = (int)msg.StatusCode, ContentType = msg.Content?.Headers?.ContentType?.MediaType, ContentTypeCharset = msg.Content?.Headers?.ContentType?.CharSet, ContentLength = msg.Content?.Headers?.ContentLength, ContentBase64 = System.Convert.ToBase64String(content), ContentEncoding = msg.Content?.Headers?.ContentEncoding?.FirstOrDefault(), Headers = msg.Headers?.AsDictionary() } } }; return(result); }
private async Task StoreReply(string serviceCode, KnownRouteDTO dto, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(serviceCode)) { throw new ArgumentNullException(nameof(serviceCode)); } if (dto == null) { throw new ArgumentNullException(nameof(dto)); } try { await _authenticatedServiceClient.StoreReply(serviceCode, dto, cancellationToken); // this can throw 401 or something _logger.LogInformation($"{serviceCode} known routes stored"); } catch (HttpResponseException ex) { _logger.LogError($"Unable to store reply: {ex.StatusCode}/{ex.Body}"); } catch (Exception ex) { _logger.LogError(ex.Message); } }
public async Task StoreReply(string serviceCode, KnownRouteDTO dto, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(serviceCode)) { throw new ArgumentNullException(nameof(serviceCode)); } if (dto == null) { throw new ArgumentNullException(nameof(dto)); } // StoreFoundRouteInCache await this.PostAsync <string>(dto, $"knownroute/store/{serviceCode}", cancellationToken); _logger.LogInformation("dto sent to configuration api"); }
public async Task <ActionResult> StoreFoundRouteInCache(string serviceCode, [FromBody] KnownRouteDTO dto) { if (dto == null) { throw new ArgumentNullException(); } var cachekey = KakaduConstants.GetFoundRoutesKey(serviceCode); var list = await _cache.GetAsync <List <KnownRouteDTO> >(cachekey) ?? new List <KnownRouteDTO>(); var contains = list.Any(route => route.RelativeUrl.Equals(dto.RelativeUrl, StringComparison.InvariantCultureIgnoreCase) && route.MethodName.Equals(dto.MethodName, StringComparison.InvariantCultureIgnoreCase)); if (!contains) { list.Add(dto); await _cache.SetAsync <List <KnownRouteDTO> >(cachekey, list, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(4) }); } return(Ok()); }
public void StoreReply_ThrowsExceptionsOnEmptyParameters() { var controller = new RestController(_loggerMock.Object, _anonymousServiceClientMock.Object, _authenticatedServiceClientMock.Object, _cacheMock.Object); var knownRoute = new KnownRouteDTO { Id = Guid.NewGuid(), MethodName = "GET", RelativeUrl = "/path", Replies = new List <KnownRouteReplyDTO> { new KnownRouteReplyDTO { Id = Guid.NewGuid(), StatusCode = 200, ContentType = "application/json", ContentEncoding = "utf-8", ContentLength = 0 } } }; var type = typeof(BaseActionApiController); var method = type .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) .First(x => x.Name == "StoreReply" && x.IsPrivate); var cts = new CancellationTokenSource(1000); var task = (Task)method.Invoke(controller, new object[] { "", null, cts.Token }); Assert.ThrowsAsync <ArgumentNullException>(async() => await task); task = (Task)method.Invoke(controller, new object[] { "dummy", null, cts.Token }); Assert.ThrowsAsync <ArgumentNullException>(async() => await task); task = (Task)method.Invoke(controller, new object[] { "", knownRoute, cts.Token }); Assert.ThrowsAsync <ArgumentNullException>(async() => await task); task = (Task)method.Invoke(controller, new object[] { null, knownRoute, cts.Token }); Assert.ThrowsAsync <ArgumentNullException>(async() => await task); }