TryingToMakeItWork
TryingToMakeItWork

Reputation: 191

Alamofire request chaining - Cannot call value of non-function type 'HTTPURLResponse'

A new Swift guy here. I'm trying to figure out how to chain multiple Alamofire calls together.

I need to

  1. get an auth token from Server 1
  2. get some data from Server 1 (need the auth token)
  3. get an auth token from Server 2
  4. Get more data from Server 2 based on the values from step 2.

I've tried following the examples on this post: Chain multiple Alamofire requests

Unfortunately none of those examples are working with Swift 4.

I've decided to pursue Option 2, but keep getting a

Cannot call value of non-function type 'HTTPURLResponse?'

error both on the putRequest and getRequest lines. I have no idea what that means or how to fix it.

My current code:

import UIKit
import PromiseKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {

    let URL = "http://httpbin.org/"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func runPutRequest() {

        let putRequest = Alamofire.request("\(URL)/get")

        putRequest.response { [weak self] putRequest, putResponse, putData, putError in
            if let strongSelf = self {
                // Probably store some data
                strongSelf.runGetRequest()
            }
        }
    }

    func runGetRequest() {

        let getRequest = Alamofire.request("\(URL)/get", method: .get)

        getRequest.response { [weak self] getRequest, getResponse, getData, getError in
            if let strongSelf = self {
                // Probably store more data
                strongSelf.processResponse()
            }
        }
    }

    func processResponse() {
        // Process that data
    }

    func reloadData() {
        // Reload that data
    }
}

Any help would be greatly appreciated.

Upvotes: 0

Views: 96

Answers (1)

glyvox
glyvox

Reputation: 58029

You have too many return arguments for the response closures, you actually just need one DataResponse argument. This code is working for me:

func runPutRequest() {
    let putRequest = Alamofire.request("\(URL)/get", method: .put)
    putRequest.response { [weak self] response in
        if let strongSelf = self {
            // Probably store some data
            strongSelf.runGetRequest()
        }
    }
}

func runGetRequest() {
    let getRequest = Alamofire.request("\(URL)/get", method: .get)
    getRequest.response { [weak self] response in
        if let strongSelf = self {
            // Probably store more data
            strongSelf.processResponse()
        }
    }
}

Upvotes: 1

Related Questions