英文:
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处理,并在完成时返回一个令牌。
我目前处于这个阶段:
英文:
I have an endpoint to initialize the database by reading and parsing the .tsv file by the provided path
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");
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:
答案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
("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; }
}
- For Read input of the file refer to this link.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论