diff options
author | Vitaly Takmazov | 2017-09-27 22:24:38 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-09-27 22:24:38 +0300 |
commit | 6499148cb3f62e20534b62c181125ebeacfa3438 (patch) | |
tree | 3aa289caa409366e70dd1802d67df61903fa992b /Juick/APIClient.m | |
parent | 25bc4f1357252dc0aeee70f0a638a4226231d5fb (diff) |
cleanup
Diffstat (limited to 'Juick/APIClient.m')
-rw-r--r-- | Juick/APIClient.m | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Juick/APIClient.m b/Juick/APIClient.m index 1fc2743..d2356b5 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -6,6 +6,8 @@ // Copyright © 2016 com.juick. All rights reserved. // #import "APIClient.h" +#import "PDKeychainBindings.h" +#import "Message.h" @implementation APIClient @@ -25,4 +27,45 @@ return self; } +-(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *, NSError *))callback { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; + if ([[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil) { + manager.requestSerializer = [AFJSONRequestSerializer serializer]; + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + } + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [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]]; + }]; + dispatch_async(dispatch_get_main_queue(), ^{ + callback(messages, nil); + }); + + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSLog(@"REST Error: %@", error); + NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; + if (statusCode == 401) { + dispatch_async(dispatch_get_main_queue(), ^{ + callback(nil, [[NSError alloc] initWithDomain:@"JuickErrorDomain" code:401 userInfo:nil]); + }); + } + }]; + }); +} + + ++(NSString *) messagesUrl { + return @"/messages"; +} + ++(NSString *) threadUrl { + return @"/thread"; +} + ++(NSString *) feedUrl { + return @"/home"; +} + @end |