Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire request interceptor not being called

Adding to my litany of Alamofire SO posts this week, why doesnt my RequestInterceptor work? Nothing inside of the adapt function gets called however a breakpoint outside of either function is hit.

Im starting to believe its not specifically the RequestInterceptor itself since I have copied a few different guides to see if it would break inside the functions and they do not. https://www.avanderlee.com/swift/authentication-alamofire-request-adapter/ https://www.raywenderlich.com/11668143-alamofire-tutorial-for-ios-advanced-usage#toc-anchor-013 https://medium.com/swlh/retry-request-using-alamofire-52c7dcd72175

finally landed here and followed a few options there which have not helped the issue either. https://github.com/Alamofire/Alamofire/issues/2998

Here is my interceptor

import Alamofire

class APIRequestInterceptor: RequestInterceptor {
    
    typealias AdapterResult = Swift.Result<URLRequest, Error>
  
    let retryLimit = 5
    let retryDelay: TimeInterval = 10
  
//    func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Swift.Result<URLRequest, Error>) -> Void) {
    func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (AdapterResult) -> Void) {
//    func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) {
//    func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, AFError>) -> Void) {
        var urlRequest = urlRequest
        
        debugPrint("In the request interceptor...")
        debugPrint(urlRequest)
        
        if let token = Globals.sharedInstance.token {
            urlRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
        }
        completion(.success(urlRequest))
    }
  
    func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
        let response = request.task?.response as? HTTPURLResponse
        //Retry for 5xx status codes
        if
            let statusCode = response?.statusCode,
            (500...599).contains(statusCode),
            request.retryCount < retryLimit {
            completion(.retryWithDelay(retryDelay))
        } else {
            return completion(.doNotRetry)
        }
    }
}

and my client creating the session

static let sessionManager: Session = {
    let configuration = URLSessionConfiguration.af.default
    
    configuration.timeoutIntervalForRequest = 30
    configuration.waitsForConnectivity = true
    
    return Session(configuration: configuration, interceptor: APIRequestInterceptor(), eventMonitors: [APILogger()])
}()

and to be thorough, the requests are being made using sessionManager.request(convertible: URLRequestConvertible)

Looking at the request function, not sure why an interceptor is defined again in the request parameters? open func request(_ convertible: URLRequestConvertible, interceptor: RequestInterceptor? = nil) -> DataRequest {} nevertheless I also tried defining it again in the request params without any luck. sessionManager.request(route, interceptor: APIRequestInterceptor())

like image 434
DevinM Avatar asked May 24 '26 05:05

DevinM


1 Answers

It turns out that the issue was in fact disambiguation but I feel in a less than obvious way.

Looking at this issue for Alamofire https://github.com/Alamofire/Alamofire/issues/2998

disambiguating Result<URLRequest, Error> was the solution due to a custom Result type. It took some time to notice that I had a custom Error type...

My solution is to strongly disambiguate both function calls to prevent this in the future.

func adapt(_ urlRequest: URLRequest, for session: Alamofire.Session, completion: @escaping (Swift.Result<URLRequest, Swift.Error>) -> Void) {}

func retry(_ request: Alamofire.Request, for session: Alamofire.Session, dueTo error: Swift.Error, completion: @escaping (RetryResult) -> Void) {}
like image 200
DevinM Avatar answered May 26 '26 18:05

DevinM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!