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

Some methods of custom URLSessionDelegate are ignored by APIClient #81

Closed
Mrteller opened this issue Jul 10, 2023 · 2 comments
Closed
Labels
question Further information is requested

Comments

@Mrteller
Copy link

It is handy we can set our own custom URLSessionDelegate in APIClient init.

        self.api = APIClient(baseURL: baseURL, {
            $0.sessionDelegate = ...
        })

But custom delegate methods are just not called for simple GET/POST queries. Don't think this how it should be. It looks like DataLoader.swift just lacks them.

Particularly in my case (I need to do some SSl pinning) if I add the following code to DataLoader.swift everything begins to work as expected:

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        userSessionDelegate?.urlSession?(session, didReceive: challenge, completionHandler: completionHandler) ??
        completionHandler(.performDefaultHandling, nil)
    }

It looks like delegation is forwarded only for some subtypes of URLSessionDelegate (URLSessionTaskDelegate, URLSessionDataDelegate, URLSessionDownloadDelegate) but not for the type itself. Just not sure if it will be correct to implement such forwarding for both URLSessionDelegate and it subtypes. That's why made it an issue, not a pull request.

@joliejuly
Copy link

I have the same problem. Also, some methods like didFinishWithError are not called neither.

@kean kean added the bug Something isn't working label Nov 26, 2023
@kean
Copy link
Owner

kean commented Nov 26, 2023

I might be wrong because it's been a while, but I think the situation with authentication challenges is that there is a per-session and a per-task delegate method, which are mutually exclusive. I would suggest implementing a per-task one, which is part of the DataLoader implementation:

func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

It's more powerful/granular, hence why only a task-based method is implemented.

@kean kean added question Further information is requested and removed bug Something isn't working labels Nov 26, 2023
@kean kean closed this as completed Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants