2

I am currently in the process of updating my codebase to Swift 3.0, and I am using Alamofire.
Thus, I had to update Alamofire to 4.0 (Alamofire git repo).
I have a request method to fetch data from the server, and before migration, it worked wonderfully.
After using Xcode's migration tool, I ended up with this error : "Extra argument in Call".
I'm not quite sure why this method is no longer working.
Any help would be wonderful!

class func makeRequest(
    _ method: RequestMethod,
    authorization: String?,
    uri: String,
    params: JSONDictionary?,
    retry: Bool = true,
    completionHandler: @escaping RequestCompletionBlock)
{

    let requestURL = baseURL + uri
    let authHeader: Dictionary<String, String>? = authorization != nil ? ["Authorization" : authorization!] : nil

    //requestURL is highlighted, says "Extra argument in call"
    sharedAlamofireManager.request(Method(rawValue: method.rawValue)!, requestURL, parameters: params, encoding: .JSON, headers: authHeader).responseJSON {
        response in

    }
}

Migration Guide for Alamofire4.0

Edit: JSONDictionary:

typealias JSONDictionary = Dictionary<String, Any>

sharedAlamoFireManager is also a SessionManager

Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98

1 Answers1

2

Per AlamoFire docs The RequestAdapter protocol is a completely new feature in Alamofire 4. It allows each Request made on a SessionManager to be inspected and adapted before being created. One very specific way to use an adapter is to append an Authorization header to requests behind a certain type of authentication.

The header is no longer a parameter of the request method, but instead set inside the request adapter.

First I had to create the AccessTokenAdapter class:

class AccessTokenAdapter: RequestAdapter {
    private let accessToken: String

    init(accessToken: String) {
        self.accessToken = accessToken
    }

    func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
        var urlRequest = urlRequest

        if urlRequest.urlString.hasPrefix(RequestManager.returnBaseURL()) {
            urlRequest.setValue("Bearer " + accessToken, forHTTPHeaderField: "Authorization")
        }

        return urlRequest
    }
}

Then I needed to update my method:

class func makeRequest(
    _ method: RequestMethod,
    authorization: String?,
    uri: String,
    params: JSONDictionary?,
    retry: Bool = true,
    completionHandler: @escaping RequestCompletionBlock)
{

    let requestURL = baseURL + uri
    var accessToken: String?
    if let authorization = authorization {
        accessToken = authorization
    }else{
        accessToken = self.token()!
    }

    sharedAlamofireManager.adapter = AccessTokenAdapter(accessToken: accessToken!)

    sharedAlamofireManager.request(requestURL, method: .get, parameters: params, encoding: JSONEncoding.default)
        .responseJSON { response in

    }
}
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
  • Hello, I am trying to follow the documentation and getting a compile error: https://stackoverflow.com/questions/44687745/incorrect-argument-label-in-call-wtih-alamofire-session-manager-download – quantumpotato Jun 22 '17 at 01:23