如何在 asp.net minimal api 中创建 Azure 事件网格 Webhook?

huangapple go评论73阅读模式
英文:

How to create an azure event grid webhook in asp.net minimal api?

问题

我有一个用于初始化数据库的端点,通过提供的路径读取和解析.tsv文件。

app.MapPost("/masterdata/initmediadb/{path}", async (
    string path,
    IMasterDataService masterDataService,
    IValidator<FilePath> validator,
    ILogger<Program> logger) =>
{
    var result = await validator.ValidateAsync(new FilePath { Path = path });
    if (!result.IsValid)
    {
        logger.LogError("Invalid path: {@path}, at initmediadb endpoint.", path);
        return Results.ValidationProblem(result.ToDictionary());
    }

    await masterDataService.InitMediaDatabaseAsync(path);
    return Results.Ok();
})
.RequireAuthorization("MustBeAdmin");

我想在Blob更新时调用它,就像我正在向Blob添加一个tsv文件,它会调用这个端点来读取、解析并保存到数据库。我应该使用哪种类型的事件,以及如何编写这样的端点?
备注:端点需要授权,授权由Okta处理,并在完成时返回一个令牌。
我目前处于这个阶段:

如何在 asp.net minimal api 中创建 Azure 事件网格 Webhook?

英文:

I have an endpoint to initialize the database by reading and parsing the .tsv file by the provided path

    app.MapPost(&quot;/masterdata/initmediadb/{path}&quot;, async (
    string path,
    IMasterDataService masterDataService,
    IValidator&lt;FilePath&gt; validator,
    ILogger&lt;Program&gt; logger) =&gt;
{
    var result = await validator.ValidateAsync(new FilePath { Path = path });
    if (!result.IsValid)
    {
        logger.LogError(&quot;Invalid path: {@path}, at initmediadb endpoint.&quot;, path);
        return Results.ValidationProblem(result.ToDictionary());
    }

    await masterDataService.InitMediaDatabaseAsync(path);
    return Results.Ok();
})
    .RequireAuthorization(&quot;MustBeAdmin&quot;);

I want to call it on the blob update, like I'am adding a tsv file to the blob, it calls this endpoint read, parse, and save to the database. What type of event should I use, and how to write such endpoint?
P.S: Endpoint requires authorization, authorization is handled by Okta, and returns a token when finished.
I am currently on this stage:
如何在 asp.net minimal api 中创建 Azure 事件网格 Webhook?

答案1

得分: 1

// 从 [WebHooks](https://www.dotnetcurry.com/aspnet/1245/aspnet-webhooks-receive-webhooks-from-github#:~:text=ASP.NET%20WebHooks%201%20The%20source%20calls%20the%20receiver%E2%80%99s,and%20then%20sends%20it%20to%20the%20WebHook%20handler) 创建了 asp.net minimal api 的 webhook。感谢 @dotnetcurry。

public class GitHubWebHookHandler : WebHookHandler
{
    public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)
    {
        if ("GitHub".Equals(receiver, StringComparison.CurrentCultureIgnoreCase))
        {
            string action = context.Actions.First();
            JObject data = context.GetDataOrDefault<JObject>();
            var dataAsString = Newtonsoft.Json.JsonConvert.SerializeObject(data);
        }

        return Task.FromResult(true);
    }
}

public class GitHubMessage
{
    public JObject[] Commits { get; set; }
    public JObject Issue { get; set; }
    public JObject Comment { get; set; }
    public string Action { get; set; }
    public GitHubActivityType ActivityType { get; private set; }
}
  • 若要阅读文件输入,请参考此链接
英文:
  • From WebHooks created webhook asp.net minimal api.Thank you @dotnetcurry.
public class GitHubWebHookHandler : WebHookHandler

{

public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)

{

if
(&quot;GitHub&quot;.Equals(receiver,StringComparison.CurrentCultureIgnoreCase))`

{

string action = context.Actions.First();

JObject data = context.GetDataOrDefault&lt;JObject&gt;();

var dataAsString = Newtonsoft.Json.JsonConvert.SerializeObject(data);`

}

return Task.FromResult(``true``);
}
}
public class GitHubMessage

{

public JObject[] Commits { get ; set ; }

public JObject Issue { get; set ; }

public JObject Comment { get ; set ; }

public string Action { get ; set ; }

public GitHubActivityType ActivityType { get ; private set; }

}

如何在 asp.net minimal api 中创建 Azure 事件网格 Webhook?

如何在 asp.net minimal api 中创建 Azure 事件网格 Webhook?

如何在 asp.net minimal api 中创建 Azure 事件网格 Webhook?

  • For Read input of the file refer to this link.

huangapple
  • 本文由 发表于 2023年6月27日 17:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定