-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.swift
84 lines (65 loc) · 3.51 KB
/
API.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import UIKit
class API {
private static func dataTask(path: String, params: Dictionary<String, AnyObject>? = nil, method: String, completion: (success: Bool, object: JSON?) -> ()) {
let request = requestForPathWithParams(path, params: params)
request.HTTPMethod = method
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let data = data {
let json = JSON(data: data)
if let response = response as? NSHTTPURLResponse where 200...299 ~= response.statusCode {
completion(success: true, object: json)
} else {
completion(success: false, object: json)
}
}
}.resume()
}
private static func post(path: String, params: Dictionary<String, AnyObject>? = nil, completion: (success: Bool, object: JSON?) -> ()) {
dataTask(path, params: params, method: "POST", completion: completion)
}
private static func put(path: String, params: Dictionary<String, AnyObject>? = nil, completion: (success: Bool, object: JSON?) -> ()) {
dataTask(path, params: params, method: "PUT", completion: completion)
}
private static func get(path: String, params: Dictionary<String, AnyObject>? = nil, completion: (success: Bool, object: JSON?) -> ()) {
dataTask(path, params: params, method: "GET", completion: completion)
}
private static func delete(path: String, params: Dictionary<String, AnyObject>? = nil, completion: (success: Bool, object: JSON?) -> ()) {
dataTask(path, params: params, method: "DELETE", completion: completion)
}
private static func requestForPathWithParams(path: String, params: Dictionary<String, AnyObject>? = nil) -> NSMutableURLRequest {
let request = NSMutableURLRequest(URL: NSURL(string: AppDelegate.apiServer + path)!)
if let params = params {
var paramString = ""
for (key, value) in params {
let escapedKey = key.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let escapedValue = value.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
paramString += "\(escapedKey)=\(escapedValue)&"
}
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
}
return request
}
private static func stringToJSON(string: String) -> Array<AnyObject> {
var json: Array<AnyObject>!
do {
json = try NSJSONSerialization.JSONObjectWithData(string.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions()) as? Array
} catch {
print(error)
}
return json
}
static func test(completion: (success: Bool, message: JSON) -> ()) {
get("users") { (success, json) -> () in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print(json)
if let json = json {
completion(success: success, message: json)
} else {
completion(success: success, message: nil)
}
})
}
}
}