Skip to main content

API Calling Example


import
 Foundation
import Alamofire
class HomeViewModel {
    
    let sharedInstance = Connection()
    var reloadHandler: DataHandler = { }
    typealias DataHandler = () -> Void
    init() { }
    
    //--------------------------------------------------
    // MarK : fetchInitialSetup  API Integration
    //--------------------------------------------------
    func fetchInitialSetup(success:@escaping (GetLocationAndDevicesModel) -> Void, failure:@escaping (Error) -> Void)
    {
        let URL = AddDeviceList().getUrlString(url: .CREATE_API)
        
        let headers : HTTPHeaders = [
            "Content-Type":"application/json",
            "x-api-key":"X8gx8eTF8i2l0lCAneLJ44vHosJ2M9ct4EPsNRcX"
        ]
        
        var params = Parameters()

        params = [
            "type""initialSetup",
            "userId": UserDefaults.standard.getEmailID() 
        ]
        

        sharedInstance.requestPOST(URL, params: params, headers: headers, success: { (JSON) in
            
            let  result :Data? = JSON
            
            if result != nil
            {
                do
                {
                    let response = try JSONDecoder().decode(GetLocationAndDevicesModel.self, from: result!)
                    success(response)
                    
                }
                catch let error as NSError
                {
                    failure(error)
                }
            }
            
        }) { (Error) in
            failure(Error)

        }
    }
    }

Comments

Popular posts from this blog

Common API Request Class

// //    Connection.swift //    Norwood // //    Created by Praveen Reddy on 10/01/19. //    Copyright © 2019 Apple. All rights reserved. // import  Foundation import  Alamofire import  SwiftyJSON class  Connection {           func  requestPOST( _  url:  String , params :  Parameters ?, headers :  HTTPHeaders ?, success: @escaping  ( Data ) ->  Void , failure: @escaping  ( Error ) ->  Void )      {                   print( "URL = " ,url)          print( "Parameter = " ,params!)                   //          if Connectivity.isConnectedToInternet() //          {              i...