< Summary - Syki

Information
Class: Syki.Back.Features.Academic.CallWebhooks.CallWebhookCommand
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: 1
Coverable lines: 1
Total lines: 46
Line coverage: 0%
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
get_WebhookCallId()100%210%

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")]
 06public record CallWebhookCommand(Guid WebhookCallId) : ICommand;
 7
 8public class CallWebhookCommandHandler(SykiDbContext ctx, IHttpClientFactory factory) : ICommandHandler<CallWebhookComma
 9{
 10    public async Task Handle(CommandId commandId, CallWebhookCommand command)
 11    {
 12        var call = await ctx.WebhookCalls
 13            .Include(x => x.Attempts)
 14            .FirstOrDefaultAsync(x => x.Id == command.WebhookCallId);
 15
 16        var webhook = await ctx.Webhooks.AsNoTracking()
 17            .Include(x => x.Authentication)
 18            .Where(x => x.Id == call.WebhookId)
 19            .Select(x => new { x.Url, x.Authentication.ApiKey })
 20            .FirstAsync();
 21
 22        var client = factory.CreateClient();
 23        client.BaseAddress = new Uri(webhook.Url);
 24        client.DefaultRequestHeaders.Add("Syki-Webhook-ApiKey", webhook.ApiKey);
 25
 26        try
 27        {
 28            var payload = new StringContent(call.Payload, Encoding.UTF8, "application/json");
 29            var response = await client.PostAsync("", payload);
 30            var responseContent = await response.Content.ReadAsStringAsync();
 31
 32            if (response.IsSuccessStatusCode)
 33            {
 34                call.Success((int)response.StatusCode, responseContent);
 35            }
 36            else
 37            {
 38                call.Failed((int)response.StatusCode, responseContent);
 39            }
 40        }
 41        catch (Exception ex)
 42        {
 43            call.Failed(999, ex.Message);
 44        }
 45    }
 46}

Methods/Properties

get_WebhookCallId()