使用ASP.NET WebAPI消费Atlassian Webhook?

5
我被委托编写一个服务,用于接收来自JIRA的Atlassian Webhook发送的HTTP POST数据。我想使用WebAPI在4.0 .NET框架中消费该服务,想知道如何实现。我找到了这个资源,可以揭示POST数据的格式:JIRA Webhooks概述
我认为我可以编写一个WebAPI“Post”方法,将输入作为Dictionary<string, object>接受,但我希望映射到一个定义良好的对象,而不是一堆字典。
我看到有一个名为Atlassian.SDK的NuGet包,其中“包含与Atlassian JIRA交互的实用程序”。然而,它似乎更偏向于读取JIRA票据、创建和更新它们的API。 Atlassian.SDK NuGet包是否允许以强类型的方式消费Webhook发送的HTTP POST,而不是使用.NET Dictionary解析JSON数据?
1个回答

5
据我所知,Atlassian.SDK不支持Webhook场景。它通过Linq to Jira扩展和类支持REST API调用(查询和更新问题),正如你所写的那样。
然而,我认为你不应该使用.NET字典手动解析JSON。我已经采取了一些非常简单的步骤,在ApiController中生成强类型Webhook结果。
  1. I set up a temporary request container in Requestb.in (http://requestb.in/1g9lg8y1)

  2. I created a new ASP.NET MVC Application with Web API, when I started it in the browser, it was accessible on http://localhost:18177/

  3. I set up 2 webhooks in Jira pointing to these addresses Webhooks in Jira

  4. I created a new issue in Jira, and in the Requestb.in page I copied the whole JSON content to the clipboard found in raw body section.

  5. The main point is here: I created custom classes from this JSON content. (You can do this with any tool you want, e.g. JSON2Sharp), personally I did it with the Visual Studio's Web Essentials extension. (Edit -> Paste special -> Paste JSON as Classes)

  6. I renamed the root to JiraWebhookObject, and I modified the signature of the ValuesController's Post method to

    public void Post([FromBody]JiraWebhookObject value)
    
  7. Finally I modified the issue's summary. As a result of the updated webhook, I got the strongly-typed result in my Web API Controller. Strongly-typed webhook


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接