-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerRequest.swift
59 lines (46 loc) · 1.87 KB
/
ServerRequest.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
//
// ServerRequest.swift
// Created by Yahia El-Dow on 6/24/17.
// Copyright © 2017 Production Code. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
class AF : NSObject {
static let httpMethod_GET = HTTPMethod.get
static let httpMethod_POST = HTTPMethod.post
static let httpMethod_PUT = HTTPMethod.put
static let httpMethod_DELETE = HTTPMethod.delete
static let urlEncoding = URLEncoding.default
static let headers = ["Content-Type": "application/x-www-form-urlencoded" ,
"Cache-Control": "no-cache" ,
"Accept-Language" : "en"
]
class func request (request_url : String ,
httpMethod : HTTPMethod = .get ,
paramter : [String : AnyObject]? = nil ,
Result:@escaping(_ result : Any? ,_ errorCode : Any? , _ errorMessage : Any?)->()) {
print(request_url)
Alamofire.request( request_url ,
method: httpMethod ,
parameters: paramter ,
encoding: urlEncoding ,
headers: headers )
.validate(statusCode: 200..<400)
.responseJSON(completionHandler: {
response in
let statusCode = response.response?.statusCode
switch response.result
{
case .failure(let error) :
print("> Error on Response JSON " , error.localizedDescription)
let errorMessage = "\(error.localizedDescription)"
Result(nil , statusCode , errorMessage)
return
case .success(let requestResult):
Result(requestResult, statusCode , nil)
return
}
})
}
}