< Summary - Estud

Information
Class: Estud.Back.Features.Webhooks.CallWebhooks.CallWebhookCommandHandler
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Webhooks/CallWebhooks/CallWebhookCommand.cs
Tag: 114_29044117136
Line coverage
86%
Covered lines: 20
Uncovered lines: 3
Coverable lines: 23
Total lines: 48
Line coverage: 86.9%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%
Handle()100%4486.36%

File(s)

/home/runner/work/syki/syki/Back/Features/Webhooks/CallWebhooks/CallWebhookCommand.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace Estud.Back.Features.Webhooks.CallWebhooks;
 4
 5[CommandDescription("Chama um webhook")]
 6public record CallWebhookCommand(int WebhookCallId) : ICommand;
 7
 188public class CallWebhookCommandHandler(EstudDbContext ctx, IHttpClientFactory factory) : ICommandHandler<CallWebhookComm
 9{
 10    public async Task Handle(int commandId, CallWebhookCommand command)
 11    {
 1812        var call = await ctx.WebhookCalls
 1813            .Include(x => x.Attempts)
 1814            .FirstOrDefaultAsync(x => x.Id == command.WebhookCallId);
 15
 1816        var webhook = await ctx.WebhookSubscriptions.AsNoTracking()
 1817            .Where(x => x.Id == call.WebhookSubscriptionId)
 1818            .Select(x => new { x.Url, x.CustomHeaders })
 1819            .FirstAsync();
 20
 1821        var client = factory.CreateClient();
 1822        client.BaseAddress = new Uri(webhook.Url);
 4423        foreach (var header in webhook.CustomHeaders)
 24        {
 425            client.DefaultRequestHeaders.Add(header.Key, header.Value);
 26        }
 27
 28        try
 29        {
 1830            var payload = new StringContent(call.Payload, Encoding.UTF8, "application/json");
 1831            var response = await client.PostAsync("", payload);
 1832            var responseContent = await response.Content.ReadAsStringAsync();
 33
 1834            if (response.IsSuccessStatusCode)
 35            {
 1636                call.Success((int)response.StatusCode, responseContent);
 37            }
 38            else
 39            {
 240                call.Failed((int)response.StatusCode, responseContent);
 41            }
 1842        }
 043        catch (Exception ex)
 44        {
 045            call.Failed(999, ex.Message);
 046        }
 1847    }
 48}