@@ -95,34 +95,20 @@ public async Task<IEnumerable<Property>> GetContactProperties()
95
95
}
96
96
97
97
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 ) ;
99
100
if ( response . IsSuccessStatusCode == false )
100
101
{
101
- if ( authenticationDetails . Mode == HubspotAuthenticationMode . OAuth )
102
+ var retryResult = await HandleFailedRequest ( response . StatusCode , requestUrl , httpMethod , authenticationDetails ) ;
103
+ if ( retryResult . Success )
102
104
{
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 > ( ) ;
122
111
}
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 > ( ) ;
126
112
}
127
113
128
114
// 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>
163
149
// POST data to hubspot
164
150
// https://api.hubapi.com/crm/v3/objects/contacts?hapikey=YOUR_HUBSPOT_API_KEY
165
151
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 ) ;
167
154
168
155
// Depending on POST status fail or mark workflow as completed
169
156
if ( response . IsSuccessStatusCode == false )
170
157
{
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 )
187
160
{
188
- HandleForbiddenResponse ( ) ;
161
+ return CommandResult . Success ;
189
162
}
190
-
191
- _logger . Error < HubspotContactService > ( "Error submitting a HubSpot contact request " ) ;
192
- return CommandResult . Failed ;
193
163
}
194
164
195
- return CommandResult . Success ;
165
+ _logger . Error < HubspotContactService > ( "Error submitting a HubSpot contact request " ) ;
166
+ return CommandResult . Failed ;
196
167
}
197
168
198
169
private bool TryGetConfiguredAuthenticationDetails ( out HubspotAuthentication authentication )
@@ -325,10 +296,46 @@ private static HttpContent CreateRequestContent(object data, string contentType)
325
296
}
326
297
}
327
298
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
+
328
328
private void HandleForbiddenResponse ( )
329
329
{
330
330
// Token is no longer valid (perhaps due to additional scopes requested). Need to clear and re-authenticate.
331
331
_keyValueService . SetValue ( RefreshTokenDatabaseKey , string . Empty ) ;
332
332
}
333
+
334
+ private class HandleFailedRequestResult
335
+ {
336
+ public bool Success { get ; set ; }
337
+
338
+ public HttpResponseMessage RetriedResponse { get ; set ; }
339
+ }
333
340
}
334
341
}
0 commit comments