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 | |
parent | 1f3e08f107064fa429c6aa2a26702f6e63a791f0 (diff) |
Move all API calls to APIClient
-rw-r--r-- | Juick.xcodeproj/project.pbxproj | 4 | ||||
-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 |
10 files changed, 76 insertions, 64 deletions
diff --git a/Juick.xcodeproj/project.pbxproj b/Juick.xcodeproj/project.pbxproj index 14a1336..b0e9a95 100644 --- a/Juick.xcodeproj/project.pbxproj +++ b/Juick.xcodeproj/project.pbxproj @@ -48,6 +48,7 @@ 77B8B399207A52BD005CB20C /* MessageInputView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 77B8B398207A52BD005CB20C /* MessageInputView.xib */; }; 77B8B39C207A5629005CB20C /* MessageInputView.m in Sources */ = {isa = PBXBuildFile; fileRef = 77B8B39B207A5629005CB20C /* MessageInputView.m */; }; 77B8DCD62093FC03000DBB04 /* BlogViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 77B8DCD52093FC03000DBB04 /* BlogViewController.m */; }; + 77C3648C2241B3060017522C /* DeviceRegistration.m in Sources */ = {isa = PBXBuildFile; fileRef = 77C3648B2241B3060017522C /* DeviceRegistration.m */; }; 77C67EE51828342000427098 /* NSURL+PathParameters.m in Sources */ = {isa = PBXBuildFile; fileRef = 77C67EE41828342000427098 /* NSURL+PathParameters.m */; }; 77C67EE81828379700427098 /* ColorScheme.m in Sources */ = {isa = PBXBuildFile; fileRef = 77C67EE71828379700427098 /* ColorScheme.m */; }; 77C67EEC18283F2D00427098 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77C67EEB18283F2D00427098 /* QuartzCore.framework */; }; @@ -153,6 +154,7 @@ 77B8B39B207A5629005CB20C /* MessageInputView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MessageInputView.m; sourceTree = "<group>"; }; 77B8DCD42093FC03000DBB04 /* BlogViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BlogViewController.h; sourceTree = "<group>"; }; 77B8DCD52093FC03000DBB04 /* BlogViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BlogViewController.m; sourceTree = "<group>"; }; + 77C3648B2241B3060017522C /* DeviceRegistration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DeviceRegistration.m; sourceTree = "<group>"; }; 77C67EE31828342000427098 /* NSURL+PathParameters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURL+PathParameters.h"; sourceTree = "<group>"; }; 77C67EE41828342000427098 /* NSURL+PathParameters.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+PathParameters.m"; sourceTree = "<group>"; }; 77C67EE61828379700427098 /* ColorScheme.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ColorScheme.h; sourceTree = "<group>"; }; @@ -286,6 +288,7 @@ 77B099A4189D57F000A84F59 /* User.h */, 77B099A5189D57F000A84F59 /* User.m */, 770A428F1E8881CB000B206A /* DeviceRegistration.h */, + 77C3648B2241B3060017522C /* DeviceRegistration.m */, 774528C01F930C06004D110B /* Attachment.h */, 774528C11F930C06004D110B /* Attachment.m */, 773E639B204BD0F2008B8F8D /* Chat.h */, @@ -574,6 +577,7 @@ 776C41BD1FD3EF180063B82E /* MessageCell.m in Sources */, 77317BB8181BBE8500D60005 /* main.m in Sources */, 77B8B39C207A5629005CB20C /* MessageInputView.m in Sources */, + 77C3648C2241B3060017522C /* DeviceRegistration.m in Sources */, 774528C21F930C06004D110B /* Attachment.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; 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]]) { |