|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Net; |
4 |
| -using System.Net.Http; |
5 |
| -using System.Threading.Tasks; |
| 1 | +using System.Threading.Tasks; |
6 | 2 | using System.Web.Http;
|
7 |
| -using Newtonsoft.Json; |
8 |
| -using Umbraco.Cms.Integrations.Commerce.Shopify.Models; |
| 3 | + |
9 | 4 | using Umbraco.Cms.Integrations.Commerce.Shopify.Models.Dtos;
|
10 |
| -using Umbraco.Cms.Integrations.Shared.Configuration; |
11 |
| -using Umbraco.Cms.Integrations.Shared.Controllers; |
12 | 5 | using Umbraco.Cms.Integrations.Shared.Models;
|
13 |
| -using Umbraco.Core.Logging; |
14 | 6 | using Umbraco.Web.Mvc;
|
15 | 7 | using Umbraco.Cms.Integrations.Shared.Models.Dtos;
|
16 |
| -using Umbraco.Cms.Integrations.Shared.Resolvers; |
17 | 8 | using Umbraco.Cms.Integrations.Shared.Services;
|
| 9 | +using Umbraco.Web.WebApi; |
18 | 10 |
|
19 | 11 | namespace Umbraco.Cms.Integrations.Commerce.Shopify.Controllers
|
20 | 12 | {
|
21 | 13 | [PluginController("UmbracoCmsIntegrationsCommerceShopify")]
|
22 |
| - public class ProductsController : BaseAuthorizedApiController |
| 14 | + public class ProductsController : UmbracoAuthorizedApiController |
23 | 15 | {
|
24 |
| - private const string ProductsApiEndpoint = "https://{0}.myshopify.com/admin/api/{1}/products.json"; |
25 |
| - |
26 |
| - private const string OAuthClientId = "23c1bc3c70de807d84b79a29b12b49f5"; |
27 |
| - |
28 |
| - private const string ShopifyServiceName = "Shopify"; |
29 |
| - private const string ShopifyServiceAddressReplace = "service_address_shop-replace"; |
30 |
| - |
31 |
| - private string ShopifyOAuthProxyUrl = $"{OAuthProxyBaseUrl}oauth/shopify"; |
32 |
| - private const string ShopifyAuthorizationUrl = |
33 |
| - "https://{0}.myshopify.com/admin/oauth/authorize?client_id={1}&redirect_uri={2}&scope=read_products&grant_options[]=value"; |
| 16 | + private readonly IApiService<ProductsListDto> _apiService; |
34 | 17 |
|
35 |
| - private const string AccessTokenDbKey = "Umbraco.Cms.Integrations.Shopify.AccessTokenDbKey"; |
36 |
| - |
37 |
| - private readonly JsonSerializerSettings _serializerSettings; |
38 |
| - |
39 |
| - public ProductsController(ILogger logger, IAppSettings appSettings, ITokenService tokenService) : base(logger, appSettings, tokenService) |
| 18 | + public ProductsController(IApiService<ProductsListDto> apiService) |
40 | 19 | {
|
41 |
| - var resolver = new JsonPropertyRenameContractResolver(); |
42 |
| - resolver.RenameProperty(typeof(ResponseDto<ProductsListDto>), "Result", "products"); |
43 |
| - |
44 |
| - _serializerSettings = new JsonSerializerSettings(); |
45 |
| - _serializerSettings.ContractResolver = resolver; |
| 20 | + _apiService = apiService; |
46 | 21 | }
|
47 | 22 |
|
48 | 23 | [HttpGet]
|
49 |
| - public EditorSettings CheckConfiguration() |
50 |
| - { |
51 |
| - return GetConfiguration(); |
52 |
| - } |
| 24 | + public EditorSettings CheckConfiguration() => _apiService.GetApiConfiguration(); |
53 | 25 |
|
54 | 26 | [HttpGet]
|
55 |
| - public string GetAuthorizationUrl() |
56 |
| - { |
57 |
| - return |
58 |
| - string.Format(ShopifyAuthorizationUrl, |
59 |
| - AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyShop], OAuthClientId, ShopifyOAuthProxyUrl); |
60 |
| - } |
| 27 | + public string GetAuthorizationUrl() => _apiService.GetAuthorizationUrl(); |
61 | 28 |
|
62 | 29 | [HttpPost]
|
63 |
| - public async Task<string> GetAccessToken([FromBody] OAuthRequestDto authRequestDto) |
64 |
| - { |
65 |
| - var data = new Dictionary<string, string> |
66 |
| - { |
67 |
| - { "client_id", OAuthClientId }, |
68 |
| - { "redirect_uri", string.Format(ShopifyOAuthProxyUrl, OAuthProxyBaseUrl) }, |
69 |
| - { "code", authRequestDto.Code } |
70 |
| - }; |
71 |
| - |
72 |
| - var requestMessage = new HttpRequestMessage |
73 |
| - { |
74 |
| - Method = HttpMethod.Post, |
75 |
| - RequestUri = new Uri(string.Format(OAuthProxyEndpoint, OAuthProxyBaseUrl)), |
76 |
| - Content = new FormUrlEncodedContent(data) |
77 |
| - }; |
78 |
| - requestMessage.Headers.Add("service_name", ShopifyServiceName); |
79 |
| - requestMessage.Headers.Add(ShopifyServiceAddressReplace, |
80 |
| - AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyShop]); |
81 |
| - |
82 |
| - var response = await ClientFactory().SendAsync(requestMessage); |
83 |
| - if (response.IsSuccessStatusCode) |
84 |
| - { |
85 |
| - var result = await response.Content.ReadAsStringAsync(); |
86 |
| - |
87 |
| - var tokenDto = JsonConvert.DeserializeObject<TokenDto>(result); |
88 |
| - |
89 |
| - TokenService.SaveParameters(AccessTokenDbKey, tokenDto.AccessToken); |
| 30 | + public async Task<string> GetAccessToken([FromBody] OAuthRequestDto authRequestDto) => await _apiService.GetAccessToken(authRequestDto); |
90 | 31 |
|
91 |
| - return result; |
92 |
| - } |
93 | 32 |
|
94 |
| - if (response.StatusCode == HttpStatusCode.BadRequest) |
95 |
| - { |
96 |
| - var errorResult = await response.Content.ReadAsStringAsync(); |
97 |
| - var errorDto = JsonConvert.DeserializeObject<ErrorDto>(errorResult); |
| 33 | + public async Task<ResponseDto<ProductsListDto>> GetList() => await _apiService.GetResults(); |
98 | 34 |
|
99 |
| - return "Error: " + errorDto.Message; |
100 |
| - } |
101 | 35 |
|
102 |
| - return "Error: An unexpected error occurred."; |
103 |
| - } |
104 |
| - |
105 |
| - public async Task<ResponseDto<ProductsListDto>> GetList() |
106 |
| - { |
107 |
| - string accessToken; |
108 |
| - if(GetConfiguration().Type == ConfigurationType.OAuth) |
109 |
| - TokenService.TryGetParameters(AccessTokenDbKey, out accessToken); |
110 |
| - else |
111 |
| - { |
112 |
| - accessToken = AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyAccessToken]; |
113 |
| - } |
114 |
| - |
115 |
| - if (string.IsNullOrEmpty(accessToken)) |
116 |
| - { |
117 |
| - ApiLogger.Info<ProductsController>(message: "Cannot access Shopify - Access Token is missing."); |
118 |
| - |
119 |
| - return new ResponseDto<ProductsListDto>(); |
120 |
| - } |
121 |
| - |
122 |
| - var requestMessage = new HttpRequestMessage |
123 |
| - { |
124 |
| - Method = HttpMethod.Get, |
125 |
| - RequestUri = new Uri(string.Format(ProductsApiEndpoint, |
126 |
| - AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyShop], |
127 |
| - AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyApiVersion])) |
128 |
| - }; |
129 |
| - requestMessage.Headers.Add("X-Shopify-Access-Token", accessToken); |
130 |
| - |
131 |
| - var response = await ClientFactory().SendAsync(requestMessage); |
132 |
| - if (response.StatusCode == HttpStatusCode.Unauthorized) |
133 |
| - { |
134 |
| - ApiLogger.Error<ProductsController>($"Failed to fetch products from Shopify store using access token: {response.ReasonPhrase}"); |
135 |
| - |
136 |
| - return new ResponseDto<ProductsListDto> { Message = response.ReasonPhrase }; |
137 |
| - } |
138 |
| - |
139 |
| - if (response.IsSuccessStatusCode) |
140 |
| - { |
141 |
| - var result = await response.Content.ReadAsStringAsync(); |
142 |
| - return new ResponseDto<ProductsListDto> |
143 |
| - { |
144 |
| - IsValid = true, |
145 |
| - Result = JsonConvert.DeserializeObject<ProductsListDto>(result, _serializerSettings) |
146 |
| - }; |
147 |
| - } |
148 |
| - |
149 |
| - return new ResponseDto<ProductsListDto>(); |
150 |
| - } |
151 |
| - |
152 |
| - private EditorSettings GetConfiguration() |
153 |
| - { |
154 |
| - if (string.IsNullOrEmpty(AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyShop]) |
155 |
| - || string.IsNullOrEmpty(AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyApiVersion])) |
156 |
| - return new EditorSettings(); |
157 |
| - |
158 |
| - return |
159 |
| - !string.IsNullOrEmpty(AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyAccessToken]) |
160 |
| - ? new EditorSettings { IsValid = true, Type = ConfigurationType.Api } |
161 |
| - : !string.IsNullOrEmpty(OAuthClientId) |
162 |
| - && !string.IsNullOrEmpty(OAuthProxyBaseUrl) |
163 |
| - && !string.IsNullOrEmpty(OAuthProxyEndpoint) |
164 |
| - ? new EditorSettings { IsValid = true, Type = ConfigurationType.OAuth } |
165 |
| - : new EditorSettings(); |
166 |
| - } |
167 | 36 | }
|
168 | 37 | }
|
0 commit comments