-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContentType.m
More file actions
101 lines (85 loc) · 3.4 KB
/
ContentType.m
File metadata and controls
101 lines (85 loc) · 3.4 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
//
// ContentType.m
// Contentstack
//
// Created by Reefaq on 22/06/15.
// Copyright (c) 2015 Contentstack. All rights reserved.
//
#import "ContentType.h"
#import "CSIOInternalHeaders.h"
#import "CSIOConstants.h"
#import "CSIOCoreHTTPNetworking.h"
#import "CSIOAPIURLs.h"
#import "NSObject+Extensions.h"
#import <Contentstack/Stack.h>
#import "Query.h"
#import "Entry.h"
#import "Asset.h"
#import "AssetLibrary.h"
@interface ContentType ()
@property (nonatomic, strong, getter=stack) Stack *csStack;
@end
@implementation ContentType
-(instancetype)initWithStack:(Stack*)stack withName:(NSString*)contentTypeName {
if (self= [super init]) {
_csStack = stack;
_name = contentTypeName;
_postParamDictionary = [NSMutableDictionary dictionary];
_headers = [NSMutableDictionary dictionary];
}
return self;
}
-(Entry*)entry {
Entry *entry = [[Entry alloc] initWithContentType:self];
return entry;
}
-(Entry*)entryWithUID:(NSString*)uid; {
Entry *entry = [[Entry alloc] initWithContentType:self withEntryUID:uid];
return entry;
}
-(Query*)query {
Query *query = [[Query alloc] initWithContentType:self];
return query;
}
//MARK: - Headers
- (void)setHeader:(NSString *)headerValue forKey:(NSString *)headerKey {
[self.headers setObject:headerValue forKey:headerKey];
}
- (void)addHeadersWithDictionary:(NSDictionary<NSString *, NSString *> *)headers {
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[self.headers setObject:obj forKey:key];
}];
}
- (void)removeHeaderForKey:(NSString *)headerKey {
if (self.headers[headerKey]) {
[self.headers removeObjectForKey:headerKey];
}
}
//MARK: - Get ContentTypes
- (void)fetch:(NSDictionary<NSString *,id> * _Nullable)params completion:(void (^)(NSDictionary<NSString *,NSString *> * _Nullable, NSError * _Nullable))completionBlock {
NSString *path = [CSIOAPIURLs fetchContenTypeSchema:self.name withVersion:self.stack.version];
[self.postParamDictionary setObject:_csStack.environment forKey:kCSIO_Environment];
NSMutableDictionary *paramDictionary = [NSMutableDictionary dictionaryWithDictionary:self.postParamDictionary];
for (NSString* key in params) {
[paramDictionary setValue:[params valueForKey:key] forKey:key];
}
NSURLSessionDataTask *op = [self.stack.network requestForStack:self.stack withURLPath:path requestType:CSIOCoreNetworkingRequestTypeGET params:paramDictionary additionalHeaders:self.stack.stackHeaders completion:^(ResponseType responseType, id responseJSON, NSError *error) {
if (completionBlock) {
if (error) {
completionBlock(nil, error);
}else {
NSDictionary *responseData = responseJSON;
if ([[responseData allKeys] containsObject:@"content_type"] && [responseData objectForKey:@"content_type"] != nil && [[responseData objectForKey:@"content_type"] isKindOfClass:[NSDictionary class]]) {
completionBlock([responseData objectForKey:@"content_type"], 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.stack.requestOperationSet addObject:op];
}
}
@end