Skip to content

Commit d8f14f0

Browse files
committed
DRYed up handling of failed responses and added notification on authorization.
1 parent 11f8a51 commit d8f14f0

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

src/Umbraco.Forms.Integrations.Crm.Hubspot/App_Plugins/UmbracoForms.Integrations/Crm/Hubspot/hubspotfields.component.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ function HubSpotFieldsController($routeParams, umbracoFormsIntegrationsCrmHubspo
6868
umbracoFormsIntegrationsCrmHubspotResource.authorize(vm.authorizationCode).then(function (response) {
6969
if (response.success) {
7070
vm.isAuthorizationConfigured = true;
71+
notificationsService.showNotification({
72+
type: 0,
73+
header: "Authorization succeeded",
74+
message: "Your Umbraco Forms installation is now connected to your HubSpot account",
75+
});
7176
getFieldsForMapping();
7277
} else {
7378
notificationsService.showNotification({

src/Umbraco.Forms.Integrations.Crm.Hubspot/Services/HubspotContactService.cs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,20 @@ public async Task<IEnumerable<Property>> GetContactProperties()
9595
}
9696

9797
var requestUrl = $"{CrmApiBaseUrl}properties/contacts";
98-
var response = await GetResponse(requestUrl, HttpMethod.Get, authenticationDetails).ConfigureAwait(false);
98+
var httpMethod = HttpMethod.Get;
99+
var response = await GetResponse(requestUrl, httpMethod, authenticationDetails).ConfigureAwait(false);
99100
if (response.IsSuccessStatusCode == false)
100101
{
101-
if (authenticationDetails.Mode == HubspotAuthenticationMode.OAuth)
102+
var retryResult = await HandleFailedRequest(response.StatusCode, requestUrl, httpMethod, authenticationDetails);
103+
if (retryResult.Success)
102104
{
103-
if (response.StatusCode == HttpStatusCode.Unauthorized)
104-
{
105-
// If we've got a 401 response and are using OAuth, likely our access token has expired.
106-
// First we should try to refresh it using the refresh token. If successful this will save the new
107-
// value into the cache.
108-
await RefreshOAuthAccessToken(authenticationDetails.RefreshToken);
109-
110-
// Repeat the operation using the refreshed token.
111-
response = await GetResponse(requestUrl, HttpMethod.Get, authenticationDetails).ConfigureAwait(false);
112-
if (response.IsSuccessStatusCode == false)
113-
{
114-
_logger.Error<HubspotContactService>("Failed to fetch contact properties from HubSpot API for mapping. {StatusCode} {ReasonPhrase}", response.StatusCode, response.ReasonPhrase);
115-
return Enumerable.Empty<Property>();
116-
}
117-
}
118-
else if (response.StatusCode == HttpStatusCode.Forbidden)
119-
{
120-
HandleForbiddenResponse();
121-
}
105+
response = retryResult.RetriedResponse;
106+
}
107+
else
108+
{
109+
_logger.Error<HubspotContactService>("Failed to fetch contact properties from HubSpot API for mapping. {StatusCode} {ReasonPhrase}", response.StatusCode, response.ReasonPhrase);
110+
return Enumerable.Empty<Property>();
122111
}
123-
124-
_logger.Error<HubspotContactService>("Failed to fetch contact properties from HubSpot API for mapping. {StatusCode} {ReasonPhrase}", response.StatusCode, response.ReasonPhrase);
125-
return Enumerable.Empty<Property>();
126112
}
127113

128114
// Map the properties to our simpler object, as we don't need all the fields in the response.
@@ -163,36 +149,21 @@ public async Task<CommandResult> PostContact(Record record, List<MappedProperty>
163149
// POST data to hubspot
164150
// https://api.hubapi.com/crm/v3/objects/contacts?hapikey=YOUR_HUBSPOT_API_KEY
165151
var requestUrl = $"{CrmApiBaseUrl}objects/contacts";
166-
var response = await GetResponse(requestUrl, HttpMethod.Post, authenticationDetails, postData, "application/json").ConfigureAwait(false);
152+
var httpMethod = HttpMethod.Post;
153+
var response = await GetResponse(requestUrl, httpMethod, authenticationDetails, postData, "application/json").ConfigureAwait(false);
167154

168155
// Depending on POST status fail or mark workflow as completed
169156
if (response.IsSuccessStatusCode == false)
170157
{
171-
if (response.StatusCode == HttpStatusCode.Unauthorized && authenticationDetails.Mode == HubspotAuthenticationMode.OAuth)
172-
{
173-
// If we've got a 401 response and are using OAuth, likely our access token has expired.
174-
// First we should try to refresh it using the refresh token. If successful this will save the new
175-
// value into the cache.
176-
await RefreshOAuthAccessToken(authenticationDetails.RefreshToken);
177-
178-
// Repeat the operation using the refreshed token.
179-
response = await GetResponse(requestUrl, HttpMethod.Post, authenticationDetails, postData, "application/json").ConfigureAwait(false);
180-
if (response.IsSuccessStatusCode == false)
181-
{
182-
_logger.Error<HubspotContactService>("Error submitting a HubSpot contact request ");
183-
return CommandResult.Failed;
184-
}
185-
}
186-
else if (response.StatusCode == HttpStatusCode.Forbidden)
158+
var retryResult = await HandleFailedRequest(response.StatusCode, requestUrl, httpMethod, authenticationDetails);
159+
if (retryResult.Success)
187160
{
188-
HandleForbiddenResponse();
161+
return CommandResult.Success;
189162
}
190-
191-
_logger.Error<HubspotContactService>("Error submitting a HubSpot contact request ");
192-
return CommandResult.Failed;
193163
}
194164

195-
return CommandResult.Success;
165+
_logger.Error<HubspotContactService>("Error submitting a HubSpot contact request ");
166+
return CommandResult.Failed;
196167
}
197168

198169
private bool TryGetConfiguredAuthenticationDetails(out HubspotAuthentication authentication)
@@ -325,10 +296,46 @@ private static HttpContent CreateRequestContent(object data, string contentType)
325296
}
326297
}
327298

299+
private async Task<HandleFailedRequestResult> HandleFailedRequest(HttpStatusCode statusCode, string requestUrl, HttpMethod httpMethod, HubspotAuthentication authenticationDetails)
300+
{
301+
var result = new HandleFailedRequestResult();
302+
if (authenticationDetails.Mode == HubspotAuthenticationMode.OAuth)
303+
{
304+
if (statusCode == HttpStatusCode.Unauthorized)
305+
{
306+
// If we've got a 401 response and are using OAuth, likely our access token has expired.
307+
// First we should try to refresh it using the refresh token. If successful this will save the new
308+
// value into the cache.
309+
await RefreshOAuthAccessToken(authenticationDetails.RefreshToken);
310+
311+
// Repeat the operation using the refreshed token.
312+
var response = await GetResponse(requestUrl, httpMethod, authenticationDetails).ConfigureAwait(false);
313+
if (response.IsSuccessStatusCode)
314+
{
315+
result.Success = true;
316+
result.RetriedResponse = response;
317+
}
318+
}
319+
else if (statusCode == HttpStatusCode.Forbidden)
320+
{
321+
HandleForbiddenResponse();
322+
}
323+
}
324+
325+
return result;
326+
}
327+
328328
private void HandleForbiddenResponse()
329329
{
330330
// Token is no longer valid (perhaps due to additional scopes requested). Need to clear and re-authenticate.
331331
_keyValueService.SetValue(RefreshTokenDatabaseKey, string.Empty);
332332
}
333+
334+
private class HandleFailedRequestResult
335+
{
336+
public bool Success { get; set; }
337+
338+
public HttpResponseMessage RetriedResponse { get; set; }
339+
}
333340
}
334341
}

0 commit comments

Comments
 (0)