Skip to content

Commit 690506d

Browse files
committed
Add ProblemDetailsBuilder to handle HTTP exceptions and update versioning
1 parent 3435b7b commit 690506d

File tree

7 files changed

+574
-540
lines changed

7 files changed

+574
-540
lines changed

src/Umbraco.Cms.Integrations.Crm.ActiveCampaign/Api/Management/Controllers/ActiveCampaignControllerBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
using System.Text.Json;
66
using System.Text.Json.Nodes;
77
using Umbraco.Cms.Api.Common.Attributes;
8+
using Umbraco.Cms.Api.Common.Builders;
89
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Configuration;
9-
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Models.Dtos;
1010
using Umbraco.Cms.Web.Common.Authorization;
1111
using Umbraco.Cms.Web.Common.Routing;
1212

1313
namespace Umbraco.Cms.Integrations.Crm.ActiveCampaign.Api.Management.Controllers
1414
{
1515
[ApiController]
1616
[BackOfficeRoute($"{Constants.ManagementApi.RootPath}/v{{version:apiVersion}}")]
17-
//[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
17+
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
1818
[MapToApi(Constants.ManagementApi.ApiName)]
1919
public class ActiveCampaignControllerBase : Controller
2020
{
@@ -55,7 +55,9 @@ protected async Task<IActionResult> HandleResponseAsync<T>(HttpResponseMessage?
5555
? Constants.Resources.ApiAccessFailed
5656
: responseMessage;
5757

58-
return StatusCode((int)httpResponse.StatusCode, message);
58+
return StatusCode((int)httpResponse.StatusCode, new ProblemDetailsBuilder()
59+
.WithTitle(message)
60+
.Build());
5961
}
6062
}
6163
}

src/Umbraco.Cms.Integrations.Crm.ActiveCampaign/Api/Management/Controllers/GetFormByIdController.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Options;
5+
using Umbraco.Cms.Api.Common.Builders;
56
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Configuration;
67
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Models.Dtos;
78

@@ -22,16 +23,25 @@ public GetFormByIdController(IOptions<ActiveCampaignSettings> options, IHttpClie
2223
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
2324
public async Task<IActionResult> GetForm(string id)
2425
{
25-
var client = HttpClientFactory.CreateClient(Constants.FormsHttpClient);
26+
try
27+
{
28+
var client = HttpClientFactory.CreateClient(Constants.FormsHttpClient);
2629

27-
var response = await client.SendAsync(
28-
new HttpRequestMessage
29-
{
30-
Method = HttpMethod.Get,
31-
RequestUri = new Uri($"{client.BaseAddress}{ApiPath}/{id}")
32-
});
30+
var response = await client.SendAsync(
31+
new HttpRequestMessage
32+
{
33+
Method = HttpMethod.Get,
34+
RequestUri = new Uri($"{client.BaseAddress}{ApiPath}/{id}")
35+
});
3336

34-
return await HandleResponseAsync<FormResponseDto>(response);
37+
return await HandleResponseAsync<FormResponseDto>(response);
38+
}
39+
catch (Exception ex)
40+
{
41+
return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetailsBuilder()
42+
.WithTitle(ex.Message)
43+
.Build());
44+
}
3545
}
3646
}
3747
}

src/Umbraco.Cms.Integrations.Crm.ActiveCampaign/Api/Management/Controllers/GetFormsPagedController.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Options;
5+
using Umbraco.Cms.Api.Common.Builders;
56
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Configuration;
67
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Models.Dtos;
78

@@ -20,23 +21,32 @@ public GetFormsByPageController(IOptions<ActiveCampaignSettings> options, IHttpC
2021
[ProducesResponseType(StatusCodes.Status404NotFound)]
2122
[ProducesResponseType(StatusCodes.Status403Forbidden)]
2223
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
23-
public async Task<IActionResult> GetForms([FromQuery]int? page = 1)
24+
public async Task<IActionResult> GetForms([FromQuery] int? page = 1)
2425
{
25-
var client = HttpClientFactory.CreateClient(Constants.FormsHttpClient);
26+
try
27+
{
28+
var client = HttpClientFactory.CreateClient(Constants.FormsHttpClient);
2629

27-
var requestUriString = page == 1
28-
? $"{client.BaseAddress}{ApiPath}&limit={Constants.DefaultPageSize}"
29-
: $"{client.BaseAddress}{ApiPath}&limit={Constants.DefaultPageSize}&offset={(page - 1) * Constants.DefaultPageSize}";
30+
var requestUriString = page == 1
31+
? $"{client.BaseAddress}{ApiPath}&limit={Constants.DefaultPageSize}"
32+
: $"{client.BaseAddress}{ApiPath}&limit={Constants.DefaultPageSize}&offset={(page - 1) * Constants.DefaultPageSize}";
3033

31-
var requestMessage = new HttpRequestMessage
32-
{
33-
RequestUri = new Uri(requestUriString),
34-
Method = HttpMethod.Get
35-
};
34+
var requestMessage = new HttpRequestMessage
35+
{
36+
RequestUri = new Uri(requestUriString),
37+
Method = HttpMethod.Get
38+
};
3639

37-
var response = await client.SendAsync(requestMessage);
40+
var response = await client.SendAsync(requestMessage);
3841

39-
return await HandleResponseAsync<FormCollectionResponseDto>(response);
42+
return await HandleResponseAsync<FormCollectionResponseDto>(response);
43+
}
44+
catch (Exception ex)
45+
{
46+
return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetailsBuilder()
47+
.WithTitle(ex.Message)
48+
.Build());
49+
}
4050
}
4151
}
4252
}

0 commit comments

Comments
 (0)