1

I have created a network manager class which inherits from AFHTTPSessionManager and I have defined methods for GET, POST, etc. in the class. Here is the POST implementation -

- (NSURLSessionTask*)performPOSTRequestToURL:(NSString*)postURL andParameters:(NSDictionary*)parameters withCompletionBlock:(WebServiceCompletionResponse)completionBlock {

    [self POST:postURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        /* Token is still valid, got success response / data */
        if (completionBlock) {
            completionBlock(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if ([error code] == SESSION_EXPIRED_CODE) { /* Token has expired because error code is SESSION_EXPIRED_CODE */

            /* Make call to refresh token */
            [[TokenLibrary sharedInstance] refreshToken:^(NSError *error, NSString *token) {
                if (error) {
                    completionBlock(error);
                }
                else{
                    /* Got refreshed token, I want to call performPOSTRequestToURL with the same url now. How do I do it? Is the call below okay? */
                    [self performPOSTRequestToURL:postURL andParameters:parameters withCompletionBlock:completionBlock];
                }
            }];
        }
    }];
}

I have put in my question in the last comment above.

Basically, I make my network calls to post or get data and if there is a SERVER_EXPIRED_CODE as error response I get from webservice, I need to refresh the token first and then make the same API call again.

Can I directly make the method call in place of the last comment? Or there is another or better ways to do it?

letsbondiway
  • 470
  • 3
  • 18
  • 2
    Yes you are on the right track, thats a clean way to recall the api once you successfully refresh the token. – AtWork Jul 22 '16 at 19:08
  • @AtWork Thanks for the quick reply. I have edited the code block to actually make the call - [self performPOSTRequestToURL:postURL andParameters:parameters withCompletionBlock:completionBlock]; This looks okay? – letsbondiway Jul 22 '16 at 19:18
  • Looks fine to me, should work. Did you try running it? – AtWork Jul 22 '16 at 19:21
  • Yes, I did but haven't been able to retrieve SESSION_EXPIRED_CODE yet. Will try to simulate that somehow or reach out to service team. Otherwise it works good. Thanks a lot for responding. – letsbondiway Jul 22 '16 at 19:25
  • Try running the service in a browser if it is good or not. – AtWork Jul 22 '16 at 19:28

1 Answers1

0

in this block you can still access current call parameters because it's captured by block.Just using [self performPOSTRequestToURL: postURL ...] call current method with same parameters.

Ali Kıran
  • 1,006
  • 7
  • 12
  • Thanks, I edited the code block with actual call again. I believe that is how it should be? – letsbondiway Jul 22 '16 at 19:19
  • maybe it's better to implement token refresh token logic inside to POST method.Even making it more generic for using also with PUT, PATCH... http methods you should automatically sniff server errors with generic method after those methods call responses. – Ali Kıran Jul 22 '16 at 19:25
  • The [self POST...] call is that to the AFHTTPSessionManager's method. Yes, but I understand your point. I will try to make it generic or cleaner in some other way. – letsbondiway Jul 22 '16 at 19:28
  • please check this answers they talking about overriding afnetworking methods to handle repeating calls http://stackoverflow.com/questions/12893837/afnetworking-handle-error-globally-and-repeat-request – Ali Kıran Jul 22 '16 at 19:34