This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static func request(urlRequest: URLRequest, completion: (DataResponse) -> (Void), failure: (Data) -> (Void)) { | |
let request = Alamofire.request(urlRequest) | |
.validate() | |
request.validate().responseJSON {response in | |
if (200..<300) ~= response.response?.statusCode { | |
completion(response.result) | |
} else { | |
failure(response.data) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func requestWith(url: URL, method: HTTPMethod, parameters: Parameters? = nil, headers: HTTPHeaders? = nil, completion: (DataResponse) -> (Void), failure: (Data) -> (Void)) | |
let request = Alamofire.request(url, | |
method: method, | |
parameters: parameters, | |
headers : headers) | |
request.validate().responseJSON { | |
response in | |
if (200..<300) ~= response.response?.statusCode { | |
completion(response.result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getURLRequest(method: HTTPMethod, body: Encodable? = nil, params: [String: Any]? = nil) throws -> URLRequest { | |
do { | |
var queryParams = [String: Any]() | |
if let myParams = params { | |
queryParams = myParams | |
} | |
queryParams[Constants.token] = AppCache.token | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Encodable { | |
let id: Int | |
let name: Encodable | |
enum CodingKeys: String, CodingKey { | |
case id | |
case name | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Encodable { | |
func asJson() -> [String : Any]? { | |
do { | |
let data = try JSONEncoder().encode(self) | |
guard let dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String : AnyObject] else { | |
return nil | |
} | |
return dictionary | |
} catch { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Encodable { | |
let id: Int | |
let name: Encodable | |
let address: Address | |
enum CodingKeys: String, CodingKey { | |
case id | |
case name | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Encodable { | |
let id: Int | |
let name: Encodable | |
let address: Address | |
enum CodingKeys: String, CodingKey { | |
case id | |
case name | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getURLRequest(method: HTTPMethod, body: Encodable? = nil, params: [String: Any]? = nil) throws -> URLRequest { | |
do { | |
var queryParams = [String: Any]() | |
if let myParams = params { | |
queryParams = myParams | |
} | |
let myURL = try urlString.asURL() | |
var urlRequest = URLRequest(url: myURL) |