-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew networking
65 lines (40 loc) · 2.25 KB
/
new networking
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
import Foundation
import Alamofire
class BaseApiClient {
static let `default` = BaseApiClient()
private init() {
}
func fetch<model:Codable>(request:APIRouter ,onSuccess: @escaping ([model]) -> Void) {
if Connectivity.isReachable {
(UIApplication.shared.delegate as! AppDelegate).addProgressView()
Alamofire.request(request).responseJSON { (response) in
switch response.result {
case .success( let apiResponse) :
DispatchQueue.main.async {
(UIApplication.shared.delegate as! AppDelegate).hideProgrssVoew()
}
if let responseData = apiResponse as? [String:Any] , let status = responseData["Code"] as? String , status == "SUCCESS" {
// onSuccess(responseData)
do {
let decoder = JSONDecoder()
let responseModel = try decoder.decode(ResponseBase<model>.self, from: response.data!)
onSuccess(responseModel.data!)
}
catch let error as NSError {
print("failed reason : \(error.localizedDescription)")
}
}
else {
UIApplication.shared.gettopMostViewController()?.presentAlerterror(title: "Erorr", message: "Service not Avilabel" ,okclick: nil)
}
case .failure(let error) :
UIApplication.shared.gettopMostViewController()?.presentAlerterror(title: "Erorr", message: error.localizedDescription, okclick: nil)
}
}
}
else {
(UIApplication.shared.delegate as! AppDelegate).hideProgrssVoew()
UIApplication.shared.gettopMostViewController()?.presentAlerterror(title: "Error", message: "connnection not avilabel", okclick: nil)
}
}
}