@@ -92,59 +92,62 @@ public async Task<ResponseDto> GetAll()
92
92
if ( string . IsNullOrEmpty ( hubspotApiKey ) )
93
93
{
94
94
#if NETCOREAPP
95
- _logger . LogInformation ( message : LoggingResources . ApiKeyMissing ) ;
95
+ _logger . LogInformation ( message : Constants . ErrorMessages . ApiKeyMissing ) ;
96
96
#else
97
- _logger . Info < FormsController > ( message : LoggingResources . ApiKeyMissing ) ;
97
+ _logger . Info < FormsController > ( message : Constants . ErrorMessages . ApiKeyMissing ) ;
98
98
#endif
99
99
100
- return new ResponseDto { IsValid = false } ;
100
+ return new ResponseDto { IsValid = false , Error = Constants . ErrorMessages . ApiKeyMissing } ;
101
101
}
102
102
103
- var requestMessage = CreateRequest ( hubspotApiKey ) ;
103
+ string responseContent = string . Empty ;
104
+ try
105
+ {
106
+ var requestMessage = CreateRequest ( hubspotApiKey ) ;
104
107
105
- var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
108
+ var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
109
+
110
+ responseContent = await response . Content . ReadAsStringAsync ( ) ;
111
+
112
+ response . EnsureSuccessStatusCode ( ) ;
113
+
114
+ var forms = await response . Content . ReadAsStringAsync ( ) ;
106
115
107
- if ( response . StatusCode == HttpStatusCode . Unauthorized )
116
+ return new ResponseDto
117
+ {
118
+ IsValid = true ,
119
+ Forms = ParseForms ( forms ) . ToList ( )
120
+ } ;
121
+ }
122
+ catch ( HttpRequestException ex ) when ( ex . Message . Contains ( HttpStatusCode . Forbidden . ToString ( ) ) )
108
123
{
109
124
#if NETCOREAPP
110
- _logger . LogError ( string . Format ( LoggingResources . ApiFetchFormsFailed , response . ReasonPhrase ) ) ;
125
+ _logger . LogError ( string . Format ( LoggingResources . ApiFetchFormsFailed , responseContent ) ) ;
111
126
#else
112
- _logger . Error < FormsController > ( string . Format ( LoggingResources . ApiFetchFormsFailed , response . ReasonPhrase ) ) ;
127
+ _logger . Error < FormsController > ( string . Format ( LoggingResources . ApiFetchFormsFailed , responseContent ) ) ;
113
128
#endif
114
-
115
- return new ResponseDto { IsExpired = true } ;
129
+ return new ResponseDto { IsValid = false , Error = Constants . ErrorMessages . TokenPermissions } ;
116
130
}
117
-
118
- if ( response . IsSuccessStatusCode )
131
+ catch ( HttpRequestException ex ) when ( ex . Message . Contains ( HttpStatusCode . Unauthorized . ToString ( ) ) )
119
132
{
120
- var forms = await response . Content . ReadAsStringAsync ( ) ;
121
- var hubspotForms = HubspotForms . FromJson ( forms ) ;
133
+ #if NETCOREAPP
134
+ _logger . LogError ( string . Format ( LoggingResources . ApiFetchFormsFailed , responseContent ) ) ;
135
+ #else
136
+ _logger . Error < FormsController > ( string . Format ( LoggingResources . ApiFetchFormsFailed , responseContent ) ) ;
137
+ #endif
122
138
123
- var responseDto = new ResponseDto { IsValid = true } ;
124
- foreach ( var hubspotForm in hubspotForms )
125
- {
126
- var hubspotFormDto = new HubspotFormDto
127
- {
128
- Name = hubspotForm . Name ,
129
- PortalId = hubspotForm . PortalId . ToString ( ) ,
130
- Id = hubspotForm . Guid ,
131
- Region = Options . Region ,
132
- Fields = string . Join ( ", " , hubspotForm . FormFieldGroups . SelectMany ( x => x . Fields ) . Select ( y => y . Label ) )
133
- } ;
134
-
135
- responseDto . Forms . Add ( hubspotFormDto ) ;
136
- }
137
-
138
- return responseDto ;
139
+ return new ResponseDto { IsExpired = true , Error = Constants . ErrorMessages . InvalidApiKey } ;
139
140
}
140
-
141
+ catch
142
+ {
141
143
#if NETCOREAPP
142
- _logger . LogError ( string . Format ( LoggingResources . ApiFetchFormsFailed , response . StatusCode + " " + response . ReasonPhrase ) ) ;
144
+ _logger . LogError ( string . Format ( LoggingResources . ApiFetchFormsFailed , responseContent ) ) ;
143
145
#else
144
- _logger . Error < FormsController > ( string . Format ( LoggingResources . ApiFetchFormsFailed , response . StatusCode + " " + response . ReasonPhrase ) ) ;
146
+ _logger . Error < FormsController > ( string . Format ( LoggingResources . ApiFetchFormsFailed , responseContent ) ) ;
145
147
#endif
146
148
147
- return new ResponseDto ( ) ;
149
+ return new ResponseDto ( ) ;
150
+ }
148
151
}
149
152
150
153
public async Task < ResponseDto > GetAllOAuth ( )
@@ -153,58 +156,74 @@ public async Task<ResponseDto> GetAllOAuth()
153
156
if ( string . IsNullOrEmpty ( accessToken ) )
154
157
{
155
158
#if NETCOREAPP
156
- _logger . LogInformation ( LoggingResources . AccessTokenMissing ) ;
159
+ _logger . LogInformation ( Constants . ErrorMessages . AccessTokenMissing ) ;
157
160
#else
158
- _logger . Info < FormsController > ( LoggingResources . AccessTokenMissing ) ;
161
+ _logger . Info < FormsController > ( Constants . ErrorMessages . AccessTokenMissing ) ;
159
162
#endif
160
163
161
- return new ResponseDto { IsValid = false } ;
164
+ return new ResponseDto
165
+ {
166
+ IsValid = false ,
167
+ Error = Constants . ErrorMessages . OAuthFetchFormsConfigurationFailed
168
+ } ;
162
169
}
163
170
164
- var requestMessage = CreateRequest ( accessToken ) ;
165
-
166
- var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
167
- if ( response . StatusCode == HttpStatusCode . Unauthorized )
171
+ string responseContent = string . Empty ;
172
+ try
168
173
{
169
- #if NETCOREAPP
170
- _logger . LogError ( string . Format ( LoggingResources . OAuthFetchFormsFailed , response . ReasonPhrase ) ) ;
171
- #else
172
- _logger . Error < FormsController > ( string . Format ( LoggingResources . OAuthFetchFormsFailed , response . ReasonPhrase ) ) ;
173
- #endif
174
+ var requestMessage = CreateRequest ( accessToken ) ;
174
175
175
- return new ResponseDto { IsExpired = true } ;
176
- }
176
+ var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
177
177
178
- if ( response . IsSuccessStatusCode )
179
- {
180
- var forms = await response . Content . ReadAsStringAsync ( ) ;
178
+ responseContent = await response . Content . ReadAsStringAsync ( ) ;
181
179
182
- var hubspotForms = HubspotForms . FromJson ( forms ) ;
180
+ response . EnsureSuccessStatusCode ( ) ;
183
181
184
- var responseDto = new ResponseDto { IsValid = true } ;
185
- foreach ( var hubspotForm in hubspotForms )
182
+ var forms = await response . Content . ReadAsStringAsync ( ) ;
183
+
184
+ return new ResponseDto
186
185
{
187
- var hubspotFormDto = new HubspotFormDto
188
- {
189
- Name = hubspotForm . Name ,
190
- PortalId = hubspotForm . PortalId . ToString ( ) ,
191
- Id = hubspotForm . Guid ,
192
- Region = Options . Region ,
193
- Fields = string . Join ( ", " , hubspotForm . FormFieldGroups . SelectMany ( x => x . Fields ) . Select ( y => y . Label ) )
194
- } ;
195
- responseDto . Forms . Add ( hubspotFormDto ) ;
196
- }
197
-
198
- return responseDto ;
186
+ IsValid = true ,
187
+ Forms = ParseForms ( forms ) . ToList ( )
188
+ } ;
199
189
}
200
-
190
+ catch ( HttpRequestException ex ) when ( ex . Message . Contains ( HttpStatusCode . Unauthorized . ToString ( ) ) )
191
+ {
201
192
#if NETCOREAPP
202
- _logger . LogError ( string . Format ( LoggingResources . OAuthFetchFormsFailed , response . StatusCode + " " + response . ReasonPhrase ) ) ;
193
+ _logger . LogError ( string . Format ( LoggingResources . OAuthFetchFormsFailed , responseContent ) ) ;
203
194
#else
204
- _logger . Error < FormsController > ( string . Format ( LoggingResources . OAuthFetchFormsFailed , response . StatusCode + " " + response . ReasonPhrase ) ) ;
195
+ _logger . Error < FormsController > ( string . Format ( LoggingResources . OAuthFetchFormsFailed , responseContent ) ) ;
205
196
#endif
197
+ return new ResponseDto { IsExpired = true , Error = Constants . ErrorMessages . InvalidApiKey } ;
198
+ }
199
+ catch
200
+ {
201
+ #if NETCOREAPP
202
+ _logger . LogError ( string . Format ( LoggingResources . OAuthFetchFormsFailed , responseContent ) ) ;
203
+ #else
204
+ _logger . Error < FormsController > ( string . Format ( LoggingResources . OAuthFetchFormsFailed , responseContent ) ) ;
205
+ #endif
206
+
207
+ return new ResponseDto ( ) ;
208
+ }
209
+ }
206
210
207
- return new ResponseDto ( ) ;
211
+ private IEnumerable < HubspotFormDto > ParseForms ( string json )
212
+ {
213
+ var hubspotForms = HubspotForms . FromJson ( json ) ;
214
+ foreach ( var hubspotForm in hubspotForms )
215
+ {
216
+ var hubspotFormDto = new HubspotFormDto
217
+ {
218
+ Name = hubspotForm . Name ,
219
+ PortalId = hubspotForm . PortalId . ToString ( ) ,
220
+ Id = hubspotForm . Guid ,
221
+ Region = Options . Region ,
222
+ Fields = string . Join ( ", " , hubspotForm . FormFieldGroups . SelectMany ( x => x . Fields ) . Select ( y => y . Label ) )
223
+ } ;
224
+
225
+ yield return hubspotFormDto ;
226
+ }
208
227
}
209
228
210
229
private static HttpRequestMessage CreateRequest ( string accessToken )
@@ -223,12 +242,12 @@ public HubspotFormPickerSettings CheckConfiguration()
223
242
{
224
243
return
225
244
! string . IsNullOrEmpty ( Options . ApiKey )
226
- ? new HubspotFormPickerSettings { IsValid = true , Type = ConfigurationType . Api }
245
+ ? new HubspotFormPickerSettings { IsValid = true , Type = ConfigurationType . Api }
227
246
: ! string . IsNullOrEmpty ( OAuthClientId )
228
247
&& ! string . IsNullOrEmpty ( OAuthScopes )
229
248
&& ! string . IsNullOrEmpty ( OAuthProxyBaseUrl )
230
249
&& ! string . IsNullOrEmpty ( OAuthProxyEndpoint )
231
- ? new HubspotFormPickerSettings { IsValid = true , Type = ConfigurationType . OAuth }
250
+ ? new HubspotFormPickerSettings { IsValid = true , Type = ConfigurationType . OAuth }
232
251
: new HubspotFormPickerSettings ( ) ;
233
252
}
234
253
@@ -322,7 +341,12 @@ public async Task<ResponseDto> ValidateAccessToken()
322
341
{
323
342
_tokenService . TryGetParameters ( AccessTokenDbKey , out string accessToken ) ;
324
343
325
- if ( string . IsNullOrEmpty ( accessToken ) ) return new ResponseDto { IsValid = false } ;
344
+ if ( string . IsNullOrEmpty ( accessToken ) )
345
+ return new ResponseDto
346
+ {
347
+ IsValid = false ,
348
+ Error = Constants . ErrorMessages . OAuthInvalidToken
349
+ } ;
326
350
327
351
var requestMessage = new HttpRequestMessage
328
352
{
@@ -336,7 +360,8 @@ public async Task<ResponseDto> ValidateAccessToken()
336
360
return new ResponseDto
337
361
{
338
362
IsValid = response . IsSuccessStatusCode ,
339
- IsExpired = response . StatusCode == HttpStatusCode . Unauthorized
363
+ IsExpired = response . StatusCode == HttpStatusCode . Unauthorized ,
364
+ Error = ! response . IsSuccessStatusCode ? Constants . ErrorMessages . OAuthInvalidToken : string . Empty
340
365
} ;
341
366
}
342
367
0 commit comments