Skip to content

Commit 9c4ba66

Browse files
committed
Async and error handling.
1 parent 5a144b1 commit 9c4ba66

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/Umbraco.Cms.Integrations.Automation.Zapier/Controllers/ZapConfigController.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,13 @@ public IEnumerable<ContentTypeDto> GetContentTypes()
4545
}
4646

4747
[HttpPost]
48-
public string Add([FromBody] ContentConfigDto dto)
48+
public async Task<string> Add([FromBody] ContentConfigDto dto)
4949
{
5050
var result = _zapConfigService.Add(dto);
5151
if (!string.IsNullOrEmpty(result)) return result;
5252

53-
var t = Task.Run(async () => await _zapierService.TriggerAsync(dto.WebHookUrl,
54-
new Dictionary<string, string> {{Constants.Content.Name, dto.ContentTypeName}}));
55-
56-
result = t.Result;
57-
58-
if (!string.IsNullOrEmpty(result)) return result;
59-
60-
return result;
53+
return await _zapierService.TriggerAsync(dto.WebHookUrl,
54+
new Dictionary<string, string> { { Constants.Content.Name, dto.ContentTypeName } });
6155
}
6256

6357
[HttpGet]

src/Umbraco.Cms.Integrations.Automation.Zapier/Services/ZapierService.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,20 @@ public class ZapierService
1515

1616
public async Task<string> TriggerAsync(string requestUri, Dictionary<string, string> content)
1717
{
18-
var result = await ClientFactory().PostAsync(requestUri, new FormUrlEncodedContent(content));
18+
try
19+
{
20+
var result = await ClientFactory().PostAsync(requestUri, new FormUrlEncodedContent(content));
1921

20-
return result.IsSuccessStatusCode ? string.Empty : result.ReasonPhrase;
22+
return result.IsSuccessStatusCode ? string.Empty : result.ReasonPhrase;
23+
}
24+
catch (HttpRequestException)
25+
{
26+
return "Could not access the requested URL: " + requestUri;
27+
}
28+
catch (Exception)
29+
{
30+
throw;
31+
}
2132
}
2233
}
2334
}

0 commit comments

Comments
 (0)