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
|
//
// APIClient.m
// Juick
//
// Created by Vitaly Takmazov on 22/08/16.
// Copyright © 2016 com.juick. All rights reserved.
//
#import "APIClient.h"
#import "PDKeychainBindings.h"
#import "Message.h"
#import "Chat.h"
@interface APIClient()
@property(nonatomic, strong) NSOperationQueue *backgroundQueue;
@end
@implementation APIClient
+(APIClient *) sharedClient {
static APIClient *sharedAPIClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedAPIClient = [[self alloc] init];
});
return sharedAPIClient;
}
-(id)init {
if (self = [super init]) {
NSString *baseURLString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"base_url"];
NSLog(@"Initializing with %@ base URL", baseURLString);
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];
self.manager.requestSerializer = [AFJSONRequestSerializer new];
[self.manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
self.backgroundQueue = [NSOperationQueue new];
self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
}
return self;
}
-(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *, NSError *))callback {
AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
[self.backgroundQueue addOperationWithBlock:^{
[manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSMutableArray *messages = [NSMutableArray new];
[((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[messages addObject:[Message yy_modelWithJSON:obj]];
}];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(messages, nil);
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"REST Error: %@", error);
NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode;
if (statusCode == 401) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(nil, [[NSError alloc] initWithDomain:@"JuickErrorDomain" code:401 userInfo:nil]);
}];
}
}];
}];
}
-(void) postMessage:(NSString *)text result:(void (^)(Message *, NSError *))callback {
AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
[self.backgroundQueue addOperationWithBlock:^{
[manager POST:@"post" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFormData:[text dataUsingEncoding:NSUTF8StringEncoding] name:@"body"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"Success!");
Message *result = [Message yy_modelWithJSON:responseObject];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(result, nil);
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", [error localizedDescription]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(nil, error);
}];
}];
}];
}
-(void) postReplyToThread:(NSNumber *)mid inReplyTo:(NSNumber *)rid text:(NSString *)text result:(void(^)(Message *, NSError *))callback {
AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
[self.backgroundQueue addOperationWithBlock:^{
[manager POST:@"comment" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFormData:[[mid stringValue] dataUsingEncoding:NSUTF8StringEncoding] name:@"mid"];
[formData appendPartWithFormData:[[NSString stringWithFormat:@"%d", [rid intValue]] dataUsingEncoding:NSUTF8StringEncoding] name:@"rid"];
[formData appendPartWithFormData:[text dataUsingEncoding:NSUTF8StringEncoding] name:@"body"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"Success!");
Message *reply = [Message yy_modelWithJSON:responseObject];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(reply, nil);
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", [error localizedDescription]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(nil, error);
}];
}];
}];
}
-(void) postPMToUser:(NSString *)uname text:(NSString *)text result:(void (^)(NSError *))callback {
AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
[self.backgroundQueue addOperationWithBlock:^{
[manager POST:@"pm" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFormData:[text dataUsingEncoding:NSUTF8StringEncoding] name:@"body"];
[formData appendPartWithFormData:[uname dataUsingEncoding:NSUTF8StringEncoding] name:@"uname"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(nil);
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", [error localizedDescription]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(error);
}];
}];
}];
}
-(void) fetchChats:(void (^)(NSArray *, NSError *))callback {
AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
[self.backgroundQueue addOperationWithBlock:^{
[manager GET:@"groups_pms" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSMutableArray *groups = [NSMutableArray new];
NSArray *pms = [(NSDictionary *)responseObject objectForKey:@"pms"];
[pms enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[groups addObject:[Chat yy_modelWithJSON:obj]];
}];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(groups, nil);
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", [error localizedDescription]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(nil, error);
}];
}];
}];
}
-(void) fetchChatWithUser:(NSString *)uname callback:(void (^)(NSArray *, NSError *))callback {
AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
NSDictionary *params = @{@"uname": uname};
[self.backgroundQueue addOperationWithBlock:^{
[manager GET:@"pm" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSMutableArray *messages = [NSMutableArray new];
NSArray *messagesList = (NSArray *)responseObject;
[messagesList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[messages addObject:[Message yy_modelWithJSON:obj]];
}];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(messages, nil);
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", [error localizedDescription]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(nil, error);
}];
}];
}];
}
+(NSString *) messagesUrl {
return @"messages";
}
+(NSString *) discussionsUrl {
return @"messages/discussions";
}
+(NSString *) threadUrl {
return @"thread";
}
+(NSString *) feedUrl {
return @"home";
}
@end
|