55using System . Threading . Tasks ;
66
77using Newtonsoft . Json ;
8-
8+ using Umbraco . Cms . Integrations . Commerce . Shopify . Configuration ;
99using Umbraco . Cms . Integrations . Commerce . Shopify . Models . Dtos ;
10- using Umbraco . Cms . Integrations . Shared . Configuration ;
1110using Umbraco . Cms . Integrations . Shared . Models ;
1211using Umbraco . Cms . Integrations . Shared . Models . Dtos ;
1312using Umbraco . Cms . Integrations . Shared . Resolvers ;
1413using Umbraco . Cms . Integrations . Shared . Services ;
14+ using Umbraco . Cms . Integrations . Commerce . Shopify . Resources ;
15+
16+ #if NETCOREAPP
17+ using Microsoft . Extensions . Logging ;
18+ using Microsoft . Extensions . Options ;
19+
20+ #else
21+ using System . Configuration ;
1522using Umbraco . Core . Logging ;
23+ #endif
1624
1725namespace Umbraco . Cms . Integrations . Commerce . Shopify . Services
1826{
1927 public class ShopifyService : BaseService , IApiService < ProductsListDto >
2028 {
2129 private readonly JsonSerializerSettings _serializerSettings ;
2230
23- public ShopifyService ( ILogger logger , IAppSettings appSettings , ITokenService tokenService ) : base ( logger , appSettings , tokenService )
31+ private ShopifySettings Options ;
32+
33+ #if NETCOREAPP
34+ public ShopifyService ( ILogger < ShopifyService > logger , IOptions < ShopifySettings > options , ITokenService tokenService ) : base ( logger , tokenService )
2435 {
2536 var resolver = new JsonPropertyRenameContractResolver ( ) ;
2637 resolver . RenameProperty ( typeof ( ResponseDto < ProductsListDto > ) , "Result" , "products" ) ;
@@ -29,16 +40,32 @@ public ShopifyService(ILogger logger, IAppSettings appSettings, ITokenService to
2940 {
3041 ContractResolver = resolver
3142 } ;
43+
44+ Options = options . Value ;
45+ }
46+ #else
47+ public ShopifyService ( ILogger logger , ITokenService tokenService ) : base ( logger , tokenService )
48+ {
49+ var resolver = new JsonPropertyRenameContractResolver ( ) ;
50+ resolver . RenameProperty ( typeof ( ResponseDto < ProductsListDto > ) , "Result" , "products" ) ;
51+
52+ _serializerSettings = new JsonSerializerSettings
53+ {
54+ ContractResolver = resolver
55+ } ;
56+
57+ Options = new ShopifySettings ( ConfigurationManager . AppSettings ) ;
3258 }
59+ #endif
3360
3461 public EditorSettings GetApiConfiguration ( )
3562 {
36- if ( string . IsNullOrEmpty ( AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] )
37- || string . IsNullOrEmpty ( AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyApiVersion ] ) )
63+ if ( string . IsNullOrEmpty ( Options . Shop )
64+ || string . IsNullOrEmpty ( Options . ApiVersion ) )
3865 return new EditorSettings ( ) ;
3966
4067 return
41- ! string . IsNullOrEmpty ( AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyAccessToken ] )
68+ ! string . IsNullOrEmpty ( Options . AccessToken )
4269 ? new EditorSettings { IsValid = true , Type = ConfigurationType . Api }
4370 : ! string . IsNullOrEmpty ( SettingsService . OAuthClientId )
4471 && ! string . IsNullOrEmpty ( OAuthProxyBaseUrl )
@@ -51,7 +78,7 @@ public string GetAuthorizationUrl()
5178 {
5279 return
5380 string . Format ( SettingsService . AuthorizationUrl ,
54- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] , SettingsService . OAuthClientId , SettingsService . ShopifyOAuthProxyUrl ) ;
81+ Options . Shop , SettingsService . OAuthClientId , SettingsService . ShopifyOAuthProxyUrl ) ;
5582 }
5683
5784 public async Task < string > GetAccessToken ( OAuthRequestDto request )
@@ -70,8 +97,7 @@ public async Task<string> GetAccessToken(OAuthRequestDto request)
7097 Content = new FormUrlEncodedContent ( data )
7198 } ;
7299 requestMessage . Headers . Add ( "service_name" , SettingsService . ServiceName ) ;
73- requestMessage . Headers . Add ( SettingsService . ServiceAddressReplace ,
74- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] ) ;
100+ requestMessage . Headers . Add ( SettingsService . ServiceAddressReplace , Options . Shop ) ;
75101
76102 var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
77103 if ( response . IsSuccessStatusCode )
@@ -102,7 +128,11 @@ public async Task<ResponseDto<ProductsListDto>> ValidateAccessToken()
102128
103129 if ( string . IsNullOrEmpty ( accessToken ) )
104130 {
105- UmbCoreLogger . Info < ShopifyService > ( message : "Cannot access Shopify - Access Token is missing." ) ;
131+ #if NETCOREAPP
132+ UmbCoreLogger . LogInformation ( LoggingResources . AccessTokenMissing ) ;
133+ #else
134+ UmbCoreLogger . Info < ShopifyService > ( message : LoggingResources . AccessTokenMissing ) ;
135+ #endif
106136
107137 return new ResponseDto < ProductsListDto > ( ) ;
108138 }
@@ -111,8 +141,8 @@ public async Task<ResponseDto<ProductsListDto>> ValidateAccessToken()
111141 {
112142 Method = HttpMethod . Get ,
113143 RequestUri = new Uri ( string . Format ( SettingsService . ProductsApiEndpoint ,
114- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] ,
115- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyApiVersion ] ) )
144+ Options . Shop ,
145+ Options . ApiVersion ) )
116146 } ;
117147 requestMessage . Headers . Add ( "X-Shopify-Access-Token" , accessToken ) ;
118148
@@ -137,12 +167,16 @@ public async Task<ResponseDto<ProductsListDto>> GetResults()
137167 TokenService . TryGetParameters ( SettingsService . AccessTokenDbKey , out accessToken ) ;
138168 else
139169 {
140- accessToken = AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyAccessToken ] ;
170+ accessToken = Options . AccessToken ;
141171 }
142172
143173 if ( string . IsNullOrEmpty ( accessToken ) )
144174 {
145- UmbCoreLogger . Info < ShopifyService > ( message : "Cannot access Shopify - Access Token is missing." ) ;
175+ #if NETCOREAPP
176+ UmbCoreLogger . LogInformation ( LoggingResources . AccessTokenMissing ) ;
177+ #else
178+ UmbCoreLogger . Info < ShopifyService > ( message : LoggingResources . AccessTokenMissing ) ;
179+ #endif
146180
147181 return new ResponseDto < ProductsListDto > ( ) ;
148182 }
@@ -151,15 +185,19 @@ public async Task<ResponseDto<ProductsListDto>> GetResults()
151185 {
152186 Method = HttpMethod . Get ,
153187 RequestUri = new Uri ( string . Format ( SettingsService . ProductsApiEndpoint ,
154- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] ,
155- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyApiVersion ] ) )
188+ Options . Shop ,
189+ Options . ApiVersion ) )
156190 } ;
157191 requestMessage . Headers . Add ( "X-Shopify-Access-Token" , accessToken ) ;
158192
159193 var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
160194 if ( response . StatusCode == HttpStatusCode . Unauthorized )
161195 {
162- UmbCoreLogger . Error < ShopifyService > ( $ "Failed to fetch products from Shopify store using access token: { response . ReasonPhrase } ") ;
196+ #if NETCOREAPP
197+ UmbCoreLogger . LogError ( string . Format ( LoggingResources . FetchProductsFailed , response . ReasonPhrase ) ) ;
198+ #else
199+ UmbCoreLogger . Error < ShopifyService > ( string . Format ( LoggingResources . FetchProductsFailed , response . ReasonPhrase ) ) ;
200+ #endif
163201
164202 return new ResponseDto < ProductsListDto > { Message = response . ReasonPhrase } ;
165203 }
0 commit comments