示例#1
0
        /// <summary>
        /// Search by title.
        /// </summary>
        /// <param name="sendMessage"></param>
        /// <returns>HttpResponseMessage</returns>
        public async Task <string> Search(GoogleAPIFilters filters)
        {
            var baseUrl = "https://www.googleapis.com/books/v1/volumes?q=";

            if (filters.FilterByTitle != null && filters.FilterByTitle != "")
            {
                  {
                    baseUrl += "intitle:" + filters.FilterByTitle + "+";
                }
            }
            if (filters.FilterByAuthor != null && filters.FilterByAuthor != "")
            {
                  {
                    baseUrl += "inauthor:" + filters.FilterByAuthor + "+";
                }
            }
            Log.ForContext <GoogleAPIClientService>().Information(JsonConvert.SerializeObject(baseUrl));
            var result = await _httpClient.GetAsync(baseUrl);

            return(await result.Content.ReadAsStringAsync());
        }
示例#2
0
        public async Task <IActionResult> GetFromGoogle([FromQuery(Name = "title")] string qTitle, [FromQuery(Name = "author")] string qAuthor)
        {
            var booksList = new List <BookDTO>()
            {
            };

            try {
                var filters = new GoogleAPIFilters()
                {
                    FilterByTitle  = qTitle,
                    FilterByAuthor = qAuthor
                };
                dynamic result = JsonConvert.DeserializeObject(await _googleApiClientService.Search(filters));
                foreach (var item in result.items)
                {
                    var book = new BookDTO()
                    {
                    };
                    book.AverageRating = item.volumeInfo.averageRating;
                    var authors = new List <string>()
                    {
                    };
                    if (item.volumeInfo.authors != null)
                    {
                        foreach (string author in item.volumeInfo.authors)
                        {
                            authors.Add(author);
                        }
                    }

                    book.Authors    = authors;
                    book.ImageLinks = new ImageLinkDTO()
                    {
                        SmallThumbnail = item?.volumeInfo?.imageLinks?.smallThumbnail,
                        Thumbnail      = item?.volumeInfo?.imageLinks?.thumbnail,
                    };
                    var identifier = new List <IndustryIdentifierDTO>()
                    {
                    };
                    if (item.volumeInfo.industryIdentifiers != null)
                    {
                        foreach (var identifierItem in item.volumeInfo.industryIdentifiers)
                        {
                            var id = new IndustryIdentifierDTO()
                            {
                                Identifier = identifierItem.identifier,
                                Type       = identifierItem.type,
                            };
                            identifier.Add(id);
                        }
                    }
                    book.IndustryIdentifiers = identifier;
                    book.InfoLink            = item.volumeInfo.infoLink;
                    book.Language            = item.volumeInfo.language;
                    book.PageCount           = item.volumeInfo.pageCount;
                    book.PreviewLink         = item.volumeInfo.previewLink;
                    book.PrintType           = item.volumeInfo.printType;
                    book.PublishedDate       = item.volumeInfo.publishedDate;
                    book.Publisher           = item.volumeInfo.publisher;
                    book.RatingsCount        = item.volumeInfo.ratingsCount;
                    book.Title = item.volumeInfo.title;
                    booksList.Add(book);
                    await _bookService.PostBook(book);
                }
            }
            catch (AlreadyInDBException e) {
                Console.WriteLine(e);
                return(Problem("AlreadyInDBException: " + e.Message));
            }
            catch (NotFoundException e) {
                Console.WriteLine(e);
                return(NotFound("NotFoundException: " + e.Message));
            }
            catch (BadRequestException e) {
                Console.WriteLine(e);
                return(BadRequest("BadRequestException: " + e.Message));
            }
            catch (Exception e) {
                Console.WriteLine(e);
                return(Problem("Exception: " + e.Message));
            }
            return(Ok(booksList));
        }