Example #1
0
        public async Task <EitherAcronyms> FindAsync([Utils.NotNull] string acronym)
        {
            Check.NotNull(acronym, nameof(acronym));


            try
            {
                var result = await _acronymRespository.FindAllAsync(acronym);

                var success = new EitherAcronyms(result.ToList());
                return(success);
            }
            catch (Exception ex)
            {
                var errorMessage = new ErrorMessage()
                {
                    ErrorCode = ErrorCode.DatabaseReadError,
                    Tag       = ex
                };

                var error = new EitherAcronyms(errorMessage);
                return(error);
            }
        }
Example #2
0
        public async Task <EitherAcronyms> FindAsync(string acronym)
        {
            Check.NotNull(acronym, nameof(acronym));

            var requestUri = string.Format("{0}/{1}", _restfulAcronymEndpoint, acronym);

            // Get response
            HttpResponseMessage response;

            try
            {
                response = await _httpClient.GetAsync(requestUri);
            }
            catch (Exception ex)
            {
                var errorMessage = new ErrorMessage()
                {
                    ErrorCode = ErrorCode.RestfulNetworkFailure,
                    Tag       = ex
                };
                var error = new EitherAcronyms(errorMessage);
                return(error);
            }

            // Check status code
            if (!response.IsSuccessStatusCode)
            {
                var errorMessage = new ErrorMessage()
                {
                    ErrorCode = ErrorCode.RestfulWrongStatusCode,
                    Tag       = response.StatusCode
                };
                var error = new EitherAcronyms(errorMessage);
                return(error);
            }

            // Read response
            string responseBody;

            try
            {
                responseBody = await response.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                var errorMessage = new ErrorMessage()
                {
                    ErrorCode = ErrorCode.RestfulNetworkFailure,
                    Tag       = ex
                };
                var error = new EitherAcronyms(errorMessage);
                return(error);
            }

            // Deserialize
            try
            {
                var acronyms = JsonConvert.DeserializeObject <List <AcronymModel> >(responseBody, _jsonConverter);
                var success  = new EitherAcronyms(acronyms);
                return(success);
            }
            catch (Exception ex)
            {
                var errorMessage = new ErrorMessage()
                {
                    ErrorCode = ErrorCode.RestfulDeserializeError,
                    Tag       = ex
                };

                var error = new EitherAcronyms(errorMessage);
                return(error);
            }
        }