Anushka Madushan
Anushka Madushan

Reputation: 681

How to return the result of alamofire Http request from a method?

I'm using alamofire and swift 3. I want to return the data which i get from Http request. Currently im getting that using completion handler.

func SendHttpRequest(params: AnyObject, header_obj: [String: String], url:String, http_method:HTTPMethod, completion: @escaping (_ response_value: JSON, _ error_type: String)->()) {

        Alamofire.request(BASE_URL + url, method: http_method, parameters: (params as! [String : Any]) , encoding: URLEncoding.default, headers: header_obj).responseJSON { (response:DataResponse<Any>) in

            switch(response.result) {
            case .success( _ ):

                if let jsonValue = response.result.value {
                    let json = JSON(jsonValue)
                    completion(json, "")
                }
                break

            case .failure(_):
                print(response.result.error!)
                completion(JSON(response.result.value!), "NO_INT")
                //"The Internet connection appears to be offline."
                break

            }
        }//Alamofire

    }//SendHttpRequest

But what i want is to return whatever the result i'm getting through Http request. Eg:-

func GetUsers() -> [Users]{
   //HERE IT SHOULD EXCUTE THE HTTP REQUEST AND RETURN THE USERS LIST
}

How can i achieve it? Can i achieve it like method pass by as parameter theory?

Upvotes: 0

Views: 684

Answers (1)

Vollan
Vollan

Reputation: 1915

I'm not entierly sure how your json is structured or how your User struct / class looks like, but its something similar like this you should do. If i understood correctly what you wanted to do.

 class NetworkingStuff {
        func SendHttpRequest(params: AnyObject, header_obj: [String: String], url:String, http_method:HTTPMethod, success:  @escaping (Any) -> (), failure: @escaping (error?) -> ()) {

                Alamofire.request(BASE_URL + url, method: http_method, parameters: (params as! [String : Any]) , encoding: URLEncoding.default, headers: header_obj).responseJSON { (response:DataResponse<Any>) in

                    switch(response.result) {
                    case .success( _ ):

                        if let jsonValue = response.result.value {
                            let json = JSON(jsonValue)
                            success(json, "")
                        }
                        break

                    case .failure(_):
                        print(response.result.error!)
                        failure(JSON(response.result.value!), "NO_INT")
                        //"The Internet connection appears to be offline."
                        break

                    }
                }
            }
        }

Handle data:

  func GetUsers() -> [Users]? {
       networkingStuff.sendHttpRequest(params: something, header_obj: dict, url: URL, http_method: Alamofire.post, success: { (responseData: Any) -> () in
              if let data = responseData as? [String: Any], let users = data["users"] as? [String: Any] {
                  if let userArray = Users(dict: users) {
                      return userArray
                  } else {
                      return nil
                  }
              }

        }, failure: { (error: Any?) -> () in
            return nil
        })
    }

Upvotes: 1

Related Questions