RestKit,拦截失败的请求/重试,如果它是由于令牌过期引起的
RestKit , Intercept failed request/ retry if it was caused due to token Expiration
我有一个名为 API 的 class 助手,其方法如下所示:
+(RKObjectManager*) getRestObjectManager{
NSURL *baseURL = [NSURL URLWithString:BASE_URL];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
return objectManager;
}
然后我将创建 class 类 API_User 、 API_Group 等。这些 class 中的每一个都将具有
类的方法
+(void)getDetails:(void (^)(User* user) )onSuccess{
//fetch object manager from api helper and perform request, on success, call the onSuccess block from the function parameter.
onSuccess(user); //if it was successful, i will create a user object and //return.
}
将有多种方法,例如 getDetails ,每种方法都需要发送身份验证令牌才能工作。令牌可以过期,需要刷新。
我该怎么做:
在API helper 中定义某种拦截器,这样当请求失败时,它将获取一个新令牌(我的令牌过期响应本身提供了一个新令牌),并重试已经过期的请求失败的?我不想为我定义的每个端点处理这个。
我所做的是扩展 RKObject Manager 并像这样处理故障:
@implementation MYOWNObjectManager
#pragma mark - RKObjectManager Overrides
- (void)getObjectsAtPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(RKObjectRequestOperation *operation,
RKMappingResult *mappingResult))success failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure {
[super getObjectsAtPath:path parameters:parameters success:success failure:^(RKObjectRequestOperation *operation, NSError *error) {
//check if failure was due to token expiry, if yes call the code to refresh token. otherwise just call failure(operation, error);
[super getObjectsAtPath:path parameters:parameters success:success failure:failure]; //this line performs the request again.
}];
}
此代码段仅适用于 GET。您还需要使用相同的逻辑
覆盖 PUT/POST 等
我有一个名为 API 的 class 助手,其方法如下所示:
+(RKObjectManager*) getRestObjectManager{
NSURL *baseURL = [NSURL URLWithString:BASE_URL];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
return objectManager;
}
然后我将创建 class 类 API_User 、 API_Group 等。这些 class 中的每一个都将具有
类的方法+(void)getDetails:(void (^)(User* user) )onSuccess{
//fetch object manager from api helper and perform request, on success, call the onSuccess block from the function parameter.
onSuccess(user); //if it was successful, i will create a user object and //return.
}
将有多种方法,例如 getDetails ,每种方法都需要发送身份验证令牌才能工作。令牌可以过期,需要刷新。
我该怎么做:
在API helper 中定义某种拦截器,这样当请求失败时,它将获取一个新令牌(我的令牌过期响应本身提供了一个新令牌),并重试已经过期的请求失败的?我不想为我定义的每个端点处理这个。
我所做的是扩展 RKObject Manager 并像这样处理故障:
@implementation MYOWNObjectManager
#pragma mark - RKObjectManager Overrides
- (void)getObjectsAtPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(RKObjectRequestOperation *operation,
RKMappingResult *mappingResult))success failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure {
[super getObjectsAtPath:path parameters:parameters success:success failure:^(RKObjectRequestOperation *operation, NSError *error) {
//check if failure was due to token expiry, if yes call the code to refresh token. otherwise just call failure(operation, error);
[super getObjectsAtPath:path parameters:parameters success:success failure:failure]; //this line performs the request again.
}];
}
此代码段仅适用于 GET。您还需要使用相同的逻辑
覆盖 PUT/POST 等