< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

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    {
 10        public ExternalBookHttpClientException(string message, Exception innerException) : base(message, innerException)
 11        {
 12        }
 13    }
 14    public class ExternalBookHttpClient : IExternalBookHttpClient
 15    {
 16        private readonly HttpClient _httpClient;
 17
 1618        public ExternalBookHttpClient(HttpClient httpClient)
 1619        {
 1620            _httpClient = httpClient;
 1621        }
 22
 23        public async Task<Book?> FetchBookDetailsAsync(string isbn)
 524        {
 25            try
 526            {
 527                var response = await _httpClient.GetStringAsync($"https://www.googleapis.com/books/v1/volumes?q=isbn:{is
 28
 329                var bookData = JsonSerializer.Deserialize<JsonElement>(response);
 30
 331                var items = bookData.GetProperty("items");
 32
 333                if (items.GetArrayLength() == 0)
 134                {
 135                    return null;
 36                }
 37
 238                var volumeInfo = items[0].GetProperty("volumeInfo");
 39
 240                return new Book
 241                {
 242                    Title = volumeInfo.GetProperty("title").GetString(),
 243                    Author = volumeInfo.GetProperty("authors")[0].GetString(),
 244                    ISBN = isbn,
 245                    Description = volumeInfo.GetProperty("description").GetString()
 246                };
 47            }
 248            catch (HttpRequestException ex)
 249            {
 250                throw new ExternalBookHttpClientException("Failed to fetch book details from external API", ex);
 51            }
 352        }
 53    }
 54}
 55