Skip to content

Instantly share code, notes, and snippets.

@fozoglu
Created May 2, 2018 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fozoglu/8dc22f51b1cc850abf71fe4039bd8d31 to your computer and use it in GitHub Desktop.
Save fozoglu/8dc22f51b1cc850abf71fe4039bd8d31 to your computer and use it in GitHub Desktop.
API Call Manager file with alamofire and swiftyJSON
import Alamofire
import SwiftyJSON
import Foundation
let API_BASE_URL = "http://api.fozoglu.com"
class APICallManager {
static let instance = APICallManager()
enum RequestMethod {
case get
case post
}
enum Endpoint: String {
case People = "/people.json"
}
// MARK: Contact
func callAPIGetPeople(
onSuccess successCallback: ((_ people: [PeopleModel]) -> Void)?,
onFailure failureCallback: ((_ errorMessage: String) -> Void)?) {
// Build URL
let url = API_BASE_URL + Endpoint.People.rawValue
// call API
self.createRequest(
url, method: .get, headers: nil, parameters: nil,
onSuccess: {(responseObject: JSON) -> Void in
// Create dictionary
if let responseDict = responseObject["data"].arrayObject {
let peopleDict = responseDict as! [[String:AnyObject]]
// Create object
var data = [PeopleModel]()
for item in peopleDict {
let single = PeopleModel.build(item)
data.append(single)
}
// Fire callback
successCallback?(data)
} else {
failureCallback?("An error has occured.")
}
},
onFailure: {(errorMessage: String) -> Void in
failureCallback?(errorMessage)
}
)
}
// MARK: Request Handler
// Create request
func createRequest(
_ url: String,
method: HTTPMethod,
headers: [String: String]?,
parameters: AnyObject?,
onSuccess successCallback: ((JSON) -> Void)?,
onFailure failureCallback: ((String) -> Void)?) {
Alamofire.request(url, method: method).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
successCallback?(json)
case .failure(let error):
if let callback = failureCallback {
// Return
callback(error.localizedDescription)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment