1
1
using Newtonsoft . Json ;
2
+
2
3
using System ;
3
4
using System . Collections . Generic ;
4
5
using System . IO ;
7
8
using System . Net . Http ;
8
9
using System . Threading ;
9
10
using System . Threading . Tasks ;
10
- using System . Web . Http ;
11
+
11
12
using Umbraco . Cms . Integrations . SEO . Semrush . Configuration ;
12
13
using Umbraco . Cms . Integrations . SEO . Semrush . Models . Dtos ;
13
14
using Umbraco . Cms . Integrations . SEO . Semrush . Services ;
15
+
16
+ #if NETCOREAPP
17
+ using Microsoft . AspNetCore . Hosting ;
18
+ using Microsoft . AspNetCore . Mvc ;
19
+
20
+ using Umbraco . Cms . Web . Common . Attributes ;
21
+ using Umbraco . Cms . Web . BackOffice . Controllers ;
22
+ #else
23
+ using System . Web ;
24
+ using System . Web . Http ;
25
+
26
+ using Umbraco . Web . WebApi ;
14
27
using Umbraco . Web . Mvc ;
28
+ #endif
15
29
16
30
namespace Umbraco . Cms . Integrations . SEO . Semrush . Controllers
17
31
{
18
32
[ PluginController ( "UmbracoCmsIntegrationsSemrush" ) ]
19
- public class SemrushController : BaseController
33
+ public class SemrushController : UmbracoAuthorizedApiController
20
34
{
21
35
// Using a static HttpClient (see: https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/).
22
36
private readonly static HttpClient s_client = new HttpClient ( ) ;
@@ -26,9 +40,28 @@ public class SemrushController : BaseController
26
40
27
41
private static readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim ( ) ;
28
42
43
+ private readonly ISemrushTokenService _semrushTokenService ;
44
+
45
+ private readonly ICacheHelper _cacheHelper ;
46
+
47
+ private readonly TokenBuilder _tokenBuilder ;
48
+
49
+ #if NETCOREAPP
50
+ private readonly IWebHostEnvironment _webHostEnvironment ;
51
+
52
+ public SemrushController ( IWebHostEnvironment webHostEnvironment , ISemrushTokenService semrushTokenService , ICacheHelper cacheHelper , TokenBuilder tokenBuilder )
53
+ {
54
+ _webHostEnvironment = webHostEnvironment ;
55
+ #else
29
56
public SemrushController ( ISemrushTokenService semrushTokenService , ICacheHelper cacheHelper , TokenBuilder tokenBuilder )
30
- : base ( semrushTokenService , cacheHelper , tokenBuilder )
31
57
{
58
+
59
+ #endif
60
+ _semrushTokenService = semrushTokenService ;
61
+
62
+ _cacheHelper = cacheHelper ;
63
+
64
+ _tokenBuilder = tokenBuilder ;
32
65
}
33
66
34
67
[ HttpGet ]
@@ -41,21 +74,21 @@ public SemrushController(ISemrushTokenService semrushTokenService, ICacheHelper
41
74
[ HttpGet ]
42
75
public TokenDto GetTokenDetails ( )
43
76
{
44
- return SemrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto tokenDto ) ? tokenDto : new TokenDto ( ) ;
77
+ return _semrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto tokenDto ) ? tokenDto : new TokenDto ( ) ;
45
78
}
46
79
47
80
[ HttpPost ]
48
81
public void RevokeToken ( )
49
82
{
50
- SemrushTokenService . RemoveParameters ( SemrushSettings . TokenDbKey ) ;
83
+ _semrushTokenService . RemoveParameters ( SemrushSettings . TokenDbKey ) ;
51
84
52
- CacheHelper . ClearCachedItems ( ) ;
85
+ _cacheHelper . ClearCachedItems ( ) ;
53
86
}
54
87
55
88
[ HttpPost ]
56
89
public async Task < string > GetAccessToken ( [ FromBody ] AuthorizationRequestDto request )
57
90
{
58
- var requestData = TokenBuilder . ForAccessToken ( request . Code ) . Build ( ) ;
91
+ var requestData = _tokenBuilder . ForAccessToken ( request . Code ) . Build ( ) ;
59
92
60
93
var requestMessage = new HttpRequestMessage
61
94
{
@@ -70,7 +103,7 @@ public async Task<string> GetAccessToken([FromBody] AuthorizationRequestDto requ
70
103
{
71
104
var result = await response . Content . ReadAsStringAsync ( ) ;
72
105
73
- SemrushTokenService . SaveParameters ( SemrushSettings . TokenDbKey , result ) ;
106
+ _semrushTokenService . SaveParameters ( SemrushSettings . TokenDbKey , result ) ;
74
107
75
108
return result ;
76
109
}
@@ -81,7 +114,7 @@ public async Task<string> GetAccessToken([FromBody] AuthorizationRequestDto requ
81
114
[ HttpPost ]
82
115
public async Task < AuthorizationResponseDto > ValidateToken ( )
83
116
{
84
- SemrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto token ) ;
117
+ _semrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto token ) ;
85
118
86
119
if ( ! token . IsAccessTokenAvailable ) return new AuthorizationResponseDto { IsExpired = true } ;
87
120
@@ -94,16 +127,16 @@ public async Task<AuthorizationResponseDto> ValidateToken()
94
127
IsFreeAccount = response . Headers . TryGetValues ( SemrushSettings . AllowLimitOffsetHeaderName ,
95
128
out IEnumerable < string > values )
96
129
? values . First ( ) . Equals ( "0" )
97
- : ( bool ? ) null
130
+ : ( bool ? ) null
98
131
} ;
99
132
}
100
133
101
134
[ HttpPost ]
102
135
public async Task < string > RefreshAccessToken ( )
103
136
{
104
- SemrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto token ) ;
137
+ _semrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto token ) ;
105
138
106
- var requestData = TokenBuilder . ForRefreshToken ( token . RefreshToken ) . Build ( ) ;
139
+ var requestData = _tokenBuilder . ForRefreshToken ( token . RefreshToken ) . Build ( ) ;
107
140
108
141
var requestMessage = new HttpRequestMessage
109
142
{
@@ -118,7 +151,7 @@ public async Task<string> RefreshAccessToken()
118
151
{
119
152
var result = await response . Content . ReadAsStringAsync ( ) ;
120
153
121
- SemrushTokenService . SaveParameters ( SemrushSettings . TokenDbKey , result ) ;
154
+ _semrushTokenService . SaveParameters ( SemrushSettings . TokenDbKey , result ) ;
122
155
123
156
return result ;
124
157
}
@@ -131,7 +164,7 @@ public async Task<RelatedPhrasesDto> GetRelatedPhrases(string phrase, int pageNu
131
164
{
132
165
string cacheKey = $ "{ dataSource } -{ method } -{ phrase } ";
133
166
134
- if ( CacheHelper . TryGetCachedItem < RelatedPhrasesDto > ( cacheKey , out var relatedPhrasesDto ) && relatedPhrasesDto . Data != null )
167
+ if ( _cacheHelper . TryGetCachedItem < RelatedPhrasesDto > ( cacheKey , out var relatedPhrasesDto ) && relatedPhrasesDto . Data != null )
135
168
{
136
169
relatedPhrasesDto . TotalPages = relatedPhrasesDto . Data . Rows . Count / SemrushSettings . DefaultPageSize ;
137
170
relatedPhrasesDto . Data . Rows = relatedPhrasesDto . Data . Rows
@@ -142,7 +175,7 @@ public async Task<RelatedPhrasesDto> GetRelatedPhrases(string phrase, int pageNu
142
175
return relatedPhrasesDto ;
143
176
}
144
177
145
- SemrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto token ) ;
178
+ _semrushTokenService . TryGetParameters ( SemrushSettings . TokenDbKey , out TokenDto token ) ;
146
179
147
180
var response = await ClientFactory ( )
148
181
. GetAsync ( string . Format ( SemrushSettings . SemrushKeywordsEndpoint , method , token . AccessToken , phrase , dataSource ) ) ;
@@ -155,7 +188,7 @@ public async Task<RelatedPhrasesDto> GetRelatedPhrases(string phrase, int pageNu
155
188
156
189
if ( ! relatedPhrasesDeserialized . IsSuccessful ) return relatedPhrasesDeserialized ;
157
190
158
- CacheHelper . AddCachedItem ( cacheKey , responseContent ) ;
191
+ _cacheHelper . AddCachedItem ( cacheKey , responseContent ) ;
159
192
160
193
relatedPhrasesDeserialized . TotalPages = relatedPhrasesDeserialized . Data . Rows . Count / SemrushSettings . DefaultPageSize ;
161
194
relatedPhrasesDeserialized . Data . Rows = relatedPhrasesDeserialized . Data . Rows
@@ -172,19 +205,26 @@ public async Task<RelatedPhrasesDto> GetRelatedPhrases(string phrase, int pageNu
172
205
[ HttpGet ]
173
206
public DataSourceDto GetDataSources ( )
174
207
{
208
+ #if NETCOREAPP
209
+ string semrushDataSourcesPath = $ "{ _webHostEnvironment . ContentRootPath } /App_Plugins/UmbracoCms.Integrations/SEO/Semrush/semrushDataSources.json";
210
+ #else
211
+ string semrushDataSourcesPath = HttpContext . Current . Server . MapPath (
212
+ "/App_Plugins/UmbracoCms.Integrations/SEO/Semrush/semrushDataSources.json" ) ;
213
+ #endif
214
+
175
215
_lock . EnterReadLock ( ) ;
176
216
177
217
try
178
218
{
179
- if ( ! File . Exists ( SemrushDataSourcesPath ) )
219
+ if ( ! System . IO . File . Exists ( semrushDataSourcesPath ) )
180
220
{
181
- var fs = File . Create ( SemrushDataSourcesPath ) ;
221
+ var fs = System . IO . File . Create ( semrushDataSourcesPath ) ;
182
222
fs . Close ( ) ;
183
223
184
224
return new DataSourceDto ( ) ;
185
225
}
186
226
187
- var content = File . ReadAllText ( SemrushDataSourcesPath ) ;
227
+ var content = System . IO . File . ReadAllText ( semrushDataSourcesPath ) ;
188
228
var dataSourceDto = new DataSourceDto
189
229
{
190
230
Items = JsonConvert . DeserializeObject < List < DataSourceItemDto > > ( content ) . Select ( p =>
@@ -212,19 +252,26 @@ public DataSourceDto GetDataSources()
212
252
[ HttpGet ]
213
253
public IEnumerable < ColumnDto > GetColumns ( )
214
254
{
255
+ #if NETCOREAPP
256
+ string semrushColumnsPath = $ "{ _webHostEnvironment . ContentRootPath } /App_Plugins/UmbracoCms.Integrations/SEO/Semrush/semrushColumns.json";
257
+ #else
258
+ string semrushColumnsPath = HttpContext . Current . Server . MapPath (
259
+ "/App_Plugins/UmbracoCms.Integrations/SEO/Semrush/semrushColumns.json" ) ;
260
+ #endif
261
+
215
262
_lock . EnterReadLock ( ) ;
216
263
217
264
try
218
265
{
219
- if ( ! File . Exists ( SemrushColumnsPath ) )
266
+ if ( ! System . IO . File . Exists ( semrushColumnsPath ) )
220
267
{
221
- var fs = File . Create ( SemrushColumnsPath ) ;
268
+ var fs = System . IO . File . Create ( semrushColumnsPath ) ;
222
269
fs . Close ( ) ;
223
270
224
271
return Enumerable . Empty < ColumnDto > ( ) ;
225
272
}
226
273
227
- var content = File . ReadAllText ( SemrushColumnsPath ) ;
274
+ var content = System . IO . File . ReadAllText ( semrushColumnsPath ) ;
228
275
return JsonConvert . DeserializeObject < IEnumerable < ColumnDto > > ( content ) . Select ( p =>
229
276
new ColumnDto
230
277
{
0 commit comments