-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.m
More file actions
293 lines (247 loc) · 12.2 KB
/
Stack.m
File metadata and controls
293 lines (247 loc) · 12.2 KB
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
//
// Stack.m
// Contentstack
//
// Created by Reefaq on 11/07/15.
// Copyright (c) 2015 Contentstack. All rights reserved.
//
#import <Contentstack/Stack.h>
#import "CSIOInternalHeaders.h"
#import "CSIOConstants.h"
#import "CSIOCoreHTTPNetworking.h"
#import "ContentType.h"
#import "Taxonomy.h"
#import "CSIOAPIURLs.h"
#import "NSObject+Extensions.h"
#import "GlobalField.h"
@interface Stack ()
@property (nonatomic, copy) NSString *apiKey;
@property (nonatomic, copy) NSString *accessToken;
@property (nonatomic, copy) Config *config;
@property (nonatomic, copy) NSString *environment;
@end
@implementation Stack
- (NSString *)hostURL {
return self.config.host;
}
- (instancetype)initWithAPIKey:(NSString*)apiKey andaccessToken:(NSString *)accessToken andEnvironment:(NSString*)environment andConfig:(Config *)sConfig {
if (self = [super init]) {
_config = sConfig;
_version = [sConfig.version copy];
_environment = [environment copy];
_apiKey = apiKey;
_accessToken = accessToken;
_network = [[CSIOCoreHTTPNetworking alloc] initWithDelegate:_config.delegate];
_stackHeaders = [NSMutableDictionary dictionary];
ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc]init];
_commonDateFormatter = formatter;
_commonDateFormatter.includeTime = YES;
_requestOperationSet = [NSMutableSet set];
// Add early access headers only if they exist
NSDictionary *earlyAccessHeaders = [_config earlyAccessHeaders];
if (earlyAccessHeaders.count > 0) {
[_stackHeaders addEntriesFromDictionary:earlyAccessHeaders];
}
[self setHeader:_apiKey forKey:kCSIO_SiteApiKey];
[self setHeader:_accessToken forKey:kCSIO_Accesstoken];
}
return self;
}
//MARK: - Get ContentTypes
-(void)getContentTypes:(NSDictionary<NSString *,id> *)params completion:(void (^)(NSArray<NSString *> * _Nullable, NSError * _Nullable))completionBlock {
NSString *path = [CSIOAPIURLs fetchSchemaWithVersion:self.version];
NSURLSessionDataTask *op = [self.network requestForStack:self withURLPath:path requestType:CSIOCoreNetworkingRequestTypeGET params:params additionalHeaders:self.stackHeaders completion:^(ResponseType responseType, id responseJSON, NSError *error) {
if (completionBlock) {
if (error) {
completionBlock(nil, error);
}else {
NSDictionary *responseData = responseJSON;
if ([[responseData allKeys] containsObject:@"content_types"] && [responseData objectForKey:@"content_types"] != nil && [[responseData objectForKey:@"content_types"] isKindOfClass:[NSArray class]]) {
completionBlock([responseData objectForKey:@"content_types"], nil);
}else {
NSError *error = [NSError errorWithDomain:@"Error" code:-4001 userInfo:@{@"error": @"Failed to retreive data."}];
completionBlock(nil, error);
}
}
}
}];
if (op && ![op isKindOfClass:[NSNull class]]) {
[self.requestOperationSet addObject:op];
}
}
//MARK: - ContentType
-(ContentType*)contentTypeWithName:(NSString*)contentTypeName; {
ContentType *contentType = [[ContentType alloc] initWithStack:self withName:contentTypeName];
return contentType;
}
//MARK: - Taxonomy
-(Taxonomy*)taxonomy {
Taxonomy *taxonomy = [[Taxonomy alloc] initWithStack:self];
return taxonomy;
}
-(GlobalField*)globalField {
GlobalField *globalField = [[GlobalField alloc] initWithStack:self];
return globalField;
}
-(GlobalField*)globalFieldWithName:(NSString*)globalFieldName {
GlobalField *globalField = [[GlobalField alloc] initWithStack:self withName:globalFieldName];
return globalField;
}
//MARK: - Asset
-(Asset *)asset {
Asset *asset = [[Asset alloc] initWithStack:self];
return asset;
}
-(Asset*)assetWithUID:(NSString*)assetUid {
Asset *asset = [[Asset alloc] initWithStack:self withAssetUID:assetUid];
return asset;
}
-(AssetLibrary *)assetLibrary {
AssetLibrary *assetLib = [[AssetLibrary alloc] initWithStack:self];
return assetLib;
}
//MARK: - Headers
- (void)setHeader:(NSString *)headerValue forKey:(NSString *)headerKey {
[self.stackHeaders setObject:headerValue forKey:headerKey];
}
- (void)addHeadersWithDictionary:(NSDictionary *)headers {
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[self.stackHeaders setObject:obj forKey:key];
}];
}
- (void)removeHeaderForKey:(NSString *)headerKey {
if (self.stackHeaders[headerKey]) {
[self.stackHeaders removeObjectForKey:headerKey];
}
}
- (NSDictionary *)getHeaders {
return [self.stackHeaders copy];
}
- (NSString *)imageTransformWithUrl:(NSString *)url andParams:(NSDictionary<NSString *, id> *)params{
if([url rangeOfString:@"?" options:NSCaseInsensitiveSearch].length==0) {
url = [url stringByAppendingString:@"?"];
}
for (id key in params) {
id value = [params objectForKey:key];
url = [url stringByAppendingString:[NSString stringWithFormat: @"&%@=%@", key, value]];
}
url = [url stringByReplacingOccurrencesOfString:@" " withString:@""];
url = [url stringByReplacingOccurrencesOfString:@"?&" withString:@"?"];
return url;
}
- (void)syncCallWithParams:(NSDictionary*)params withCompletion:(void (^)(NSDictionary *responseDictionary, NSError *error))completionBlock {
NSString *path = [CSIOAPIURLs syncWithVersion:self.version];
NSMutableDictionary *paramsDictionary = [NSMutableDictionary dictionaryWithDictionary:params];
NSURLSessionDataTask *op = [self.network requestForStack:self withURLPath:path requestType:CSIOCoreNetworkingRequestTypeGET params:paramsDictionary additionalHeaders:self.stackHeaders completion:^(ResponseType responseType, id responseJSON, NSError *error) {
if (completionBlock) {
if (error) {
completionBlock(nil, error);
}else {
NSDictionary *responseData = responseJSON;
completionBlock(responseData, nil);
}
}
}];
if (op && ![op isKindOfClass:[NSNull class]]) {
[self.requestOperationSet addObject:op];
}
}
-(void)sync:(void (^)(SyncStack * BUILT_NULLABLE_P syncResult, NSError * BUILT_NULLABLE_P error))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:nil];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
-(void)syncToken:(NSString *)token completion:(void (^)(SyncStack * BUILT_NULLABLE_P syncResult, NSError * BUILT_NULLABLE_P error))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{@"sync_token": token}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
-(void)syncPaginationToken:(NSString *)token completion:(void (^)(SyncStack * BUILT_NULLABLE_P syncResult, NSError * BUILT_NULLABLE_P error))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{@"pagination_token": token}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
- (void)syncFrom:(NSDate*)date completion:(void (^)(SyncStack * BUILT_NULLABLE_P syncResult, NSError * BUILT_NULLABLE_P error))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{kCSIO_Start_From : [_commonDateFormatter stringFromDate:date]}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
- (void)syncOnly:(NSString *)contentType completion:(void (^)(SyncStack * BUILT_NULLABLE_P syncResult, NSError * BUILT_NULLABLE_P error))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{kCSIO_Content_Type: contentType}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
- (void)syncOnly:(NSString *)contentType from:(NSDate *)date completion:(void (^)(SyncStack * BUILT_NULLABLE_P syncResult, NSError * BUILT_NULLABLE_P error))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{kCSIO_Start_From : [_commonDateFormatter stringFromDate:date], kCSIO_Content_Type: contentType}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
-(void)syncLocale:(NSString*)locale completion:(void (^)(SyncStack * _Nullable, NSError * _Nullable))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{kCSIO_Locale : locale}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
-(void)syncLocale:(NSString*)locale from:(NSDate *)date completion:(void (^)(SyncStack * _Nullable, NSError * _Nullable))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{kCSIO_Locale : locale, kCSIO_Start_From : [_commonDateFormatter stringFromDate:date]}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
-(void)syncPublishType:(PublishType)publishType completion:(void (^)(SyncStack * _Nullable, NSError * _Nullable))completionBlock {
SyncStack *syncStack = [self getCurrentSyncStack:@{kCSIO_Type : [self publishType:publishType]}];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
-(void)syncOnly:(NSString *)contentType locale:(NSString*)locale from:(NSDate *)date completion:(void (^)(SyncStack * _Nullable, NSError * _Nullable))completionBlock {
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
[paramsDict setValue:contentType forKey:kCSIO_Content_Type];
[paramsDict setValue:locale forKey:kCSIO_Locale];
if (date != nil) {
[paramsDict setValue:[_commonDateFormatter stringFromDate:date] forKey:kCSIO_Start_From];
}
SyncStack *syncStack = [self getCurrentSyncStack:paramsDict];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
-(void)syncOnly:(NSString *)contentType locale:(NSString*)locale from:(NSDate *)date publishType:(PublishType)publishType completion:(void (^)(SyncStack * _Nullable, NSError * _Nullable))completionBlock {
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
[paramsDict setValue:contentType forKey:kCSIO_Content_Type];
[paramsDict setValue:locale forKey:kCSIO_Locale];
[paramsDict setValue:[self publishType:publishType] forKey:kCSIO_Type];
if (date != nil) {
[paramsDict setValue:[_commonDateFormatter stringFromDate:date] forKey:kCSIO_Start_From];
}
SyncStack *syncStack = [self getCurrentSyncStack:paramsDict];
[self sync:syncStack completion:^(SyncStack * _Nullable syncResult, NSError * _Nullable error) {
completionBlock(syncResult, error);
}];
}
- (void)sync:(SyncStack *)syncResult completion:(void (^)(SyncStack * BUILT_NULLABLE_P syncResult, NSError * BUILT_NULLABLE_P error))completionBlock {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:[syncResult getParameters]];
if (syncResult.paginationToken == nil && syncResult.syncToken == nil) {
[params setValue:self.environment forKey:@"environment"];
}
[self syncCallWithParams:params withCompletion:^(NSDictionary *responseDictionary, NSError *error) {
if (error == nil) {
[syncResult parseSyncResult:responseDictionary];
if (syncResult.hasMorePages) {
[self sync:syncResult completion:completionBlock];
}
}
completionBlock(syncResult, error);
}];
}
- (SyncStack*) getCurrentSyncStack:(NSDictionary*) params {
SyncStack *syncResult = [[SyncStack alloc] initWithParmas:params];
return syncResult;
}
@end