1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Configuration ;
4
3
using System . Linq ;
5
4
using System . Net . Http ;
6
5
using System . Net . Http . Headers ;
13
12
using Umbraco . Cms . Integrations . Crm . Dynamics . Models . Dtos ;
14
13
using Umbraco . Cms . Integrations . Crm . Dynamics . Models ;
15
14
16
-
17
15
#if NETCOREAPP
16
+ using Microsoft . Extensions . Logging ;
18
17
using Microsoft . Extensions . Options ;
19
18
#else
19
+ using System . Configuration ;
20
+ using Umbraco . Core . Logging ;
20
21
#endif
21
22
22
23
namespace Umbraco . Cms . Integrations . Crm . Dynamics . Services
23
24
{
24
- public class DynamicsService
25
+ public class DynamicsService
25
26
{
26
27
private readonly DynamicsSettings _settings ;
27
28
@@ -34,18 +35,26 @@ public class DynamicsService
34
35
public static Func < HttpClient > ClientFactory = ( ) => s_client ;
35
36
36
37
#if NETCOREAPP
37
- public DynamicsService ( IOptions < DynamicsSettings > options , DynamicsConfigurationService dynamicsConfigurationService )
38
+ private readonly ILogger < DynamicsService > _logger ;
39
+
40
+ public DynamicsService ( IOptions < DynamicsSettings > options , DynamicsConfigurationService dynamicsConfigurationService , ILogger < DynamicsService > logger )
38
41
{
39
42
_settings = options . Value ;
40
43
41
44
_dynamicsConfigurationService = dynamicsConfigurationService ;
45
+
46
+ _logger = logger ;
42
47
}
43
48
#else
44
- public DynamicsService ( DynamicsConfigurationService dynamicsConfigurationService )
49
+ private readonly ILogger _logger ;
50
+
51
+ public DynamicsService ( DynamicsConfigurationService dynamicsConfigurationService , ILogger logger )
45
52
{
46
53
_settings = new DynamicsSettings ( ConfigurationManager . AppSettings ) ;
47
54
48
55
_dynamicsConfigurationService = dynamicsConfigurationService ;
56
+
57
+ _logger = logger ;
49
58
}
50
59
#endif
51
60
@@ -187,13 +196,24 @@ private async Task<ResponseDto<T>> Get<T>(string accessToken, string modulePath)
187
196
RequestUri = new Uri ( $ "{ _settings . HostUrl } { _settings . ApiPath } { modulePath } ")
188
197
} ;
189
198
requestMessage . Headers . Authorization =
190
- new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
199
+ new AuthenticationHeaderValue ( "Bearer" , accessToken + "1" ) ;
191
200
192
201
var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
193
202
194
- if ( ! response . IsSuccessStatusCode ) return null ;
203
+ var result = await response . Content . ReadAsStringAsync ( ) ;
195
204
196
- var result = await response . Content . ReadAsStringAsync ( ) ;
205
+ if ( ! response . IsSuccessStatusCode )
206
+ {
207
+ var errorMessage = string . Format ( "An error has occured while trying to retrieve the Dynamics {0} forms: {1} {2}" ,
208
+ modulePath , response . ReasonPhrase , result ) ;
209
+ #if NETCOREAPP
210
+ _logger . LogError ( errorMessage ) ;
211
+ #else
212
+ _logger . Error < DynamicsService > ( errorMessage ) ;
213
+ #endif
214
+
215
+ return null ;
216
+ }
197
217
198
218
return JsonConvert . DeserializeObject < ResponseDto < T > > ( result ) ;
199
219
}
0 commit comments