Skip to content

Commit 2d382c6

Browse files
committed
Handle API responses generically
1 parent ebc5259 commit 2d382c6

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
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
@@ -1,11 +1,13 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.Extensions.Options;
34
using System.Net;
45
using System.Text.Json;
56
using System.Text.Json.Nodes;
67
using Umbraco.Cms.Api.Common.Attributes;
78
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Configuration;
89
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Models.Dtos;
10+
using Umbraco.Cms.Web.Common.Authorization;
911
using Umbraco.Cms.Web.Common.Routing;
1012

1113
namespace Umbraco.Cms.Integrations.Crm.ActiveCampaign.Api.Management.Controllers
@@ -29,7 +31,7 @@ public ActiveCampaignControllerBase(IOptions<ActiveCampaignSettings> options, IH
2931
HttpClientFactory = httpClientFactory;
3032
}
3133

32-
protected async Task<IActionResult> HandleResponseAsync(HttpResponseMessage? httpResponse)
34+
protected async Task<IActionResult> HandleResponseAsync<T>(HttpResponseMessage? httpResponse)
3335
{
3436
if (httpResponse is null)
3537
{
@@ -40,7 +42,7 @@ protected async Task<IActionResult> HandleResponseAsync(HttpResponseMessage? htt
4042

4143
if (httpResponse.IsSuccessStatusCode)
4244
{
43-
return Ok(new JsonResult(JsonSerializer.Deserialize<FormCollectionResponseDto>(content)));
45+
return Ok(JsonSerializer.Deserialize<T>(content));
4446
}
4547

4648
var responseMessage = content.Contains("message")

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Options;
5-
using System.Text.Json;
6-
using System.Text.Json.Nodes;
75
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Configuration;
86
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Models.Dtos;
97

@@ -19,6 +17,9 @@ public GetFormByIdController(IOptions<ActiveCampaignSettings> options, IHttpClie
1917

2018
[HttpGet("forms/{id}")]
2119
[ProducesResponseType(typeof(FormResponseDto), StatusCodes.Status200OK)]
20+
[ProducesResponseType(StatusCodes.Status404NotFound)]
21+
[ProducesResponseType(StatusCodes.Status403Forbidden)]
22+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
2223
public async Task<IActionResult> GetForm(string id)
2324
{
2425
var client = HttpClientFactory.CreateClient(Constants.FormsHttpClient);
@@ -30,7 +31,7 @@ public async Task<IActionResult> GetForm(string id)
3031
RequestUri = new Uri($"{client.BaseAddress}{ApiPath}/{id}")
3132
});
3233

33-
return await HandleResponseAsync(response);
34+
return await HandleResponseAsync<FormResponseDto>(response);
3435
}
3536
}
3637
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Options;
5-
using System.Text.Json;
6-
using System.Text.Json.Nodes;
75
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Configuration;
86
using Umbraco.Cms.Integrations.Crm.ActiveCampaign.Models.Dtos;
97

@@ -19,7 +17,10 @@ public GetFormsByPageController(IOptions<ActiveCampaignSettings> options, IHttpC
1917

2018
[HttpGet("forms")]
2119
[ProducesResponseType(typeof(FormCollectionResponseDto), StatusCodes.Status200OK)]
22-
public async Task<IActionResult> GetForms([FromQuery]int page = 1)
20+
[ProducesResponseType(StatusCodes.Status404NotFound)]
21+
[ProducesResponseType(StatusCodes.Status403Forbidden)]
22+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
23+
public async Task<IActionResult> GetForms([FromQuery]int? page = 1)
2324
{
2425
var client = HttpClientFactory.CreateClient(Constants.FormsHttpClient);
2526

@@ -35,7 +36,7 @@ public async Task<IActionResult> GetForms([FromQuery]int page = 1)
3536

3637
var response = await client.SendAsync(requestMessage);
3738

38-
return await HandleResponseAsync(response);
39+
return await HandleResponseAsync<FormCollectionResponseDto>(response);
3940
}
4041
}
4142
}

0 commit comments

Comments
 (0)