-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathThemeServiceRemote.m
463 lines (384 loc) · 21 KB
/
ThemeServiceRemote.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#import "ThemeServiceRemote.h"
#import "RemoteTheme.h"
#import "WPKit-Swift.h"
@import NSObject_SafeExpectations;
// Service dictionary keys
static NSString* const ThemeServiceRemoteThemesKey = @"themes";
static NSString* const ThemeServiceRemoteThemeCountKey = @"found";
static NSString* const ThemeRequestTierKey = @"tier";
static NSString* const ThemeRequestTierAllValue = @"all";
static NSString* const ThemeRequestTierFreeValue = @"free";
static NSString* const ThemeRequestNumberKey = @"number";
static NSInteger const ThemeRequestNumberValue = 50;
static NSString* const ThemeRequestPageKey = @"page";
@implementation ThemeServiceRemote
#pragma mark - Getting themes
- (NSProgress *)getActiveThemeForBlogId:(NSNumber *)blogId
success:(ThemeServiceRemoteThemeRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert([blogId isKindOfClass:[NSNumber class]]);
NSString *path = [NSString stringWithFormat:@"sites/%@/themes/mine", blogId];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
NSProgress *progress = [self.wordPressComRestApi GET:requestUrl
parameters:nil
success:^(NSDictionary *themeDictionary, NSHTTPURLResponse *httpResponse) {
if (success) {
RemoteTheme *theme = [self themeFromDictionary:themeDictionary];
theme.active = YES;
success(theme);
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
return progress;
}
- (NSProgress *)getPurchasedThemesForBlogId:(NSNumber *)blogId
success:(ThemeServiceRemoteThemeIdentifiersRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert([blogId isKindOfClass:[NSNumber class]]);
NSString *path = [NSString stringWithFormat:@"sites/%@/themes/purchased", blogId];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
NSProgress *progress = [self.wordPressComRestApi GET:requestUrl
parameters:nil
success:^(NSDictionary *response, NSHTTPURLResponse *httpResponse) {
if (success) {
NSArray *themes = [self themeIdentifiersFromPurchasedThemesRequestResponse:response];
success(themes);
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
return progress;
}
- (NSProgress *)getThemeId:(NSString*)themeId
success:(ThemeServiceRemoteThemeRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert([themeId isKindOfClass:[NSString class]]);
NSString *path = [NSString stringWithFormat:@"themes/%@", themeId];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
NSProgress *progress = [self.wordPressComRestApi GET:requestUrl
parameters:nil
success:^(NSDictionary *themeDictionary, NSHTTPURLResponse *httpResponse) {
if (success) {
RemoteTheme *theme = [self themeFromDictionary:themeDictionary];
success(theme);
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
return progress;
}
- (NSProgress *)getWPThemesPage:(NSInteger)page
freeOnly:(BOOL)freeOnly
success:(ThemeServiceRemoteThemesRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert(page > 0);
NSString *requestUrl = [self pathForEndpoint:@"themes"
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_2];
NSDictionary *parameters = @{ThemeRequestTierKey: freeOnly ? ThemeRequestTierFreeValue : ThemeRequestTierAllValue,
ThemeRequestNumberKey: @(ThemeRequestNumberValue),
ThemeRequestPageKey: @(page),
};
return [self getThemesWithRequestUrl:requestUrl
page:page
parameters:parameters
success:success
failure:failure];
}
- (NSProgress *)getThemesPage:(NSInteger)page
path:(NSString *)path
success:(ThemeServiceRemoteThemesRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert(page > 0);
NSParameterAssert([path isKindOfClass:[NSString class]]);
NSString *requestUrl = [self pathForEndpoint:path
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_2];
NSDictionary *parameters = @{ThemeRequestTierKey: ThemeRequestTierAllValue,
ThemeRequestNumberKey: @(ThemeRequestNumberValue),
ThemeRequestPageKey: @(page),
};
return [self getThemesWithRequestUrl:requestUrl
page:page
parameters:parameters
success:success
failure:failure];
}
- (NSProgress *)getThemesForBlogId:(NSNumber *)blogId
page:(NSInteger)page
success:(ThemeServiceRemoteThemesRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert([blogId isKindOfClass:[NSNumber class]]);
NSParameterAssert(page > 0);
NSProgress *progress = [self getThemesForBlogId:blogId
page:page
apiVersion:ServiceRemoteWordPressComRESTApiVersion_1_2
params:@{ThemeRequestTierKey: ThemeRequestTierAllValue}
success:success
failure:failure];
return progress;
}
- (NSProgress *)getCustomThemesForBlogId:(NSNumber *)blogId
success:(ThemeServiceRemoteThemesRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert([blogId isKindOfClass:[NSNumber class]]);
NSProgress *progress = [self getThemesForBlogId:blogId
page:1
apiVersion:ServiceRemoteWordPressComRESTApiVersion_1_0
params:@{}
success:success
failure:failure];
return progress;
}
- (NSProgress *)getThemesForBlogId:(NSNumber *)blogId
page:(NSInteger)page
apiVersion:(ServiceRemoteWordPressComRESTApiVersion) apiVersion
params:(NSDictionary *)params
success:(ThemeServiceRemoteThemesRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert(page > 0);
NSString *path = [NSString stringWithFormat:@"sites/%@/themes", blogId];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:apiVersion];
NSMutableDictionary *parameters = [params mutableCopy];
parameters[ThemeRequestNumberKey] = @(ThemeRequestNumberValue);
parameters[ThemeRequestPageKey] = @(page);
return [self getThemesWithRequestUrl:requestUrl
page:page
parameters:parameters
success:success
failure:failure];
}
- (void)getStartingThemesForCategory:(NSString *)category
page:(NSInteger)page
success:(ThemeServiceRemoteThemesRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert(page > 0);
NSParameterAssert([category isKindOfClass:[NSString class]]);
NSString *path = [NSString stringWithFormat:@"themes/?filter=starting-%@", category];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_2];
NSDictionary *parameters = @{
ThemeRequestNumberKey: @(ThemeRequestNumberValue),
ThemeRequestPageKey: @(page),
};
[self getThemesWithRequestUrl:requestUrl
page:page
parameters:parameters
success:success
failure:failure];
}
- (NSProgress *)getThemesWithRequestUrl:(NSString *)requestUrl
page:(NSInteger)page
parameters:(NSDictionary *)parameters
success:(ThemeServiceRemoteThemesRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
return [self.wordPressComRestApi GET:requestUrl
parameters:parameters
success:^(NSDictionary *response, NSHTTPURLResponse *httpResponse) {
if (success) {
NSArray<RemoteTheme *> *themes = [self themesFromMultipleThemesRequestResponse:response];
NSInteger themesLoaded = (page - 1) * ThemeRequestNumberValue;
for (RemoteTheme *theme in themes){
theme.order = ++themesLoaded;
}
// v1 of the API does not return the found field
NSInteger themesCount = MAX(themes.count, [[response numberForKey:ThemeServiceRemoteThemeCountKey] integerValue]);
BOOL hasMore = themesLoaded < themesCount;
success(themes, hasMore, themesCount);
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
}
#pragma mark - Activating themes
- (NSProgress *)activateThemeId:(NSString *)themeId
forBlogId:(NSNumber *)blogId
success:(ThemeServiceRemoteThemeRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert([themeId isKindOfClass:[NSString class]]);
NSParameterAssert([blogId isKindOfClass:[NSNumber class]]);
NSString* const path = [NSString stringWithFormat:@"sites/%@/themes/mine", blogId];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
NSDictionary* parameters = @{@"theme": themeId};
NSProgress *progress = [self.wordPressComRestApi POST:requestUrl
parameters:parameters
success:^(NSDictionary *themeDictionary, NSHTTPURLResponse *httpResponse) {
if (success) {
RemoteTheme *theme = [self themeFromDictionary:themeDictionary];
theme.active = YES;
success(theme);
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
return progress;
}
- (NSProgress *)installThemeId:(NSString*)themeId
forBlogId:(NSNumber *)blogId
success:(ThemeServiceRemoteThemeRequestSuccessBlock)success
failure:(ThemeServiceRemoteFailureBlock)failure
{
NSParameterAssert([themeId isKindOfClass:[NSString class]]);
NSParameterAssert([blogId isKindOfClass:[NSNumber class]]);
NSString* const path = [NSString stringWithFormat:@"sites/%@/themes/%@/install", blogId, themeId];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
NSProgress *progress = [self.wordPressComRestApi POST:requestUrl
parameters:nil
success:^(NSDictionary *themeDictionary, NSHTTPURLResponse *httpResponse) {
if (success) {
RemoteTheme *theme = [self themeFromDictionary:themeDictionary];
success(theme);
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
return progress;
}
#pragma mark - Parsing responses
/**
* @brief Parses a purchased-themes-request response.
*
* @param response The response object. Cannot be nil.
*/
- (NSArray *)themeIdentifiersFromPurchasedThemesRequestResponse:(id)response
{
NSParameterAssert(response != nil);
NSArray *themeIdentifiers = [response arrayForKey:ThemeServiceRemoteThemesKey];
return themeIdentifiers;
}
/**
* @brief Parses a generic multi-themes-request response.
*
* @param response The response object. Cannot be nil.
*/
- (NSArray<RemoteTheme *> *)themesFromMultipleThemesRequestResponse:(id)response
{
NSParameterAssert(response != nil);
NSArray *themeDictionaries = [response arrayForKey:ThemeServiceRemoteThemesKey];
NSArray<RemoteTheme *> *themes = [self themesFromDictionaries:themeDictionaries];
return themes;
}
#pragma mark - Parsing the dictionary replies
/**
* @brief Creates a remote theme object from the specified dictionary.
*
* @param dictionary The dictionary containing the theme information. Cannot be nil.
*
* @returns A remote theme object.
*/
- (RemoteTheme *)themeFromDictionary:(NSDictionary *)dictionary
{
NSParameterAssert([dictionary isKindOfClass:[NSDictionary class]]);
static NSString* const ThemeActiveKey = @"active";
static NSString* const ThemeAuthorKey = @"author";
static NSString* const ThemeAuthorURLKey = @"author_uri";
static NSString* const ThemeCostPath = @"cost.number";
static NSString* const ThemeDemoURLKey = @"demo_uri";
static NSString* const ThemeURL = @"theme_uri";
static NSString* const ThemeDescriptionKey = @"description";
static NSString* const ThemeDownloadURLKey = @"download_uri";
static NSString* const ThemeIdKey = @"id";
static NSString* const ThemeNameKey = @"name";
static NSString* const ThemePreviewURLKey = @"preview_url";
static NSString* const ThemePriceKey = @"price";
static NSString* const ThemePurchasedKey = @"purchased";
static NSString* const ThemePopularityRankKey = @"rank_popularity";
static NSString* const ThemeScreenshotKey = @"screenshot";
static NSString* const ThemeStylesheetKey = @"stylesheet";
static NSString* const ThemeTrendingRankKey = @"rank_trending";
static NSString* const ThemeVersionKey = @"version";
static NSString* const ThemeDomainPublic = @"pub";
static NSString* const ThemeDomainPremium = @"premium";
RemoteTheme *theme = [RemoteTheme new];
[self loadLaunchDateForTheme:theme fromDictionary:dictionary];
theme.active = [[dictionary numberForKey:ThemeActiveKey] boolValue];
theme.author = [dictionary stringForKey:ThemeAuthorKey];
theme.authorUrl = [dictionary stringForKey:ThemeAuthorURLKey];
theme.demoUrl = [dictionary stringForKey:ThemeDemoURLKey];
theme.themeUrl = [dictionary stringForKey:ThemeURL];
theme.desc = [dictionary stringForKey:ThemeDescriptionKey];
theme.downloadUrl = [dictionary stringForKey:ThemeDownloadURLKey];
theme.name = [dictionary stringForKey:ThemeNameKey];
theme.popularityRank = [dictionary numberForKey:ThemePopularityRankKey];
theme.previewUrl = [dictionary stringForKey:ThemePreviewURLKey];
theme.price = [dictionary stringForKey:ThemePriceKey];
theme.purchased = [dictionary numberForKey:ThemePurchasedKey];
theme.screenshotUrl = [dictionary stringForKey:ThemeScreenshotKey];
theme.stylesheet = [dictionary stringForKey:ThemeStylesheetKey];
theme.themeId = [dictionary stringForKey:ThemeIdKey];
theme.trendingRank = [dictionary numberForKey:ThemeTrendingRankKey];
theme.version = [dictionary stringForKey:ThemeVersionKey];
if (!theme.stylesheet) {
NSString *domain = [dictionary numberForKeyPath:ThemeCostPath].intValue > 0 ? ThemeDomainPremium : ThemeDomainPublic;
theme.stylesheet = [NSString stringWithFormat:@"%@/%@", domain, theme.themeId];
}
return theme;
}
/**
* @brief Creates remote theme objects from the specified array of dictionaries.
*
* @param dictionaries The array of dictionaries containing the themes information. Cannot
* be nil.
*
* @returns An array of remote theme objects.
*/
- (NSArray<RemoteTheme *> *)themesFromDictionaries:(NSArray *)dictionaries
{
NSParameterAssert([dictionaries isKindOfClass:[NSArray class]]);
NSMutableArray *themes = [[NSMutableArray alloc] initWithCapacity:dictionaries.count];
for (NSDictionary *dictionary in dictionaries) {
NSAssert([dictionary isKindOfClass:[NSDictionary class]],
@"Expected a dictionary.");
RemoteTheme *theme = [self themeFromDictionary:dictionary];
[themes addObject:theme];
}
return [NSArray arrayWithArray:themes];
}
#pragma mark - Field parsing
/**
* @brief Loads a theme's launch date from a dictionary into the specified remote theme
* object.
*
* @param theme The theme to load the info into. Cannot be nil.
* @param dictionary The dictionary to load the info from. Cannot be nil.
*/
- (void)loadLaunchDateForTheme:(RemoteTheme *)theme
fromDictionary:(NSDictionary *)dictionary
{
NSParameterAssert([theme isKindOfClass:[RemoteTheme class]]);
NSParameterAssert([dictionary isKindOfClass:[NSDictionary class]]);
static NSString* const ThemeLaunchDateKey = @"date_launched";
NSString *launchDateString = [dictionary stringForKey:ThemeLaunchDateKey];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-mm-dd"];
theme.launchDate = [formatter dateFromString:launchDateString];
}
@end