< Summary - Syki

Information
Class: Syki.Back.Features.Academic.CallWebhooks.CallWebhookCommandHandler
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Academic/CallWebhooks/CallWebhookCommand.cs
Tag: 4_16869239191
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 46
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
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%
Handle()0%620%

File(s)

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

#LineLine coverage
 1using System.Text;
 2
 3namespace Syki.Back.Features.Academic.CallWebhooks;
 4
 5[CommandDescription("Chama um webhook")]
 6public record CallWebhookCommand(Guid WebhookCallId) : ICommand;
 7
 08public class CallWebhookCommandHandler(SykiDbContext ctx, IHttpClientFactory factory) : ICommandHandler<CallWebhookComma
 9{
 10    public async Task Handle(CommandId commandId, CallWebhookCommand command)
 11    {
 012        var call = await ctx.WebhookCalls
 013            .Include(x => x.Attempts)
 014            .FirstOrDefaultAsync(x => x.Id == command.WebhookCallId);
 15
 016        var webhook = await ctx.Webhooks.AsNoTracking()
 017            .Include(x => x.Authentication)
 018            .Where(x => x.Id == call.WebhookId)
 019            .Select(x => new { x.Url, x.Authentication.ApiKey })
 020            .FirstAsync();
 21
 022        var client = factory.CreateClient();
 023        client.BaseAddress = new Uri(webhook.Url);
 024        client.DefaultRequestHeaders.Add("Syki-Webhook-ApiKey", webhook.ApiKey);
 25
 26        try
 27        {
 028            var payload = new StringContent(call.Payload, Encoding.UTF8, "application/json");
 029            var response = await client.PostAsync("", payload);
 030            var responseContent = await response.Content.ReadAsStringAsync();
 31
 032            if (response.IsSuccessStatusCode)
 33            {
 034                call.Success((int)response.StatusCode, responseContent);
 35            }
 36            else
 37            {
 038                call.Failed((int)response.StatusCode, responseContent);
 39            }
 040        }
 041        catch (Exception ex)
 42        {
 043            call.Failed(999, ex.Message);
 044        }
 045    }
 46}