Ejemplo n.º 1
0
        public GoodreadsBookSearchResult ParseResponse(HttpResponseMessage response)
        {
            var noMatch = new GoodreadsBookSearchResult()
            {
                NumResults = 0,
                BestMatch  = null
            };

            if (!response.IsSuccessStatusCode)
            {
                return(noMatch);
            }

            var body    = response.Content.ReadAsStringAsync().Result;
            var xmlRoot = XElement.Parse(body);

            var totalResults = int.Parse((string)xmlRoot.Descendants("total-results").First());

            var resultsElem = xmlRoot.Descendants("results").First();

            if (resultsElem.Elements().Count() == 0)
            {
                return(noMatch);
            }

            var bookElem = resultsElem.Element("work").Element("best_book");
            var match    = new GoodreadsBestMatch()
            {
                BookID          = int.Parse((string)bookElem.Element("id")),
                Author          = (string)bookElem.Element("author").Element("name"),
                Title           = (string)bookElem.Element("title"),
                IsReliableMatch = true
            };

            return(new GoodreadsBookSearchResult()
            {
                NumResults = totalResults,
                BestMatch = match
            });
        }
Ejemplo n.º 2
0
        public async Task <GoodreadsBookSearchResult> SearchBooksAsync(Item item)
        {
            var settings = ConfigurationManager.AppSettings;
            var baseUrl  = settings["GoodreadsSearchBaseUrl"];
            var apiKey   = settings["GoodreadsApiKey"];

            var search = new GoodreadsApiBookSearch()
            {
                BaseUrl = baseUrl,
                ApiKey  = apiKey,
                Query   = item.Title + " " + item.AuthorName
            };
            HttpRequestMessage request = search.CreateRequest();

            using (var client = new HttpClient())
            {
                HttpResponseMessage response = await client.SendAsync(request);

                response.EnsureSuccessStatusCode();
                GoodreadsBookSearchResult result = search.ParseResponse(response);
                return(result);
            }
        }