Skip to content

Commit b2acb10

Browse files
committed
Shopify Property Editor multi-targeted & V9 test site
1 parent 49e6ba2 commit b2acb10

33 files changed

+980
-263
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Specialized;
2+
3+
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Configuration
4+
{
5+
public class ShopifySettings
6+
{
7+
public ShopifySettings()
8+
{
9+
10+
}
11+
12+
public ShopifySettings(NameValueCollection appSettings)
13+
{
14+
ApiVersion = appSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyApiVersion];
15+
Shop = appSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyShop];
16+
AccessToken = appSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyAccessToken];
17+
}
18+
19+
public string ApiVersion { get; set; }
20+
21+
public string Shop { get; set; }
22+
23+
public string AccessToken { get; set; }
24+
}
25+
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public static class Constants
1414

1515
public const string AppPluginFolderPath = "~/App_Plugins/UmbracoCms.Integrations/Commerce/Shopify";
1616

17+
public static class Configuration
18+
{
19+
public const string Settings = "Umbraco:Cms:Integrations:Commerce:Shopify:Settings";
20+
}
21+
1722
public static class PropertyEditors
1823
{
1924
public const string ProductPickerAlias = "Umbraco.Cms.Integrations.Commerce.Shopify.ProductPicker";

src/Umbraco.Cms.Integrations.Commerce.Shopify/Controllers/ProductsController.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
using System.Linq;
2-
using System.Threading.Tasks;
3-
using System.Web.Http;
4-
using Newtonsoft.Json;
1+
using System.Threading.Tasks;
2+
53
using Umbraco.Cms.Integrations.Commerce.Shopify.Models.Dtos;
64
using Umbraco.Cms.Integrations.Shared.Models;
7-
using Umbraco.Web.Mvc;
85
using Umbraco.Cms.Integrations.Shared.Models.Dtos;
96
using Umbraco.Cms.Integrations.Shared.Services;
7+
8+
9+
#if NETCOREAPP
10+
using Microsoft.AspNetCore.Mvc;
11+
12+
using Umbraco.Cms.Web.BackOffice.Controllers;
13+
using Umbraco.Cms.Web.Common.Attributes;
14+
#else
15+
using System.Web.Http;
16+
using Umbraco.Web.Mvc;
1017
using Umbraco.Web.WebApi;
18+
#endif
1119

1220
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Controllers
1321
{
@@ -16,13 +24,9 @@ public class ProductsController : UmbracoAuthorizedApiController
1624
{
1725
private readonly IApiService<ProductsListDto> _apiService;
1826

19-
private readonly ICacheHelper _cacheHelper;
20-
21-
public ProductsController(IApiService<ProductsListDto> apiService, ICacheHelper cacheHelper)
27+
public ProductsController(IApiService<ProductsListDto> apiService)
2228
{
2329
_apiService = apiService;
24-
25-
_cacheHelper = cacheHelper;
2630
}
2731

2832
[HttpGet]

src/Umbraco.Cms.Integrations.Commerce.Shopify/Editors/ShopifyProductPickerConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using Umbraco.Cms.Integrations.Commerce.Shopify.Models;
22
using Umbraco.Cms.Integrations.Shared.Models;
3+
4+
#if NETCOREAPP
5+
using Umbraco.Cms.Core.PropertyEditors;
6+
#else
37
using Umbraco.Core.PropertyEditors;
8+
#endif
49

510
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Editors
611
{
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
using Umbraco.Core.PropertyEditors;
1+
#if NETCOREAPP
2+
using Umbraco.Cms.Core.IO;
3+
using Umbraco.Cms.Core.PropertyEditors;
4+
#else
5+
using Umbraco.Core.PropertyEditors;
6+
#endif
27

38
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Editors
49
{
510
public class ShopifyProductPickerConfigurationEditor: ConfigurationEditor<ShopifyProductPickerConfiguration>
611
{
12+
#if NETCOREAPP
13+
public ShopifyProductPickerConfigurationEditor(IIOHelper ioHelper) : base(ioHelper)
14+
{
15+
}
16+
#endif
717
}
818
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Editors/ShopifyProductPickerPropertyEditor.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
using Umbraco.Core.Logging;
1+
#if NETCOREAPP
2+
using Umbraco.Cms.Core.IO;
3+
using Umbraco.Cms.Core.PropertyEditors;
4+
#else
5+
using Umbraco.Core.Logging;
26
using Umbraco.Core.PropertyEditors;
7+
#endif
38

49
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Editors
510
{
@@ -11,6 +16,19 @@ namespace Umbraco.Cms.Integrations.Commerce.Shopify.Editors
1116
Icon = "icon-shopping-basket-alt")]
1217
public class ShopifyProductPickerPropertyEditor: DataEditor
1318
{
19+
#if NETCOREAPP
20+
private IIOHelper _ioHelper;
21+
22+
public ShopifyProductPickerPropertyEditor(IDataValueEditorFactory dataValueEditorFactory, IIOHelper ioHelper) : base(dataValueEditorFactory)
23+
{
24+
_ioHelper = ioHelper;
25+
}
26+
27+
protected override IConfigurationEditor CreateConfigurationEditor()
28+
{
29+
return new ShopifyProductPickerConfigurationEditor(_ioHelper);
30+
}
31+
#else
1432
public ShopifyProductPickerPropertyEditor(ILogger logger) : base(logger)
1533
{
1634
}
@@ -19,5 +37,6 @@ protected override IConfigurationEditor CreateConfigurationEditor()
1937
{
2038
return new ShopifyProductPickerConfigurationEditor();
2139
}
40+
#endif
2241
}
2342
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Editors/ShopifyProductPickerValueConverter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Threading.Tasks;
54

65
using Umbraco.Cms.Integrations.Commerce.Shopify.Models.Dtos;
76
using Umbraco.Cms.Integrations.Commerce.Shopify.Models.ViewModels;
87
using Umbraco.Cms.Integrations.Shared.Services;
8+
9+
#if NETCOREAPP
10+
using Umbraco.Cms.Core.PropertyEditors;
11+
using Umbraco.Cms.Core.Models.PublishedContent;
12+
#else
913
using Umbraco.Core.Models.PublishedContent;
1014
using Umbraco.Core.PropertyEditors;
15+
#endif
1116

1217
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Editors
1318
{
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
using System.Collections.Generic;
2+
3+
#if NETCOREAPP
4+
using Microsoft.AspNetCore.Html;
5+
using Microsoft.AspNetCore.Mvc.Rendering;
6+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
7+
#else
28
using System.Web;
39
using System.Web.Mvc;
410
using System.Web.Mvc.Html;
11+
#endif
512

613
using Umbraco.Cms.Integrations.Commerce.Shopify.Models.ViewModels;
714

815
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Helpers
916
{
1017
public static class ShopifyHtmlExtensions
1118
{
12-
public static IHtmlString RenderProductsList(this HtmlHelper htmlHelper, List<ProductViewModel> vm)
19+
#if NETCOREAPP
20+
public static IHtmlContent RenderShopifyProductsList(this HtmlHelper htmlHelper, List<ProductViewModel> vm)
21+
{
22+
return htmlHelper.Partial($"{Constants.AppPluginFolderPath}/Render/Products.cshtml", vm);
23+
}
24+
#else
25+
public static IHtmlString RenderShopifyProductsList(this HtmlHelper htmlHelper, List<ProductViewModel> vm)
1326
{
1427
return htmlHelper.Partial($"{Constants.AppPluginFolderPath}/Render/Products.cshtml", vm);
1528
}
29+
#endif
1630
}
1731
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
namespace Umbraco.Cms.Integrations.Commerce.Shopify.Resources
3+
{
4+
public class LoggingResources
5+
{
6+
public const string AccessTokenMissing = "Cannot access Shopify - Access Token is missing.";
7+
8+
public const string FetchProductsFailed =
9+
"Failed to fetch products from Shopify store using access token: {0}";
10+
}
11+
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Services/ShopifyService.cs

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,33 @@
55
using System.Threading.Tasks;
66

77
using Newtonsoft.Json;
8-
8+
using Umbraco.Cms.Integrations.Commerce.Shopify.Configuration;
99
using Umbraco.Cms.Integrations.Commerce.Shopify.Models.Dtos;
10-
using Umbraco.Cms.Integrations.Shared.Configuration;
1110
using Umbraco.Cms.Integrations.Shared.Models;
1211
using Umbraco.Cms.Integrations.Shared.Models.Dtos;
1312
using Umbraco.Cms.Integrations.Shared.Resolvers;
1413
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;
1522
using Umbraco.Core.Logging;
23+
#endif
1624

1725
namespace 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

Comments
 (0)