コード例 #1
0
        private static Task HandleErrorAsync(HttpContext context, Exception exception)
        {
            var statusCode = HttpStatusCode.BadRequest;
            var response   = new QrResponseModel
            {
                Data  = null,
                Error = exception.Message
            };

            var payload = JsonSerializer.Serialize(response);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)statusCode;

            return(context.Response.WriteAsync(payload));
        }
コード例 #2
0
        public async Task <QrResponseModel> ScanQrAsync(string qrPath)
        {
            _logger.LogTrace($"{TAG} ScanQrAsync called with path: {qrPath}");
            if (string.IsNullOrWhiteSpace(qrPath))
            {
                throw new ArgumentException("The path cannot be null or empty.");
            }

            var cacheKey       = new QrCacheKey(qrPath);
            var cachedResponse = await _cacheStore.GetAsync <QrResponseModel>(cacheKey);

            if (cachedResponse != null)
            {
                _logger.LogTrace($"{TAG} Returning response found in cache.");
                return(cachedResponse);
            }

            QrFileModel file = null;

            try
            {
                file = await _provider.ProvideQrAsync(qrPath);
            }
            catch (Exception e)
            {
                _logger.LogError($"{TAG} Provider call failed. Reason: '{e.Message}'.");
                throw;
            }

            QrResponseModel result = null;

            try
            {
                result = await _client.PostQrAsync(file);
            }
            catch (Exception e)
            {
                _logger.LogError($"{TAG} QR api call failed. Reason: '{e.Message}'.");
                throw;
            }

            await _cacheStore.AddAsync(cacheKey, result);

            return(result);
        }