diff options
author | Vitaly Takmazov | 2019-03-20 02:35:23 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2019-03-20 02:35:23 +0300 |
commit | 458e55b9cddcadb2bbbe3fb4002f06a5744b84c0 (patch) | |
tree | aa32d167c3a6914b1fd326085e94d2a2cb43b057 /Juick | |
parent | 1f3e08f107064fa429c6aa2a26702f6e63a791f0 (diff) |
Move all API calls to APIClient
Diffstat (limited to 'Juick')
-rw-r--r-- | Juick/APIClient.h | 5 | ||||
-rw-r--r-- | Juick/APIClient.m | 57 | ||||
-rw-r--r-- | Juick/AppDelegate.m | 11 | ||||
-rw-r--r-- | Juick/Model/DeviceRegistration.h | 9 | ||||
-rw-r--r-- | Juick/Model/DeviceRegistration.m | 19 | ||||
-rw-r--r-- | Juick/Model/User.h | 4 | ||||
-rw-r--r-- | Juick/Model/User.m | 22 | ||||
-rw-r--r-- | Juick/ViewControllers/DiscoverViewController.m | 2 | ||||
-rw-r--r-- | Juick/ViewControllers/LoginViewController.m | 7 |
9 files changed, 72 insertions, 64 deletions
diff --git a/Juick/APIClient.h b/Juick/APIClient.h index 5cf6437..078b91f 100644 --- a/Juick/APIClient.h +++ b/Juick/APIClient.h @@ -7,10 +7,10 @@ // #import <Foundation/Foundation.h> #import "Message.h" +#import "DeviceRegistration.h" @interface APIClient : NSObject -@property (nonatomic, retain) AFHTTPSessionManager *manager; @property (nonatomic, strong) NSDateFormatter *dateFormatter; +(APIClient *) sharedClient; @@ -21,6 +21,9 @@ -(void) postPMToUser:(NSString *)uname text:(NSString *)text result:(void(^)(NSError *))callback; -(void) fetchChats:(void(^)(NSArray *, NSError *))callback; -(void) fetchChatWithUser:(NSString *)uname callback:(void(^)(NSArray *, NSError *))callback; +-(void) authenticate:(void(^)(BOOL))callback; +-(void) refreshDeviceRegistration:(DeviceRegistration *)registrationData callback:(void(^)(BOOL))callback; +-(void) getUserByName:(NSString *) name callback:(void(^)(User *))callback; +(NSString *) messagesUrl; +(NSString *) threadUrl; 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 { diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m index 5aff686..f1afdc1 100644 --- a/Juick/AppDelegate.m +++ b/Juick/AppDelegate.m @@ -68,12 +68,11 @@ DeviceRegistration *registration = [DeviceRegistration new]; registration.type = @"apns"; registration.token = token; - AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - [manager PUT:@"/notifications" parameters:@[[registration toJSON]] success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { - NSLog(@"success %@", token); - } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { - NSLog(@"fail %@", [error localizedDescription]); - } ]; + [[APIClient sharedClient] refreshDeviceRegistration:registration callback:^(BOOL success) { + if (success) { + NSLog(@"successfully refreshed registration with %@", token); + } + }]; }]; } diff --git a/Juick/Model/DeviceRegistration.h b/Juick/Model/DeviceRegistration.h index d5b620c..bd786ef 100644 --- a/Juick/Model/DeviceRegistration.h +++ b/Juick/Model/DeviceRegistration.h @@ -14,12 +14,3 @@ -(NSDictionary *) toJSON; @end - -@implementation DeviceRegistration --(NSDictionary *) toJSON { - return @{ - @"type": self.type, - @"token": self.token - }; -} -@end diff --git a/Juick/Model/DeviceRegistration.m b/Juick/Model/DeviceRegistration.m new file mode 100644 index 0000000..c1728f4 --- /dev/null +++ b/Juick/Model/DeviceRegistration.m @@ -0,0 +1,19 @@ +// +// DeviceRegistration.m +// Juick +// +// Created by Vitaly Takmazov on 20/03/2019. +// Copyright © 2019 com.juick. All rights reserved. +// + +#import <Foundation/Foundation.h> +#import "DeviceRegistration.h" + +@implementation DeviceRegistration +-(NSDictionary *) toJSON { + return @{ + @"type": self.type, + @"token": self.token + }; +} +@end diff --git a/Juick/Model/User.h b/Juick/Model/User.h index 59b010d..92a6333 100644 --- a/Juick/Model/User.h +++ b/Juick/Model/User.h @@ -15,12 +15,8 @@ + (User *) fromJSON:(NSDictionary *)jsonData; -+ (void) get:(NSString *)name callback:(void(^)(User *))callback; - + (BOOL) isAuthenticated; -+ (void) checkIsValid:(void(^)(BOOL))callback; - + (void) throwUnableToLogin:(UIViewController *)view; @end diff --git a/Juick/Model/User.m b/Juick/Model/User.m index 7f97e34..6d5e818 100644 --- a/Juick/Model/User.m +++ b/Juick/Model/User.m @@ -38,26 +38,4 @@ [view presentViewController:alert animated:YES completion:nil]; } -+(void) checkIsValid:(void (^)(BOOL))callback { - AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - [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) { - callback(YES); - } else { - [manager.requestSerializer clearAuthorizationHeader]; - callback(NO); - } - }];} - -+(void) get:(NSString *) name callback:(void(^)(User *))callback { - AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil]; - [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); - }]; -} - @end diff --git a/Juick/ViewControllers/DiscoverViewController.m b/Juick/ViewControllers/DiscoverViewController.m index 27b273b..edbd152 100644 --- a/Juick/ViewControllers/DiscoverViewController.m +++ b/Juick/ViewControllers/DiscoverViewController.m @@ -24,7 +24,7 @@ -(void) viewDidLoad { self.messagesDelegate = self; if ([User isAuthenticated]) { - [User checkIsValid:^(BOOL success) { + [[APIClient sharedClient] authenticate:^(BOOL success) { if (success) { if ([self.path length] == 0) { self.path = [APIClient feedUrl]; diff --git a/Juick/ViewControllers/LoginViewController.m b/Juick/ViewControllers/LoginViewController.m index e1b12f2..f8cbcc8 100644 --- a/Juick/ViewControllers/LoginViewController.m +++ b/Juick/ViewControllers/LoginViewController.m @@ -56,12 +56,8 @@ } - (void) doneSignIn { - [User checkIsValid:^(BOOL success) { + [[APIClient sharedClient] authenticate:^(BOOL success) { if (success) { - NSString *username = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"]; - NSString *password = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"]; - [[APIClient sharedClient].manager.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password]; - [[AppDelegate shared] registerForRemoteNotifications]; CATransition* transition = [CATransition animation]; transition.duration = 0.3; @@ -74,7 +70,6 @@ [User throwUnableToLogin:self]; } }]; - } - (IBAction)passwordChanged:(id)sender { if ([self.passwordField.text isKindOfClass:[NSString class]]) { |