diff options
Diffstat (limited to 'Juick/User.m')
-rw-r--r-- | Juick/User.m | 47 |
1 files changed, 12 insertions, 35 deletions
diff --git a/Juick/User.m b/Juick/User.m index 97f4232..f4b1e00 100644 --- a/Juick/User.m +++ b/Juick/User.m @@ -7,8 +7,7 @@ // #import "User.h" -#import "PDKeychainBindings.h" -#import "SIAlertView.h" + @implementation User @@ -25,48 +24,26 @@ } +(void) checkIsValid:(void (^)(BOOL))callback { - RKObjectManager *manager = [RKObjectManager sharedManager]; - [manager.HTTPClient setAuthorizationHeaderWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; - NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodPOST path:@"/post" parameters:nil]; - RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; - - RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; - - RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request - responseDescriptors:@[responseDescriptor]]; - [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { - // this will never happens - } failure:^(RKObjectRequestOperation *operation, NSError *error) { - if (operation.HTTPRequestOperation.response.statusCode == 400) { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + [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.HTTPClient setAuthorizationHeaderWithUsername:nil password:nil]; + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:nil password:nil]; callback(NO); } - }]; - [operation start]; - -} + }];} +(void) get:(NSString *) name callback:(void(^)(User *))callback { - RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; - [mapping addAttributeMappingsFromDictionary:@ - { - @"uname": @"uname", - @"uid" : @"uid", - }]; - NSString *path = @"/users"; + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil]; - RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; - NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodGET path:path parameters:params]; - RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request - responseDescriptors:@[responseDescriptor]]; - [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { - callback([[mappingResult array] firstObject]); - } failure:^(RKObjectRequestOperation *operation, NSError *error) { + [manager GET:@"/users" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + callback([User yy_modelWithJSON:[(NSArray *)responseObject firstObject]]); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { callback(nil); }]; - [operation start]; } @end |