use form data
This commit is contained in:
29
Main.cs
29
Main.cs
@@ -14,30 +14,39 @@ using System.Linq;
|
|||||||
|
|
||||||
namespace bundlephobia
|
namespace bundlephobia
|
||||||
{
|
{
|
||||||
public class SlackRequest
|
|
||||||
{
|
|
||||||
public string text { get; set; }
|
|
||||||
}
|
|
||||||
public static class Main
|
public static class Main
|
||||||
{
|
{
|
||||||
static HttpClient Client = new HttpClient();
|
static HttpClient Client = new HttpClient();
|
||||||
[FunctionName("Main")]
|
[FunctionName("Main")]
|
||||||
public static async Task<IActionResult> Run(
|
public static async Task Run(
|
||||||
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] SlackRequest req,
|
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest httpReq,
|
||||||
HttpRequest httpReq,
|
|
||||||
ILogger log)
|
ILogger log)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
var text = httpReq.Form["text"];
|
||||||
|
var responseUrl = httpReq.Form["response_url"];
|
||||||
|
if (string.IsNullOrWhiteSpace(text))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(text));
|
||||||
|
}
|
||||||
|
if (string.IsNullOrWhiteSpace(responseUrl))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(text));
|
||||||
|
}
|
||||||
|
httpReq.HttpContext.Response.Clear();
|
||||||
|
log.LogInformation("form data", httpReq.Form);
|
||||||
|
log.LogInformation("body data", httpReq.Body);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var resp = await Client.GetAsync($"https://bundlephobia.com/api/package-history?package={req.text}");
|
var resp = await Client.GetAsync($"https://bundlephobia.com/api/package-history?package={text}");
|
||||||
var respData = JsonConvert.DeserializeObject<Dictionary<string, object>>(await resp.Content.ReadAsStringAsync());
|
var respData = JsonConvert.DeserializeObject<Dictionary<string, object>>(await resp.Content.ReadAsStringAsync());
|
||||||
var version = respData.Keys.OrderByDescending(a => a).First();
|
var version = respData.Keys.OrderByDescending(a => a).First();
|
||||||
return new JsonResult(new { text = $"https://bundlephobia.com/result?p={req.text}@{version}" });
|
await Client.PostAsJsonAsync(responseUrl, new { text = $"https://bundlephobia.com/result?p={text}@{version}" });
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
await Client.PostAsJsonAsync(responseUrl, new { text = $"😢 Something went wrong" });
|
||||||
log.LogError(e, "error");
|
log.LogError(e, "error");
|
||||||
return new JsonResult(new { text = $"Something went wrong" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user