private async Task <HttpResponseMessage> AskForBook(IBookSite bookSite, string isbn) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("User-Agent", "User-Agent-Here"); return(await client.GetAsync(bookSite.SearchUri + isbn)); } }
private async Task <BookPriceObject?> GetBookData(IBookSite bookSite, string isbn) { var htmlDocument = new HtmlDocument(); var response = await this.AskForBook(bookSite, isbn); htmlDocument.LoadHtml(await response.Content.ReadAsStringAsync()); var nodeWithValue = htmlDocument.DocumentNode.SelectNodes(bookSite.XPath)?.FirstOrDefault(); try { return(bookSite.GetValueFromHtmlNode(nodeWithValue)); } catch (ArgumentNullException) { Console.WriteLine($"Could not find book with: {isbn} in {bookSite.GetType().Name}"); return(null); } catch (NullReferenceException) { Console.WriteLine($"Could not find book with: {isbn} in {bookSite.GetType().Name}"); return(null); } }