< Summary

Information
Class: BookstoreAPI.Services.ExternalBookHttpClientException
Assembly: BookstoreAPI
File(s): /home/runner/work/dotnet-unit-test-webinar/dotnet-unit-test-webinar/BookstoreAPI/Services/ExternalBookHttpClient.cs
Line coverage
100%
Covered lines: 3
Uncovered lines: 0
Coverable lines: 3
Total lines: 55
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/home/runner/work/dotnet-unit-test-webinar/dotnet-unit-test-webinar/BookstoreAPI/Services/ExternalBookHttpClient.cs

#LineLine coverage
 1using BookstoreAPI.Models;
 2using System.Text.Json;
 3using BookstoreAPI.Services;
 4using System.Drawing;
 5
 6namespace BookstoreAPI.Services
 7{
 8    public class ExternalBookHttpClientException : Exception
 9    {
 210        public ExternalBookHttpClientException(string message, Exception innerException) : base(message, innerException)
 211        {
 212        }
 13    }
 14    public class ExternalBookHttpClient : IExternalBookHttpClient
 15    {
 16        private readonly HttpClient _httpClient;
 17
 18        public ExternalBookHttpClient(HttpClient httpClient)
 19        {
 20            _httpClient = httpClient;
 21        }
 22
 23        public async Task<Book?> FetchBookDetailsAsync(string isbn)
 24        {
 25            try
 26            {
 27                var response = await _httpClient.GetStringAsync($"https://www.googleapis.com/books/v1/volumes?q=isbn:{is
 28
 29                var bookData = JsonSerializer.Deserialize<JsonElement>(response);
 30
 31                var items = bookData.GetProperty("items");
 32
 33                if (items.GetArrayLength() == 0)
 34                {
 35                    return null;
 36                }
 37
 38                var volumeInfo = items[0].GetProperty("volumeInfo");
 39
 40                return new Book
 41                {
 42                    Title = volumeInfo.GetProperty("title").GetString(),
 43                    Author = volumeInfo.GetProperty("authors")[0].GetString(),
 44                    ISBN = isbn,
 45                    Description = volumeInfo.GetProperty("description").GetString()
 46                };
 47            }
 48            catch (HttpRequestException ex)
 49            {
 50                throw new ExternalBookHttpClientException("Failed to fetch book details from external API", ex);
 51            }
 52        }
 53    }
 54}
 55