Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconfigure Header Fields #63

Closed
kiroskirin opened this issue Aug 13, 2019 · 2 comments
Closed

Reconfigure Header Fields #63

kiroskirin opened this issue Aug 13, 2019 · 2 comments

Comments

@kiroskirin
Copy link

I setup httpManager like this

extension AppDelegate: HTTPManagerConfigurable {
    func configure(httpManager: HTTPManager) {
        httpManager.environment = APIConfig.staging
            ? HTTPManager.Environment.Staging
            : HTTPManager.Environment.Production

        let config = URLSessionConfiguration.default
        config.timeoutIntervalForRequest = 10
        httpManager.sessionConfiguration = config

        if let token = AuthManager.JWTToken {
            httpManager.defaultHeaderFields.addValue("Bearer \(token)", forHeaderField: "Authorization")
        }

        httpManager.defaultRetryBehavior = HTTPManagerRetryBehavior
            .retryNetworkFailureOrServiceUnavailable(withStrategy: .retryTwiceWithDefaultDelay)
    }
}

Then I have a case when user log out and log in again.
The header fields token is not refreshed.
I have to close the app then open it again to make it work.

How do I handle this case and Is there a way to reset header fields?

@lilyball
Copy link
Collaborator

You can modify the defaultHeaderFields property at any time, not just within this function. Modifications to that property won't affect any requests that have already been created, but it will affect requests created after the modification.

That said, you may want to investigate the defaultAuth property instead, which is the preferred way of dealing with the "Authorization" header. That property is designed to support multiple different auth mechanisms, including allowing the auth object to handle a 401 Unauthorized, refresh its credentials, and automatically retry the request (e.g. for implementing OAuth token refresh). I don't know the details of your auth scheme, so I don't know if you can make use of the flexibility of this object. If you really just have a string value that you want to provide, then this property may not get you anything.

@kiroskirin
Copy link
Author

I just try defaultAuth and it seems to work properly now.
Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants