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
@@ -133,23 +142,30 @@ public async Task<IEnumerable<FormDto>> GetForms(DynamicsModule module)
133
142
if ( module . HasFlag ( DynamicsModule . Outbound ) )
134
143
{
135
144
var forms = await Get < OutboundFormDto > ( oauthConfiguration . AccessToken , Constants . Modules . OutboundPath ) ;
136
- list . AddRange ( forms . Value . Select ( p => new FormDto
145
+
146
+ if ( forms != null && forms . Value != null && forms . Value . Any ( ) )
137
147
{
138
- Id = p . Id ,
139
- Name = p . Name ,
140
- Module = DynamicsModule . Outbound
141
- } ) ) ;
148
+ list . AddRange ( forms . Value . Select ( p => new FormDto
149
+ {
150
+ Id = p . Id ,
151
+ Name = p . Name ,
152
+ Module = DynamicsModule . Outbound
153
+ } ) ) ;
154
+ }
142
155
}
143
156
144
157
if ( module . HasFlag ( DynamicsModule . RealTime ) )
145
158
{
146
159
var forms = await Get < RealTimeFormDto > ( oauthConfiguration . AccessToken , Constants . Modules . RealTimePath ) ;
147
- list . AddRange ( forms . Value . Select ( p => new FormDto
160
+ if ( forms != null && forms . Value != null && forms . Value . Any ( ) )
148
161
{
149
- Id = p . Id ,
150
- Name = p . Name ,
151
- Module = DynamicsModule . RealTime
152
- } ) ) ;
162
+ list . AddRange ( forms . Value . Select ( p => new FormDto
163
+ {
164
+ Id = p . Id ,
165
+ Name = p . Name ,
166
+ Module = DynamicsModule . RealTime
167
+ } ) ) ;
168
+ }
153
169
}
154
170
155
171
return list ;
@@ -184,9 +200,20 @@ private async Task<ResponseDto<T>> Get<T>(string accessToken, string modulePath)
184
200
185
201
var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
186
202
187
- if ( ! response . IsSuccessStatusCode ) return null ;
203
+ var result = await response . Content . ReadAsStringAsync ( ) ;
188
204
189
- 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
+ }
190
217
191
218
return JsonConvert . DeserializeObject < ResponseDto < T > > ( result ) ;
192
219
}
0 commit comments