Beispiel #1
0
        public async Task <Monad <string> > GetShakespeareanTranslation(string source)
        {
            var hash     = _hasher.ComputeBase64Hash(source);
            var cacheKey = $"shakespeare:{hash}";

            var cachedResult = await _cache.GetStringAsync(cacheKey);

            if (cachedResult is not null)
            {
                _logger.LogInformation("Cache hit for translation");
                return(new Monad <string>(cachedResult));
            }

            ApiResponse <TranslationResult> response;

            try
            {
                response = await _translationApi.GetShakespeareanTranslation(source);
            }
            catch (ApiException e)
            {
                return(new Monad <string>(e));
            }

            return(response switch
            {
                { StatusCode : HttpStatusCode.TooManyRequests } => new Monad <string>(new LimitExceededException()),