What is the best way working with API service?

I'm currently working on a project which works with API service. But I'm trying to use object oriented programming concept more successfully. I have login functions, purchase functions etc. First of all I will create a ApiService.swift class, and I will create a function called login in it. And I will call this function whenever login button clicked in LoginViewController. So I have some questions;

1-) How would I handle errors when I call login function in LoginViewController like network errors or wrong credentials?

2-) Is there any better way to use API Services with object oriented programming concept?

Let me explain more briefly;

I have a class Called ApiService

class ApiService{
let baseUrl = "http://pokeapi.co/api/v2/pokemon/1/"
static let sharedInstance = ApiService()
func downloadData(completion : @escaping (Error? ,[String: Any]?) -> ()){
    Alamofire.request(baseUrl).responseJSON(completionHandler: { (response) in

        if response.result.error != nil{
            completion(response.result.error, nil)
        }else{
            completion(nil, response.result.value as! [String: Any])
        }
        })
    }
}

And this is my viewDidLoad method ;

  override func viewDidLoad() {
    super.viewDidLoad()
    ApiService.sharedInstance.downloadData { (error, value) in
        print("this is error ", error)
        print("this is value ",value)
    }

}

Is the way that I'm using completion handler true?

2 answers