- Fix Respose Error grab data

This commit is contained in:
giuseppenuc committed 2016-02-08 14:52:32 +01:00
1 parent 7a2474ea92
commit 85548a7209
9 files changed
+99 -26

No files matched your search

+1 -3
View File
@@ -135,9 +135,7 @@ static bool isFirstAccess = YES;
[self saveLocally];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
NSLogDebug(@"error : %ld",[error code]);
}];
}
+21 -13
View File
@@ -17,7 +17,7 @@
@implementation PNObject (PNObjectConnection)
+ (void) GETWithProgress:(nullable void (^)(NSProgress * _Nonnull downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, PNObject * _Nullable responseObject))success
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) {
@@ -52,7 +52,7 @@
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
Progress:(nullable void (^)(NSProgress * _Nonnull downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, PNObject * _Nullable responseObject))success
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
@@ -83,14 +83,6 @@
}
}
+ (void) POSTWithProgress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, PNObject * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
}
- (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
Progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, PNObject * _Nullable responseObject))success
@@ -100,15 +92,19 @@
[[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint] parameters:[self JSONFormObject] progress:uploadProgress success:^(NSURLSessionDataTask *task, id responseObject) {
id PNObjectResponse = [[[self class] alloc] initWithJSON:[responseObject copy]];
id PNObjectResponse;
if (responseObject) {
PNObjectResponse = [[[self class] alloc] initWithJSON:[responseObject copy]];
}
if (success) {
success(task,PNObjectResponse);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(task,error);
failure(task, error);
}
}];
}
@@ -118,11 +114,23 @@
[self POSTWithEndpointAction:endPoint Progress:uploadProgress success:success failure:failure];
} failure:^(NSError * _Nonnull error) {
if (failure) {
failure(nil,error);
}
}];
}
}
+ (NSError* _Nonnull) getErrorFromTask:(NSURLSessionDataTask* _Nonnull) task andError:(NSError * _Nonnull) error {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];
return [NSError errorWithDomain:[error domain] code:response.statusCode userInfo:serializedData];
}
/*+ (nullable NSURLSessionDataTask *) POSTConstructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> _Nonnull formData))block
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject))success
+4 -1
View File
@@ -9,6 +9,7 @@
#import "PNObjectConfig.h"
#import "PNObjectConstants.h"
#import "PNUser.h"
#import "AFJSONResponseSerializerWithData.h"
NSString * const PNObjectLocalNotificationRefreshTokenClientCredentialSuccess = @"PNObjectLocalNotificationRefreshTokenClientCredentialSuccess";
@@ -218,7 +219,9 @@ static bool isFirstAccess = YES;
[self refreshTokenForClientCredential];
}
}
_manager.responseSerializer = [AFJSONResponseSerializerWithData serializer];
return _manager;
}
@@ -0,0 +1,15 @@
//
// AFJSONResponseSerializerWithData.h
// Pods
//
// Created by Giuseppe Nucifora on 08/02/16.
//
//
#import <AFNetworking/AFNetworking.h>
static NSString * const JSONResponseSerializerWithDataKey = @"JSONResponseSerializerWithDataKey";
@interface AFJSONResponseSerializerWithData : AFJSONResponseSerializer
@end
@@ -0,0 +1,31 @@
//
// AFJSONResponseSerializerWithData.m
// Pods
//
// Created by Giuseppe Nucifora on 08/02/16.
//
//
#import "AFJSONResponseSerializerWithData.h"
@implementation AFJSONResponseSerializerWithData
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error
{
id JSONObject = [super responseObjectForResponse:response data:data error:error];
NSHTTPURLResponse *currentResponse = (NSHTTPURLResponse*)response;
if (*error != nil) {
NSData *errorData = [[(*error).userInfo mutableCopy] objectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];
NSMutableDictionary *userInfo = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];;
NSError *newError = [NSError errorWithDomain:(*error).domain code:currentResponse.statusCode userInfo:userInfo];
(*error) = newError;
}
return (JSONObject);
}
@end