RestKit:重新认证后如何重新提交失败的请求? [英] RestKit: How to resubmit failed request after re-authenticating?

查看:96
本文介绍了RestKit:重新认证后如何重新提交失败的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发的API要求我在自定义HTTP标头中提供身份验证令牌.该令牌每隔几分钟就会过期一次,并且只要用户闲置了足够长的时间,便可能会在用户仍在应用程序中时发生.令牌过期后,我会收到403响应,但只有在尝试请求后才能找到.

The API I'm developing against requires me to present an authentication token in a custom HTTP header. This token expires every few minutes, and could happen while the user is still within the app as long as they have been idle long enough. When the token has expired I receive a 403 response but I only find out after attempting a request.

让RestKit自动重新认证并重试请求的最佳方法是什么,这样我就不必在发出请求的所有地方都采用这种逻辑?建议使用RKRequestDelegate协议或RKObjectLoaderDelegate协议来回答类似的问题,但不幸的是,这些不再是RestKit的一部分(从0.20开始).

What's the best way to get RestKit to automatically reauthenticate and retry the request so I don't have to put in this logic everywhere I make a request? Responses to similar questions have suggested using the RKRequestDelegate protocol or the RKObjectLoaderDelegate protocol but unfortunately these are no longer part of RestKit (as of 0.20).

任何想法现在应该是什么正确"方法?我应该为RKObjectManager子类化并重试每个请求操作,还是应该提供自定义HTTPOperation或HTTPClient类,或者是否有更好的方法?谢谢!

Any idea what the "correct" approach should be now? Should I be subclassing RKObjectManager and tacking on a retry to each of the request operations or should I provide a custom HTTPOperation or HTTPClient class or is there some better approach altogether? Thanks!

推荐答案

将其捕获到失败"块中,然后检查状态码并重新进行身份验证

Catch it in Failure block , and check for the status code and re-do the authentication

    RKObjectRequestOperation *requestOp = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[getObjResp]];

            [requestOp setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                ....
                }
            } failure:^(RKObjectRequestOperation *operation, NSError *error){
// Here your status code check                
// Here your retry-code
                }

这篇关于RestKit:重新认证后如何重新提交失败的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆