11using System ;
22using System . Collections . Generic ;
3- using System . Configuration ;
43using System . Linq ;
54using System . Net . Http ;
65using System . Net . Http . Headers ;
1312using Umbraco . Cms . Integrations . Crm . Dynamics . Models . Dtos ;
1413using Umbraco . Cms . Integrations . Crm . Dynamics . Models ;
1514
16-
1715#if NETCOREAPP
16+ using Microsoft . Extensions . Logging ;
1817using Microsoft . Extensions . Options ;
1918#else
19+ using System . Configuration ;
20+ using Umbraco . Core . Logging ;
2021#endif
2122
2223namespace Umbraco . Cms . Integrations . Crm . Dynamics . Services
2324{
24- public class DynamicsService
25+ public class DynamicsService
2526 {
2627 private readonly DynamicsSettings _settings ;
2728
@@ -34,18 +35,26 @@ public class DynamicsService
3435 public static Func < HttpClient > ClientFactory = ( ) => s_client ;
3536
3637#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 )
3841 {
3942 _settings = options . Value ;
4043
4144 _dynamicsConfigurationService = dynamicsConfigurationService ;
45+
46+ _logger = logger ;
4247 }
4348#else
44- public DynamicsService ( DynamicsConfigurationService dynamicsConfigurationService )
49+ private readonly ILogger _logger ;
50+
51+ public DynamicsService ( DynamicsConfigurationService dynamicsConfigurationService , ILogger logger )
4552 {
4653 _settings = new DynamicsSettings ( ConfigurationManager . AppSettings ) ;
4754
4855 _dynamicsConfigurationService = dynamicsConfigurationService ;
56+
57+ _logger = logger ;
4958 }
5059#endif
5160
@@ -133,23 +142,30 @@ public async Task<IEnumerable<FormDto>> GetForms(DynamicsModule module)
133142 if ( module . HasFlag ( DynamicsModule . Outbound ) )
134143 {
135144 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 ( ) )
137147 {
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+ }
142155 }
143156
144157 if ( module . HasFlag ( DynamicsModule . RealTime ) )
145158 {
146159 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 ( ) )
148161 {
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+ }
153169 }
154170
155171 return list ;
@@ -184,9 +200,20 @@ private async Task<ResponseDto<T>> Get<T>(string accessToken, string modulePath)
184200
185201 var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
186202
187- if ( ! response . IsSuccessStatusCode ) return null ;
203+ var result = await response . Content . ReadAsStringAsync ( ) ;
188204
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+ }
190217
191218 return JsonConvert . DeserializeObject < ResponseDto < T > > ( result ) ;
192219 }
0 commit comments