Skip to content

Instantly share code, notes, and snippets.

@RadwaEbrahim
Last active August 29, 2015 14:26
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 RadwaEbrahim/60856c3d6d0a89459baf to your computer and use it in GitHub Desktop.
Save RadwaEbrahim/60856c3d6d0a89459baf to your computer and use it in GitHub Desktop.
//
// APIClient.swift
//
//
// Created by Radwa Muhammad on 6/11/15.
// Copyright (c) 2015 Radwa Muhammad. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
enum KeyFields: String {
/*
// list all the API keys that you will use to parse the JSON data
//ex.
case kLanguage = "local"
case kArabic = "ar"
case kEnglish = "eng"
case kErrorMessage = "Message"
*/
}
enum MethodPath: String {
/*
// List all the relative urls (web service method pathes)
//ex.
case notificationsPath = "item/notifications?"
case newsPath = "item/news"
case contactinfoPath = "index/contactinfo"
*/
}
// protocol APIProtocol {
// func didReceiveResult(results: JSON)
// }
class APIClient {
let hostName = "http://xxxx.xxx/" //base url
func get(path: MethodPath, parameters: [String: AnyObject]? = nil, completionHandler: (JSON?, NSError?, String?) -> Void)-> Void {
let url = "\(self.hostName)\(path.rawValue)"
// NSLog("Preparing for GET request to: \(url)")
Alamofire.request(.GET, url, parameters: parameters)
.validate(statusCode: 200..<300)
.responseJSON { (request, response, data, error) in
if(error != nil) { //is there's error
NSLog("GET Error: \(error)")
println(response)
completionHandler(nil,error,nil)
}
else if let data: AnyObject = data {
var json = JSON(data) as JSON
NSLog("GET Result: \(json)")
if json[0][KeyFields.kErrorMessage.rawValue] != nil { //is there error message
completionHandler(nil,nil,json[KeyFields.kErrorMessage.rawValue].stringValue)
}
else{ //else then everything is okay
completionHandler(json,nil,nil)
}
}
else{ // JSON response is empty
completionHandler(nil,nil,nil)
}
}
}
func post(path: MethodPath, parameters: [String: AnyObject]? = nil, completionHandler: (JSON?, NSError?, String?) -> Void)-> Void {
let url = "\(self.hostName)\(path.rawValue)"
// NSLog("Preparing for POST request to: \(url)")
Alamofire.request(.POST, url, parameters: parameters)
.validate(statusCode: 200..<300)
.responseJSON { (request, response, data, error) in
if(error != nil) { //is there error
NSLog("GET Error: \(error)")
println(response)
completionHandler(nil,error,nil)
}
else if let data: AnyObject = data {
var json = JSON(data) as JSON
// NSLog("GET Result: \(json)")
if json[0][KeyFields.kErrorMessage.rawValue][KeyFields.kMessage.rawValue].string != "DONE" { //is there error message
completionHandler(nil,nil,json[KeyFields.kErrorMessage.rawValue][KeyFields.kMessage.rawValue].stringValue)
}
else{ //else then everything is okay
completionHandler(json,nil,nil)
}
}
else{ // JSON response is empty
completionHandler(nil,nil,nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment