summaryrefslogtreecommitdiff
path: root/Juick/APIClient.m
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-03-20 02:35:23 +0300
committerGravatar Vitaly Takmazov2019-03-20 02:35:23 +0300
commit458e55b9cddcadb2bbbe3fb4002f06a5744b84c0 (patch)
treeaa32d167c3a6914b1fd326085e94d2a2cb43b057 /Juick/APIClient.m
parent1f3e08f107064fa429c6aa2a26702f6e63a791f0 (diff)
Move all API calls to APIClient
Diffstat (limited to 'Juick/APIClient.m')
-rw-r--r--Juick/APIClient.m57
1 files changed, 42 insertions, 15 deletions
diff --git a/Juick/APIClient.m b/Juick/APIClient.m
index ae2ebad..d3bd248 100644
--- a/Juick/APIClient.m
+++ b/Juick/APIClient.m
@@ -10,6 +10,7 @@
#import "Chat.h"
@interface APIClient()
+@property(nonatomic, readwrite) AFHTTPSessionManager *manager;
@property(nonatomic, strong) NSOperationQueue *backgroundQueue;
@end
@@ -28,9 +29,9 @@
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];
+ _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];
+ _manager.requestSerializer = [AFJSONRequestSerializer new];
+ [_manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
self.backgroundQueue = [NSOperationQueue new];
self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
@@ -50,9 +51,8 @@
}
-(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) {
+ [self.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 fromJSON:obj]];
@@ -74,9 +74,8 @@
}
-(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) {
+ [self.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!");
@@ -94,9 +93,8 @@
}
-(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) {
+ [self.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"];
@@ -115,9 +113,8 @@
}];
}
-(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) {
+ [self.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) {
@@ -134,9 +131,8 @@
}
-(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) {
+ [self.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) {
@@ -154,10 +150,9 @@
}];
}
-(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) {
+ [self.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) {
@@ -174,6 +169,38 @@
}];
}];
}
+-(void) authenticate:(void (^)(BOOL))callback {
+ [self.manager POST:@"post" parameters:nil progress:nil success:nil failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
+ NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode;
+ if (statusCode == 400) {
+ NSString *username = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"];
+ NSString *password = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"];
+ [self.manager.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password];
+ callback(YES);
+ } else {
+ [self.manager.requestSerializer clearAuthorizationHeader];
+ callback(NO);
+ }
+ }];
+}
+
+-(void) getUserByName:(NSString *) name callback:(void(^)(User *))callback {
+ NSDictionary *params = @{@"uname": name};
+ [self.manager GET:@"users" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
+ callback([User fromJSON:[(NSArray *)responseObject firstObject]]);
+ } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
+ callback(nil);
+ }];
+}
+
+-(void) refreshDeviceRegistration:(DeviceRegistration *)registrationData callback:(void (^)(BOOL))callback {
+ [self.manager PUT:@"/notifications" parameters:@[[registrationData toJSON]] success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
+ callback(YES);
+ } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
+ NSLog(@"fail %@", [error localizedDescription]);
+ callback(NO);
+ } ];
+}
+(NSString *) messagesUrl {