5
5
using System . Threading . Tasks ;
6
6
7
7
using Newtonsoft . Json ;
8
-
8
+ using Umbraco . Cms . Integrations . Commerce . Shopify . Configuration ;
9
9
using Umbraco . Cms . Integrations . Commerce . Shopify . Models . Dtos ;
10
- using Umbraco . Cms . Integrations . Shared . Configuration ;
11
10
using Umbraco . Cms . Integrations . Shared . Models ;
12
11
using Umbraco . Cms . Integrations . Shared . Models . Dtos ;
13
12
using Umbraco . Cms . Integrations . Shared . Resolvers ;
14
13
using 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 ;
15
22
using Umbraco . Core . Logging ;
23
+ #endif
16
24
17
25
namespace Umbraco . Cms . Integrations . Commerce . Shopify . Services
18
26
{
19
27
public class ShopifyService : BaseService , IApiService < ProductsListDto >
20
28
{
21
29
private readonly JsonSerializerSettings _serializerSettings ;
22
30
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 )
24
35
{
25
36
var resolver = new JsonPropertyRenameContractResolver ( ) ;
26
37
resolver . RenameProperty ( typeof ( ResponseDto < ProductsListDto > ) , "Result" , "products" ) ;
@@ -29,16 +40,32 @@ public ShopifyService(ILogger logger, IAppSettings appSettings, ITokenService to
29
40
{
30
41
ContractResolver = resolver
31
42
} ;
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 ) ;
32
58
}
59
+ #endif
33
60
34
61
public EditorSettings GetApiConfiguration ( )
35
62
{
36
- if ( string . IsNullOrEmpty ( AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] )
37
- || string . IsNullOrEmpty ( AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyApiVersion ] ) )
63
+ if ( string . IsNullOrEmpty ( Options . Shop )
64
+ || string . IsNullOrEmpty ( Options . ApiVersion ) )
38
65
return new EditorSettings ( ) ;
39
66
40
67
return
41
- ! string . IsNullOrEmpty ( AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyAccessToken ] )
68
+ ! string . IsNullOrEmpty ( Options . AccessToken )
42
69
? new EditorSettings { IsValid = true , Type = ConfigurationType . Api }
43
70
: ! string . IsNullOrEmpty ( SettingsService . OAuthClientId )
44
71
&& ! string . IsNullOrEmpty ( OAuthProxyBaseUrl )
@@ -51,7 +78,7 @@ public string GetAuthorizationUrl()
51
78
{
52
79
return
53
80
string . Format ( SettingsService . AuthorizationUrl ,
54
- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] , SettingsService . OAuthClientId , SettingsService . ShopifyOAuthProxyUrl ) ;
81
+ Options . Shop , SettingsService . OAuthClientId , SettingsService . ShopifyOAuthProxyUrl ) ;
55
82
}
56
83
57
84
public async Task < string > GetAccessToken ( OAuthRequestDto request )
@@ -70,8 +97,7 @@ public async Task<string> GetAccessToken(OAuthRequestDto request)
70
97
Content = new FormUrlEncodedContent ( data )
71
98
} ;
72
99
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 ) ;
75
101
76
102
var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
77
103
if ( response . IsSuccessStatusCode )
@@ -102,7 +128,11 @@ public async Task<ResponseDto<ProductsListDto>> ValidateAccessToken()
102
128
103
129
if ( string . IsNullOrEmpty ( accessToken ) )
104
130
{
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
106
136
107
137
return new ResponseDto < ProductsListDto > ( ) ;
108
138
}
@@ -111,8 +141,8 @@ public async Task<ResponseDto<ProductsListDto>> ValidateAccessToken()
111
141
{
112
142
Method = HttpMethod . Get ,
113
143
RequestUri = new Uri ( string . Format ( SettingsService . ProductsApiEndpoint ,
114
- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] ,
115
- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyApiVersion ] ) )
144
+ Options . Shop ,
145
+ Options . ApiVersion ) )
116
146
} ;
117
147
requestMessage . Headers . Add ( "X-Shopify-Access-Token" , accessToken ) ;
118
148
@@ -137,12 +167,16 @@ public async Task<ResponseDto<ProductsListDto>> GetResults()
137
167
TokenService . TryGetParameters ( SettingsService . AccessTokenDbKey , out accessToken ) ;
138
168
else
139
169
{
140
- accessToken = AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyAccessToken ] ;
170
+ accessToken = Options . AccessToken ;
141
171
}
142
172
143
173
if ( string . IsNullOrEmpty ( accessToken ) )
144
174
{
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
146
180
147
181
return new ResponseDto < ProductsListDto > ( ) ;
148
182
}
@@ -151,15 +185,19 @@ public async Task<ResponseDto<ProductsListDto>> GetResults()
151
185
{
152
186
Method = HttpMethod . Get ,
153
187
RequestUri = new Uri ( string . Format ( SettingsService . ProductsApiEndpoint ,
154
- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyShop ] ,
155
- AppSettings [ Constants . UmbracoCmsIntegrationsCommerceShopifyApiVersion ] ) )
188
+ Options . Shop ,
189
+ Options . ApiVersion ) )
156
190
} ;
157
191
requestMessage . Headers . Add ( "X-Shopify-Access-Token" , accessToken ) ;
158
192
159
193
var response = await ClientFactory ( ) . SendAsync ( requestMessage ) ;
160
194
if ( response . StatusCode == HttpStatusCode . Unauthorized )
161
195
{
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
163
201
164
202
return new ResponseDto < ProductsListDto > { Message = response . ReasonPhrase } ;
165
203
}
0 commit comments