|
|
@ -1,6 +1,7 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Net; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Net.Http.Formatting; |
|
|
|
using System.Threading.Tasks; |
|
|
@ -20,6 +21,11 @@ namespace WebhookClient.Pages |
|
|
|
|
|
|
|
[BindProperty] public string Token { get; set; } |
|
|
|
|
|
|
|
public int ResponseCode { get; set; } |
|
|
|
public string RequestUrl { get; set; } |
|
|
|
public string GrantUrl { get; set; } |
|
|
|
public string ResponseMessage { get; set; } |
|
|
|
|
|
|
|
|
|
|
|
public RegisterWebhookModel(IOptions<Settings> settings, IHttpClientFactory httpClientFactory) |
|
|
|
{ |
|
|
@ -29,15 +35,21 @@ namespace WebhookClient.Pages |
|
|
|
|
|
|
|
public void OnGet() |
|
|
|
{ |
|
|
|
ResponseCode = (int)HttpStatusCode.OK; |
|
|
|
Token = _settings.Token; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task OnPost() |
|
|
|
public async Task<IActionResult> OnPost() |
|
|
|
{ |
|
|
|
ResponseCode = (int)HttpStatusCode.OK; |
|
|
|
var protocol = Request.IsHttps ? "https" : "http"; |
|
|
|
var selfurl = !string.IsNullOrEmpty(_settings.SelfUrl) ? _settings.SelfUrl : $"{protocol}://{Request.Host}/{Request.PathBase}"; |
|
|
|
if (!selfurl.EndsWith("/")) |
|
|
|
{ |
|
|
|
selfurl = selfurl + "/"; |
|
|
|
} |
|
|
|
var granturl = $"{selfurl}check"; |
|
|
|
var url = $"{selfurl}webhook"; |
|
|
|
var url = $"{selfurl}webhook-received"; |
|
|
|
var client = _httpClientFactory.CreateClient("GrantClient"); |
|
|
|
|
|
|
|
var payload = new WebhookSubscriptionRequest() |
|
|
@ -49,7 +61,19 @@ namespace WebhookClient.Pages |
|
|
|
}; |
|
|
|
var response = await client.PostAsync<WebhookSubscriptionRequest>(_settings.WebhooksUrl + "/api/v1/webhooks", payload, new JsonMediaTypeFormatter()); |
|
|
|
|
|
|
|
RedirectToPage("Index"); |
|
|
|
if (response.IsSuccessStatusCode) |
|
|
|
{ |
|
|
|
return RedirectToPage("WebhooksList"); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
ResponseCode = (int)response.StatusCode; |
|
|
|
ResponseMessage = response.ReasonPhrase; |
|
|
|
GrantUrl = granturl; |
|
|
|
RequestUrl = $"{response.RequestMessage.Method} {response.RequestMessage.RequestUri}"; |
|
|
|
} |
|
|
|
|
|
|
|
return Page(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |