Esempio n. 1
0
        public async Task <ActionResult> StartRecordingAsync(string serviceCode, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(serviceCode))
            {
                throw new ArgumentNullException(nameof(serviceCode));
            }

            if (!await TryValidateTokenAsync(cancellationToken))
            {
                return(Unauthorized());
            }

            var authToken      = HttpContext.Request.Headers["Authorization"];
            var recordCacheKey = KakaduConstants.GetRecordKey(serviceCode);

            await _cache.SetStringAsync(KakaduConstants.ACCESS_TOKEN, authToken.ToString(), new DistributedCacheEntryOptions {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
            }, token : cancellationToken);

            await _cache.SetAsync <bool>(recordCacheKey, true, new DistributedCacheEntryOptions {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
            }, token : cancellationToken);

            return(Ok(true));
        }
Esempio n. 2
0
        private async Task DropServiceCacheAsync(string serviceCode, CancellationToken cancellationToken = default)
        {
            await _cache.RemoveAsync(KakaduConstants.SERVICES, cancellationToken);

            if (string.IsNullOrWhiteSpace(serviceCode))
            {
                return;
            }

            await _cache.RemoveAsync(KakaduConstants.GetServiceKey(serviceCode), cancellationToken);
        }
Esempio n. 3
0
        private bool ShouldSaveResponse(string serviceCode)
        {
            if (string.IsNullOrWhiteSpace(serviceCode))
            {
                throw new ArgumentNullException(nameof(serviceCode));
            }

            var recordCacheKey = KakaduConstants.GetRecordKey(serviceCode);

            return(_cache.GetAsync <bool?>(recordCacheKey).GetAwaiter().GetResult() ?? false);
        }
Esempio n. 4
0
        internal async Task <ServiceCaptureStatusDTO> GetRecordingStatusAsync(string serviceCode, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(serviceCode))
            {
                throw new ArgumentNullException(serviceCode);
            }

            var recordCacheKey = KakaduConstants.GetRecordKey(serviceCode);

            return(new ServiceCaptureStatusDTO {
                ServiceCode = serviceCode,
                IsRecording = (await _cache.GetAsync <bool?>(recordCacheKey, cancellationToken)) ?? false
            });
        }
Esempio n. 5
0
        public async Task <ActionResult> StopRecordingAsync(string serviceCode, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(serviceCode))
            {
                throw new ArgumentNullException(nameof(serviceCode));
            }

            if (!await TryValidateTokenAsync(cancellationToken))
            {
                return(Unauthorized());
            }

            var recordCacheKey = KakaduConstants.GetRecordKey(serviceCode);
            await _cache.RemoveAsync(recordCacheKey, cancellationToken);

            return(Ok(false));
        }
Esempio n. 6
0
        public async Task <ActionResult <ServiceDTO> > GetByCodeAsync(string serviceCode)
        {
            if (string.IsNullOrWhiteSpace(serviceCode))
            {
                throw new ArgumentNullException(nameof(serviceCode));
            }

            var dto = await _cache.GetOrAddAsync(KakaduConstants.GetServiceKey(serviceCode), async (options) => {
                options.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(3);

                var entity = await Task.Run(() => _service.Get(serviceCode));
                if (entity == null)
                {
                    throw new HttpNotFoundException($"No service definition found for '{serviceCode}'");
                }

                return(_mapper.Map <ServiceDTO>(entity));
            });

            return(dto);
        }
Esempio n. 7
0
        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());
        }
Esempio n. 8
0
        public async Task <ActionResult <List <ActionApiCallResultDTO> > > StopRecordingAsync(string serviceCode, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(serviceCode))
            {
                throw new ArgumentNullException(nameof(serviceCode));
            }

            var actionResult = await _actionApiService.StopRecordingAsync(serviceCode, cancellationToken);

            // save captured routes
            var cachekey       = KakaduConstants.GetFoundRoutesKey(serviceCode);
            var capturedRoutes = await _cache.GetAsync <List <KnownRouteDTO> >(cachekey, token : cancellationToken);

            if (capturedRoutes != null && capturedRoutes.Any())
            {
                var entities = _mapper.Map <List <KnownRouteModel> >(capturedRoutes);

                // TODO: go through replies, if compressed, store them in a raw form and make sure they're encoded when sending back from cache

                /*
                 * if (contentEncoding == "gzip")
                 * {
                 *  content = DecompressGZip(content);
                 *  if (msg.Content?.Headers?.Contains("Content-Encoding") ?? false)
                 *  {
                 *      msg.Content?.Headers?.Remove("Content-Encoding");
                 *      contentEncoding = string.Empty;
                 *  }
                 * }
                 */

                _serviceService.AddKnownRoutes(serviceCode, entities);
            }

            // clear cache
            await _cache.RemoveAsync(KakaduConstants.GetServiceKey(serviceCode), cancellationToken);

            return(Ok(actionResult));
        }