Ejemplo n.º 1
0
        /// <summary>
        /// GetChangedBooksList()
        /// </summary>
        /// <returns></returns>
        public async Task <List <BookViewModel> > GetChangedBooksList()
        {
            //string bearerToken = _context.Request.Headers["Authorization"].FirstOrDefault();
            //if (bearerToken == null)
            //    throw new Exception(String.Format("Cannot get books: No authentication token."));

            //bearerToken = bearerToken.Replace("Bearer", "");

            HttpResponseMessage response = null;
            var delayedRetry             = _apiServiceRetryWithDelay;

            await delayedRetry.RunAsync(async() =>
            {
                response = await _apiServiceHelper.GetAPI(
                    "http://localhost/BookLoan.Catalog.API/api/Book/ChangedBooksList",
                    null,
                    null
                    );
            });

            List <BookViewModel> bookViews = new List <BookViewModel>();

            if (response.IsSuccessStatusCode)
            {
                string respContent = response.Content.ReadAsStringAsync().Result;

                JArray books = JArray.Parse(respContent) as JArray;
                foreach (var item in books)
                {
                    BookViewModel bitem = JsonConvert.DeserializeObject <BookViewModel>(item.ToString());
                    bookViews.Add(bitem);
                }
            }
            else
            {
                string errorString = response.Headers.WwwAuthenticate.ToString().Replace("Bearer", "");
                if (!errorString.StartsWith("{") || !errorString.EndsWith("}"))
                {
                    errorString = "{ " + errorString + " }";
                }
                errorString = errorString.Replace("=", ":");
                ApiErrorResponse apiErrorResponse =
                    JsonConvert.DeserializeObject <ApiErrorResponse>(errorString);
                throw new AppException(apiErrorResponse.error_description);
            }

            if (bookViews == null)
            {
                throw new Exception(String.Format("Books cannot be found."));
            }

            return(bookViews);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// GetBooks()
        /// </summary>
        /// <returns></returns>
        public async Task <List <BookViewModel> > GetBooks()
        {
            //return await _db.Books.ToListAsync();

            string bearerToken = _context.Request.Headers["Authorization"].FirstOrDefault();

            if (bearerToken == null)
            {
                throw new Exception(String.Format("Cannot get books: No authentication token."));
            }

            bearerToken = bearerToken.Replace("Bearer", "");

            //_context.Request.Cookies["token"];
            //string bearerToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluQGJvb2tsb2FuLmNvbSIsIm5iZiI6MTU4MTQyNDM4MCwiZXhwIjoxNTgxNDI2MTgwLCJpYXQiOjE1ODE0MjQzODB9.CvATaOujiJZXTeAqG25u7Pos5h0wNLuSCtbHAVtCIew";

            HttpResponseMessage response = null;
            var delayedRetry             = _apiServiceRetryWithDelay;

            //http://localhost/BookLoan.Catalog.API/api/Book/List,

            await delayedRetry.RunAsync(async() =>
            {
                response = await _apiServiceHelper.GetAPI(
                    _appConfiguration.Value.UrlCatalogAPI + "api/Book/List",
                    null,
                    bearerToken
                    );
            });

            List <BookViewModel> bookViews = new List <BookViewModel>();

            if (response.IsSuccessStatusCode)
            {
                string respContent = response.Content.ReadAsStringAsync().Result;

                JArray books = JArray.Parse(respContent);
                foreach (var item in books)
                {
                    BookViewModel bitem = JsonConvert.DeserializeObject <BookViewModel>(item.ToString());
                    bookViews.Add(bitem);
                }
            }
            else
            {
                string errorString = response.Headers.WwwAuthenticate.ToString().Replace("Bearer", "");
                if (!errorString.StartsWith("{") || !errorString.EndsWith("}"))
                {
                    errorString = "{ " + errorString + " }";
                }
                errorString = errorString.Replace("=", ":");
                ApiErrorResponse apiErrorResponse =
                    JsonConvert.DeserializeObject <ApiErrorResponse>(errorString);
                throw new AppException(apiErrorResponse.error_description);
            }

            if (bookViews == null)
            {
                throw new Exception(String.Format("Books cannot be found."));
            }

            return(bookViews);
        }