< Summary

Information
Class: Chronicis.Client.Services.QuoteService
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/QuoteService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 69
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetRandomQuoteAsync()0%2040%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/QuoteService.cs

#LineLine coverage
 1using System.Net.Http.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Chronicis.Client.Services;
 5
 6public class QuoteService : IQuoteService
 7{
 8    private readonly HttpClient _httpClient;
 9    private readonly ILogger<QuoteService> _logger;
 10
 011    public QuoteService(HttpClient httpClient, ILogger<QuoteService> logger)
 12    {
 013        _httpClient = httpClient;
 014        _logger = logger;
 015    }
 16
 17    public async Task<Quote?> GetRandomQuoteAsync()
 18    {
 19        try
 20        {
 21            // Use CORS proxy to bypass CORS restrictions
 022            var response = await _httpClient.GetFromJsonAsync<List<ZenQuote>>(
 023                "https://corsproxy.io/?https://zenquotes.io/api/random");
 24
 025            if (response?.Any() ?? false)
 26            {
 027                var zenQuote = response.First();
 028                return new Quote
 029                {
 030                    Content = zenQuote.Quote,
 031                    Author = zenQuote.Author
 032                };
 33            }
 34
 035            _logger.LogWarning("No quote received from Zen Quotes API");
 036        }
 037        catch (Exception ex)
 38        {
 039            _logger.LogError(ex, "Error fetching quote from Zen Quotes API");
 040        }
 41
 42        // Fallback quote if API fails
 043        return new Quote
 044        {
 045            Content = "The world is indeed full of peril, and in it there are many dark places; but still there is much 
 046            Author = "J.R.R. Tolkien"
 047        };
 048    }
 49}
 50
 51// Response model for Zen Quotes API
 52public class ZenQuote
 53{
 54    [JsonPropertyName("q")]
 55    public string Quote { get; set; } = string.Empty;
 56
 57    [JsonPropertyName("a")]
 58    public string Author { get; set; } = string.Empty;
 59
 60    [JsonPropertyName("h")]
 61    public string Html { get; set; } = string.Empty;
 62}
 63
 64// Our internal quote model
 65public class Quote
 66{
 67    public string Content { get; set; } = string.Empty;
 68    public string Author { get; set; } = string.Empty;
 69}