-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapiResult.swift
131 lines (102 loc) · 3.49 KB
/
apiResult.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// apiResult.swift
// Copyright © 2016 Analystik. All rights reserved.
import Foundation
class ApiResult {
func ApiGet(url: String) -> NSArray {
let newUrl = NSURL(string: url)!
let session = NSURLSession.sharedSession()
let semaphore = dispatch_semaphore_create(0)
var jsonArray = []
session.dataTaskWithURL(newUrl, completionHandler: { (data, response, error) -> Void in
do{
if NSString(data: data!, encoding: NSUTF8StringEncoding) != nil {
jsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [[String: AnyObject]]
}
} catch {
print("Serialization n'a pas fonctionné")
}
dispatch_semaphore_signal(semaphore)
}).resume()
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
return jsonArray
}
func ApiPut(profil: Profil) -> AnyObject {
let request = NSMutableURLRequest(URL: NSURL(string: "http://cars101.azurewebsites.net/api/calculate")!)
let semaphore = dispatch_semaphore_create(0)
let session = NSURLSession.sharedSession()
var jsonResponse: AnyObject = ""
var params = [:]
request.HTTPMethod = "PUT"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
params = [
"CarId" : profil.carId,
"KMPerYear" : profil.kmPerYear,
"ProvinceId" : profil.provinceId
] as Dictionary<String, AnyObject>
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: [])
} catch {
print("Serialization n'a pas fonctionné")
}
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("Pas de data: \(error)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
jsonResponse = json
} else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Erreur, ne parse pas JSON: \(jsonStr)")
}
} catch let parseError {
print(parseError)
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Erreur, ne parse pas JSON: '\(jsonStr)'")
}
dispatch_semaphore_signal(semaphore)
}
task.resume()
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
return jsonResponse
}
func getMakes() -> Array<Make> {
let connection = ApiGet("http://cars101.azurewebsites.net/api/makes")
var result: Array<Make> = []
for obj in connection {
result.append(Make(json: obj))
}
return result
}
func getModels(makeId: Int) -> Array<Model> {
let connection = ApiGet("http://cars101.azurewebsites.net/api/makes/" + String(makeId) + "/models")
var result: Array<Model> = []
for obj in connection {
result.append(Model(json: obj))
}
return result
}
func getCars(makeId: Int, modelId: Int) -> Array<Car> {
let connection = ApiGet("http://cars101.azurewebsites.net/api/makes/" + String(makeId) + "/models/" + String(modelId) + "/Cars")
var result: Array<Car> = []
for obj in connection {
result.append(Car(json: obj))
}
return result
}
func getProvinces() -> Array<Province> {
let connection = ApiGet("http://cars101.azurewebsites.net/api/provinces")
var result: Array<Province> = []
for obj in connection {
result.append(Province(json: obj))
}
return result
}
func calculate(profil: Profil) -> FinancialEvaluation {
let connection = ApiPut(profil)
let result = FinancialEvaluation(json: connection)
return result
}
}