Alamofire response is nil - swift

I am facing an issue with Alamofire requests and interceptor.
I have two types of responses from my server -> Success response which is kind of object and the error response which is always a message what's wrong.
I founded a sample code to create a very own function for decoding two types of responses which seems to work well.
The problem occurs after the Alamofire adapt function is called (adapt add a cookies for each request) -> in my .responseTwoDecodable i'll get always response as nil but the status code is 200 and everything is fine, server returns object but alamofire is ignoring it.
Here is my code for each request:
func request<T: Decodable>(
_ url: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
decoder: JSONDecoder = JSONDecoder(),
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil
) -> Future<T, ServerError> {
return Future({ promise in
AF.request(
url,
method: method,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers
interceptor: interceptor ?? self
)
.validate(statusCode: [200, 201, 204, 401]
.responseTwoDecodable(of: T.self) { response in
switch response {
case .success(let value):
promise(.success(value))
case .failure(let error):
promise(.failure(error))
}
}
})
}
Here is adapt function:
func adapt(_ urlRequest: URLRequest, for session: Session, completion: #escaping (Result<URLRequest, ServerError>) -> Void) {
let request = urlRequest
if let accessToken = UserDefaults.standard.string(forKey: Constants.accessToken), let refreshToken = UserDefaults.standard.string(forKey: Constants.refreshToken) {
let access = [
HTTPCookiePropertyKey.domain: Constants.cookieDomain,
HTTPCookiePropertyKey.path: Constants.cookieAccessPath,
HTTPCookiePropertyKey.name: Constants.accessToken,
HTTPCookiePropertyKey.value: accessToken
]
let refresh = [
HTTPCookiePropertyKey.domain: Constants.cookieDomain,
HTTPCookiePropertyKey.path: Constants.cookieRefreshPath,
HTTPCookiePropertyKey.name: Constants.refreshToken,
HTTPCookiePropertyKey.value: refreshToken
]
if let accessCookie = HTTPCookie(properties: access), let refreshCookie = HTTPCookie(properties: refresh) {
AF.session.configuration.httpCookieStorage?.setCookie(accessCookie)
AF.session.configuration.httpCookieStorage?.setCookie(refreshCookie)
completion(.success(request))
}
}
completion(.success(request))
}
And here is my decoding code for two decodables:
struct ErrorMessage: Error, Decodable {
let message: String
}
struct ServerError: Error {
var message: String
var code: ServerErrorCodes
let args: [String]?
}
enum ServerErrorCodes: Int {
case unauthorized = 401
case forbidden = 403
case internalServerError = 500
case notFound = 404
case conflict = 409
case unknown
case emptyResponse
case unserialized
case userInput
case coreData
}
final class TwoDecodableResponseSerializer<T: Decodable>: ResponseSerializer {
lazy var decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
private lazy var successSerializer = DecodableResponseSerializer<T>(decoder: decoder)
private lazy var errorSerializer = DecodableResponseSerializer<ErrorMessage>(decoder: decoder)
public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Result<T, ServerError> {
guard error == nil else { return .failure(ServerError(message: "Unknown error", code: .unknown, args: [])) }
guard let response = response else { return .failure(ServerError(message: "Empty response", code: .emptyResponse, args: [])) } // HERE I AM GETTING A NIL AS A RESPONSE, BUT SERVER RESPONDED WITH CORRECT BODY AND 200 AS STATUS CODE
do {
print(response.debugDescription)
if response.statusCode < 200 || response.statusCode >= 300 {
let serverMessage = try errorSerializer.serialize(request: request, response: response, data: data, error: nil)
let responseCode: ServerErrorCodes
if let code = ServerErrorCodes(rawValue: response.statusCode) {
responseCode = code
} else {
responseCode = .unknown
}
let result = ServerError(message: serverMessage.message, code: responseCode, args: nil)
return .failure(result)
} else {
let result = try successSerializer.serialize(request: request, response: response, data: data, error: nil)
return .success(result)
}
} catch(let err) {
return .failure(ServerError(message: "Could not serialize body", code: .unserialized, args: [String(data: data!, encoding: .utf8)!, err.localizedDescription]))
}
}
}
extension DataRequest {
#discardableResult func responseTwoDecodable<T: Decodable>(queue: DispatchQueue = DispatchQueue.global(qos: .userInitiated), of t: T.Type, completionHandler: #escaping (Result<T, ServerError>) -> Void) -> Self {
return response(queue: .main, responseSerializer: TwoDecodableResponseSerializer<T>()) { response in
switch response.result {
case .success(let result):
completionHandler(result)
case .failure(let error):
completionHandler(.failure(ServerError(message: "Other error", code: .unknown, args: [error.localizedDescription])))
}
}
}
}
I am a newbie in Alamofire, so if something can be done in better way, I will appreciate your help or sharing your thoughts! Has anybody idea why this can happen? Thanks!

SOLVED: I completely wipe out the code implementing .responseTwoDecodable and write very own solution:
func createError(response: HTTPURLResponse?, error: AFError, data: Data?) -> ServerError {
guard let serverResponse = response else {
return .init(message: "Unknown error", code: .unknown, args: [error.localizedDescription])
}
guard let serverData = data else {
return .init(message: "Empty response", code: .emptyResponse, args: [error.localizedDescription])
}
let responseCode: ServerErrorCodes
if let code = ServerErrorCodes(rawValue: serverResponse.statusCode) {
responseCode = code
} else {
responseCode = .unknown
}
do {
let serverMessage = try JSONDecoder().decode(ErrorMessage.self, from: serverData)
return .init(message: serverMessage.message, code: responseCode, args: [])
} catch (let error) {
return .init(message: "Body not serializable", code: .unserialized, args: [String(data: serverData, encoding: .utf8) ?? "", error.localizedDescription])
}
}
And calling it like:
.responseDecodable(completionHandler: { (response: DataResponse<T, AFError>) in
switch response.result {
case .success(let value):
promise(.success(value))
case .failure(let error):
promise(.failure(self.createError(response: response.response, error: error, data: response.data)))
}
})
With this code I was able to decode all the errors from the server as expected.

Related

how to use alamofire httpstatusCode

I want a diffrerent responseDecodable on the httpStatusCode
server return
if statusCode == 200
resonseBody
{id: number}
if statusCode 400..<500
resonseBody
{
code: String
timestamp: String
message: String
}
so now my code is
AF.request(url, method: .post, headers: header).responseData { response in
switch response.result {
case .success(let data) :
guard let response = response.response else {return}
let json = try? JSONSerialization.jsonObject(with: data)
switch response.statusCode {
case 200:
if let json = json as? [String: Any] , let message = json["id"] as? Int{print(message)}
case (400..<500):
if let json = json as? [String: Any] , let message = json["message"] as? String{print(message)}
default:
return
}
case .failure(let err) :
print(err)
}
}
I try this code convert responseDecodable
struct a: Codable {var id: Int}
struct b: Codable{
var code: String
var timestamp: String
var message: String
}
AF.request(url, method: .post, headers: header).responseDecodable(of: a.self) { response in
guard let data = response.value else {return}
print(data)
}
.responseDecodable(of: b.self) { response in
guard let data = response.value else {return}
print(data)
}
but this way Regardless statusCode return both a and b
I want
stautsCode == 200 return a or
statusCode 400..<500 return b
What should I Do?
AFAIK, Alamofire does not have a “decode one object for success and another for failure” implementation. You'll have to do this yourself.
If you really want a one distinct object for 2xx responses and another for 4xx responses, there are a few approaches:
Use validate to handle the 2xx responses, and manually decode 4xx responses in the error handler.
AF.request(url, method: .post, parameters: parameters, headers: header)
.validate(statusCode: 200 ..< 300) // only 2xx are auto-decoded for us; handle 4xx responses in `failure` handler
.responseDecodable(of: Foo.self) { response in
switch response.result {
case .failure(let error):
guard
let statusCode = response.response?.statusCode,
400 ..< 500 ~= statusCode,
let data = response.data,
let apiError = try? JSONDecoder().decode(ApiErrorResponse.self, from: data)
else {
print("other error:", error) // didn't parse `ApiErrorResponse` object, so obviously some other error
return
}
print("apiError:", apiError) // this is our parsed API error object
case .success(let foo):
print("success:", foo)
}
}
If you want, you could write your own ResponseSerializer to parse 2xx and 4xx responses differently:
struct ApiErrorResponse: Decodable, Error {
let code: String
let timestamp: String
let message: String
}
final class ApiResponseSerializer<T: Decodable>: ResponseSerializer {
lazy var decoder = JSONDecoder()
private lazy var successSerializer = DecodableResponseSerializer<T>(decoder: decoder)
private lazy var errorSerializer = DecodableResponseSerializer<ApiErrorResponse>(decoder: decoder)
public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> T {
if let error = error { throw error }
guard let response = response else { throw URLError(.badServerResponse) }
switch response.statusCode {
case 400 ..< 500:
let apiErrorObject = try errorSerializer.serialize(request: request, response: response, data: data, error: nil)
throw apiErrorObject
default:
return try successSerializer.serialize(request: request, response: response, data: data, error: nil)
}
}
}
And then you can do:
AF.request(url, method: .post, parameters: parameters, headers: header)
.response(responseSerializer: ApiResponseSerializer<Foo>()) { response in
switch response.result {
case .failure(.responseSerializationFailed(reason: .customSerializationFailed(error: let apiErrorObject))):
print("api response error:", apiErrorObject)
case .failure(let error):
print("some other error:", error)
case .success(let foo):
print("success:", foo)
}
}
I find the nesting of the parsed error object to be a little tedious, but it does abstract the 2xx vs 4xx logic out of the response/responseDecoder completion handler.
In both of these, I'm considering all 2xx responses as success, not just 200. Some servers return 2xx codes other than just 200.

Alamofire - How to get API error from AFError

In my quest to implement Alamofire 5 correctly and handle custom error model responses, I have yet to find an accepted answer that has an example.
To be as thorough as possible, here is my apiclient
class APIClient {
static let sessionManager: Session = {
let configuration = URLSessionConfiguration.af.default
configuration.timeoutIntervalForRequest = 30
configuration.waitsForConnectivity = true
return Session(configuration: configuration, eventMonitors: [APILogger()])
}()
#discardableResult
private static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:#escaping (Result<T, AFError>)->Void) -> DataRequest {
return sessionManager.request(route)
// .validate(statusCode: 200..<300) // This will kill the server side error response...
.responseDecodable (decoder: decoder){ (response: DataResponse<T, AFError>) in
completion(response.result)
}
}
static func login(username: String, password: String, completion:#escaping (Result<User, AFError>)->Void) {
performRequest(route: APIRouter.login(username: username, password: password), completion: completion)
}
}
I am using it like this
APIClient.login(username: "", password: "") { result in
debugPrint(result)
switch result {
case .success(let user):
debugPrint("__________SUCCESS__________")
case .failure(let error):
debugPrint("__________FAILURE__________")
debugPrint(error.localizedDescription)
}
}
I have noticed that if I use .validate() that the calling function will receive a failure however the response data is missing. Looking around it was noted here and here to cast underlyingError but thats nil.
The server responds with a parsable error model that I need at the calling function level. It would be far more pleasant to deserialize the JSON at the apiclient level and return it back to the calling function as a failure.
{
"errorObject": {
"summary": "",
"details": [{
...
}]
}
}
UPDATE
Thanks to #GIJoeCodes comment I implemented this similar solution using the router.
class APIClient {
static let sessionManager: Session = {
let configuration = URLSessionConfiguration.af.default
configuration.timeoutIntervalForRequest = 30
configuration.waitsForConnectivity = true
return Session(configuration: configuration, eventMonitors: [APILogger()])
}()
#discardableResult
private static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:#escaping (_ response: T?, _ error: Error?)->Void) {
sessionManager.request(route)
.validate(statusCode: 200..<300) // This will kill the server side error response...
.validate(contentType: ["application/json"])
.responseJSON { response in
guard let data = response.data else { return }
do {
switch response.result {
case .success:
let object = try decoder.decode(T.self, from: data)
completion(object, nil)
case .failure:
let error = try decoder.decode(ErrorWrapper.self, from: data)
completion(nil, error.error)
}
} catch {
debugPrint(error)
}
}
}
// MARK: - Authentication
static func login(username: String, password: String, completion:#escaping (_ response: User?, _ error: Error?)->Void) {
performRequest(route: APIRouter.login(username: username, password: password), completion: completion)
}
}
Called like this
APIClient.login(username: "", password: "") { (user, error) in
if let error = error {
debugPrint("__________FAILURE__________")
debugPrint(error)
return
}
if let user = user {
debugPrint("__________SUCCESS__________")
debugPrint(user)
}
}
This is how I get the errors and customize my error messages. In the validation, I get the errors outside of the 200..<300 response:
AF.request(
url,
method: .post,
parameters: json,
encoder: JSONParameterEncoder.prettyPrinted,
headers: headers
).validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch response.result {
case .success(let result):
let json = JSON(result)
onSuccess()
case .failure(let error):
guard let data = response.data else { return }
do {
let json = try JSON(data: data)
let message = json["message"]
onError(message.rawValue as! String)
} catch {
print(error)
}
onError(error.localizedDescription)
}
debugPrint(response)
}
First, there's no need to use responseJSON if you already have a Decodable model. You're doing unnecessary work by decoding the response data multiple times. Use responseDecodable and provide your Decodable type, in this case your generic T. responseDecodable(of: T).
Second, wrapping your expected Decodable types in an enum is a typical approach to solving this problem. For instance:
enum APIResponse<T: Decodable> {
case success(T)
case failure(APIError)
}
Then implement APIResponse's Decodable to try to parse either the successful type or APIError (there are a lot of examples of this). You can then parse your response using responseDecodable(of: APIResponse<T>.self).

Swift Combine Memory Leak from CFNetwork

I am trying to plug a memory leak. I have the following class that fetches API requests:
public struct Service {
public let baseURL: URL
public let session: URLSession
public init (baseURL: URL, session: URLSession) {
self.baseURL = baseURL
self.session = session
}
public struct Response {
public let data: Data
public let response: URLResponse
}
public enum ServiceError: Error {
case api(title: String, messages: [String])
case other(Error)
}
struct ServiceErrorResponse: Decodable {
let response: ErrorResponse
enum CodingKeys: String, CodingKey {
case response = "error"
}
}
struct ErrorResponse: Decodable {
let title: String
let messages: [String]
}
public enum HTTPMethod: String {
case get = "GET"
case put = "PUT"
case post = "POST"
case patch = "PATCH"
case delete = "DELETE"
}
public func run(_ request: URLRequest) -> AnyPublisher<Response, ServiceError> {
return session
.dataTaskPublisher(for: request)
.tryMap { data, response in
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
let error = try JSONDecoder().decode(ServiceErrorResponse.self, from: data)
let title = error.response.title
let messages = error.response.messages
print(error.response)
throw ServiceError.api(title: title, messages: messages)
}
return Response(data: data, response: response)
}
.mapError { err in
let error = err is ServiceError ? err : ServiceError.other(err)
return error as! Service.ServiceError
}
.eraseToAnyPublisher()
}
public func fetch(
_ path: String,
method: HTTPMethod = .get,
params: Data? = nil
) -> AnyPublisher<Response, ServiceError> {
let url: URL
if let params = params, method == .get {
url = buildGetURLWithParams(path: path, params: params)!
}
else {
url = baseURL.appendingPathComponent(path)
}
var request = URLRequest(url: url)
request.httpMethod = method.rawValue
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
if let params = params, method != .get {
request.httpBody = params
}
return run(request)
}
private func buildGetURLWithParams(path: String, params: Data) -> URL? {
if let json = try? JSONSerialization.jsonObject(with: params, options: []) as? [String: String] {
var urlComponents = URLComponents(
url: baseURL.appendingPathComponent(path),
resolvingAgainstBaseURL: false
)
urlComponents?.queryItems = json.map { URLQueryItem(name: $0, value: $1) }
return urlComponents?.url
}
else { return nil }
}
}
I then make requests from the app using the following:
typealias ServiceResponse = Service.Response
typealias ServiceError = Service.ServiceError
typealias ServiceMethod = Service.HTTPMethod
enum MyAPI {
static let service = Service(
baseURL: URL(string: "http://127.0.0.1:3000/api")!,
session: URLSession(configuration: URLSessionConfiguration.default)
)
static func login(email: String, password: String) -> AnyPublisher<ServiceResponse, ServiceError> {
let params = ["email": email, "password": password]
let json = try! JSONEncoder().encode(params)
return service.fetch("/login", method: .post, params: json)
}
}
The login function fetches a response and returns an AnyPublisher which is consumed as follows:
enum UserAction {
case login
case loginSuccess(UserResponse)
case loginFailure
case logout
static func login(email: String, password: String) -> Dispatch<AppAction> {
return { dispatch in
dispatch(.userAction(action: .login))
return MyAPI.login(email: email, password: password)
.map(\.data)
.decode(type: UserResponse.self, decoder: JSONDecoder())
.receive(on: DispatchQueue.main)
.sink(
receiveCompletion: { completion in
if case .failure(let err) = completion {
print("--------------------------")
print("Retrieving data failed with error \(err)")
}
},
receiveValue: { result in
dispatch(.userAction(action: .loginSuccess(result))) // Here I have a memory leak
}
)
}
}
}
I am mimicking something like Redux where a dispatch actions to change state, so I return an 'effect' from the UserAction login that gets the dispatch function. Everything works nicely but at the receiveValue line I get a memory leak with the following description:
Any ideas what could be causing it or how I can find out? Im fairly new to Xcode and Swift.

How to get JSON response and parse through it [duplicate]

Following code I have written and I am getting response in JSON also but the type of JSON is "AnyObject" and I am not able to convert that into Array so that I can use that.
Alamofire.request(.POST, "MY URL", parameters:parameters, encoding: .JSON) .responseJSON
{
(request, response, JSON, error) in
println(JSON?)
}
The answer for Swift 2.0 Alamofire 3.0 should actually look more like this:
Alamofire.request(.POST, url, parameters: parameters, encoding:.JSON).responseJSON
{ response in switch response.result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
let response = JSON as! NSDictionary
//example if there is an id
let userId = response.objectForKey("id")!
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%203.0%20Migration%20Guide.md
UPDATE for Alamofire 4.0 and Swift 3.0 :
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
print(response)
//to get status code
if let status = response.response?.statusCode {
switch(status){
case 201:
print("example success")
default:
print("error with response status: \(status)")
}
}
//to get JSON return value
if let result = response.result.value {
let JSON = result as! NSDictionary
print(JSON)
}
}
like above mention you can use SwiftyJSON library and get your values like i have done below
Alamofire.request(.POST, "MY URL", parameters:parameters, encoding: .JSON) .responseJSON
{
(request, response, data, error) in
var json = JSON(data: data!)
println(json)
println(json["productList"][1])
}
my json product list return from script
{ "productList" :[
{"productName" : "PIZZA","id" : "1","productRate" : "120.00","productDescription" : "PIZZA AT 120Rs","productImage" : "uploads\/pizza.jpeg"},
{"productName" : "BURGER","id" : "2","productRate" : "100.00","productDescription" : "BURGER AT Rs 100","productImage" : "uploads/Burgers.jpg"}
]
}
output :
{
"productName" : "BURGER",
"id" : "2",
"productRate" : "100.00",
"productDescription" : "BURGER AT Rs 100",
"productImage" : "uploads/Burgers.jpg"
}
Swift 3, Alamofire 4.4, and SwiftyJSON:
Alamofire.request(url, method: .get)
.responseJSON { response in
if response.data != nil {
let json = JSON(data: response.data!)
let name = json["people"][0]["name"].string
if name != nil {
print(name!)
}
}
}
That will parse this JSON input:
{
people: [
{ name: 'John' },
{ name: 'Dave' }
]
}
I found the answer on GitHub for Swift2
https://github.com/Alamofire/Alamofire/issues/641
Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
.responseJSON { request, response, result in
switch result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
case .Failure(let data, let error):
print("Request failed with error: \(error)")
if let data = data {
print("Response data: \(NSString(data: data, encoding: NSUTF8StringEncoding)!)")
}
}
}
I'm neither a JSON expert nor a Swift expert, but the following is working for me. :) I have extracted the code from my current app, and only changed "MyLog to println", and indented with spaces to get it to show as a code block (hopefully I didn't break it).
func getServerCourseVersion(){
Alamofire.request(.GET,"\(PUBLIC_URL)/vtcver.php")
.responseJSON { (_,_, JSON, _) in
if let jsonResult = JSON as? Array<Dictionary<String,String>> {
let courseName = jsonResult[0]["courseName"]
let courseVersion = jsonResult[0]["courseVersion"]
let courseZipFile = jsonResult[0]["courseZipFile"]
println("JSON: courseName: \(courseName)")
println("JSON: courseVersion: \(courseVersion)")
println("JSON: courseZipFile: \(courseZipFile)")
}
}
}
Hope this helps.
Edit:
For reference, here is what my PHP Script returns:
[{"courseName": "Training Title","courseVersion": "1.01","courseZipFile": "101/files.zip"}]
Swift 5
class User: Decodable {
var name: String
var email: String
var token: String
enum CodingKeys: String, CodingKey {
case name
case email
case token
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.email = try container.decode(String.self, forKey: .email)
self.token = try container.decode(String.self, forKey: .token)
}
}
Alamofire API
Alamofire.request("url.endpoint/path", method: .get, parameters: params, encoding: URLEncoding.queryString, headers: nil)
.validate()
.responseJSON { response in
switch (response.result) {
case .success( _):
do {
let users = try JSONDecoder().decode([User].self, from: response.data!)
print(users)
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
case .failure(let error):
print("Request error: \(error.localizedDescription)")
}
swift 3
pod 'Alamofire', '~> 4.4'
pod 'SwiftyJSON'
File json format:
{
"codeAd": {
"dateExpire": "2017/12/11",
"codeRemoveAd":"1231243134"
}
}
import Alamofire
import SwiftyJSON
private func downloadJson() {
Alamofire.request("https://yourlinkdownloadjson/abc").responseJSON { response in
debugPrint(response)
if let json = response.data {
let data = JSON(data: json)
print("data\(data["codeAd"]["dateExpire"])")
print("data\(data["codeAd"]["codeRemoveAd"])")
}
}
}
This was build with Xcode 10.1 and Swift 4
Perfect combination "Alamofire"(4.8.1) and "SwiftyJSON"(4.2.0). First you should install both pods
pod 'Alamofire' and pod 'SwiftyJSON'
The server response in JSON format:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip;q=1.0, compress;q=0.5",
"Accept-Language": "en;q=1.0",
"Host": "httpbin.org",
"User-Agent": "AlamoFire TEST/1.0 (com.ighost.AlamoFire-TEST; build:1; iOS 12.1.0) Alamofire/4.8.1"
},
"origin": "200.55.140.181, 200.55.140.181",
"url": "https://httpbin.org/get"
}
In this case I want print the "Host" info : "Host": "httpbin.org"
Alamofire.request("https://httpbin.org/get").validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful)")
if let json = response.data {
do{
let data = try JSON(data: json)
let str = data["headers"]["Host"]
print("DATA PARSED: \(str)")
}
catch{
print("JSON Error")
}
}
case .failure(let error):
print(error)
}
}
Keep Calm and happy Code😎
I found a way to convert the response.result.value (inside an Alamofire responseJSON closure) into JSON format that I use in my app.
I'm using Alamofire 3 and Swift 2.2.
Here's the code I used:
Alamofire.request(.POST, requestString,
parameters: parameters,
encoding: .JSON,
headers: headers).validate(statusCode: 200..<303)
.validate(contentType: ["application/json"])
.responseJSON { (response) in
NSLog("response = \(response)")
switch response.result {
case .Success:
guard let resultValue = response.result.value else {
NSLog("Result value in response is nil")
completionHandler(response: nil)
return
}
let responseJSON = JSON(resultValue)
// I do any processing this function needs to do with the JSON here
// Here I call a completionHandler I wrote for the success case
break
case .Failure(let error):
NSLog("Error result: \(error)")
// Here I call a completionHandler I wrote for the failure case
return
}
I usually use Gloss library to serialize or deserialize JSON in iOS. For example, I have JSON that looks like this:
{"ABDC":[{"AB":"qwerty","CD":"uiop"}],[{"AB":"12334","CD":"asdf"}]}
First, I model the JSON array in Gloss struct:
Struct Struct_Name: Decodable {
let IJ: String?
let KL: String?
init?(json: JSON){
self.IJ = "AB" <~~ json
self.KL = "CD" <~~ json
}
}
And then in Alamofire responseJSON, I do this following thing:
Alamofire.request(url, method: .get, paramters: parametersURL).validate(contentType: ["application/json"]).responseJSON{ response in
switch response.result{
case .success (let data):
guard let value = data as? JSON,
let eventsArrayJSON = value["ABDC"] as? [JSON]
else { fatalError() }
let struct_name = [Struct_Name].from(jsonArray: eventsArrayJSON)//the JSON deserialization is done here, after this line you can do anything with your JSON
for i in 0 ..< Int((struct_name?.count)!) {
print((struct_name?[i].IJ!)!)
print((struct_name?[i].KL!)!)
}
break
case .failure(let error):
print("Error: \(error)")
break
}
}
The output from the code above:
qwerty
uiop
1234
asdf
in swift 5 we do like, Use typealias for the completion. Typlealias nothing just use to clean the code.
typealias response = (Bool,Any?)->()
static func postCall(_ url : String, param : [String : Any],completion : #escaping response){
Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response) in
switch response.result {
case .success(let JSON):
print("\n\n Success value and JSON: \(JSON)")
case .failure(let error):
print("\n\n Request failed with error: \(error)")
}
}
}
The easy answer is to let AlamoFire do the decoding directly.
Surprisingly, you don't use the .responseJSON because that returns an untyped json object
Instead, you make your objects Decodable - and ask AF to decode directly to them
My json response contains an array of Account objects. I only care about the id and name keys (though there are many more)
struct Account:Codable {
let id:Int
let name:String
}
then simply
AF.request(url,
method: .get)
.responseDecodable(of:[Account].self) { response in
switch response.result {
case .success:
switch response.response?.statusCode {
case 200:
//response.value is of type [Account]
default:
//handle other cases
}
case let .failure(error):
//probably the decoding failed because your json doesn't match the expected format
}
}
let semaphore = DispatchSemaphore (value: 0)
var request = URLRequest(url: URL(string: Constant.localBaseurl2 + "compID")!,timeoutInterval: Double.infinity)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let response = response {
let nsHTTPResponse = response as! HTTPURLResponse
print(nsHTTPResponse)
}
if let error = error {
print ("\(error)")
return
}
if let data = data {
DispatchQueue.main.async {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase//or any other Decoder\
do{
let jsonDecoder = JSONDecoder()
let memberRecord = try jsonDecoder.decode(COMPLAINTSVC.GetComplaints.self, from: data)
print(memberRecord.message)
for detailData in memberRecord.message{
print(detailData)
}
}catch{
print(error.localizedDescription)
}
}
}
semaphore.signal()
}
task.resume()
semaphore.wait()
}
pod 'Alamofire'
pod 'SwiftyJSON'
pod 'ReachabilitySwift'
import UIKit
import Alamofire
import SwiftyJSON
import SystemConfiguration
class WebServiceHelper: NSObject {
typealias SuccessHandler = (JSON) -> Void
typealias FailureHandler = (Error) -> Void
// MARK: - Internet Connectivity
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
return false
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
// MARK: - Helper Methods
class func getWebServiceCall(_ strURL : String, isShowLoader : Bool, success : #escaping SuccessHandler, failure : #escaping FailureHandler)
{
if isConnectedToNetwork() {
print(strURL)
if isShowLoader == true {
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL).responseJSON { (resObj) -> Void in
print(resObj)
if resObj.result.isSuccess {
let resJson = JSON(resObj.result.value!)
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
debugPrint(resJson)
success(resJson)
}
if resObj.result.isFailure {
let error : Error = resObj.result.error!
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
debugPrint(error)
failure(error)
}
}
}else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func getWebServiceCall(_ strURL : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure :#escaping FailureHandler){
if isConnectedToNetwork() {
if isShowLoader == true {
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL, method: .get, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {(resObj) -> Void in
print(resObj)
if resObj.result.isSuccess {
let resJson = JSON(resObj.result.value!)
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if resObj.result.isFailure {
let error : Error = resObj.result.error!
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
})
}
else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func postWebServiceCall(_ strURL : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure :#escaping FailureHandler)
{
if isConnectedToNetwork()
{
if isShowLoader == true
{
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {(resObj) -> Void in
print(resObj)
if resObj.result.isSuccess
{
let resJson = JSON(resObj.result.value!)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if resObj.result.isFailure
{
let error : Error = resObj.result.error!
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
})
}else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func postWebServiceCallWithImage(_ strURL : String, image : UIImage!, strImageParam : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure : #escaping FailureHandler)
{
if isConnectedToNetwork() {
if isShowLoader == true
{
AppDelegate.getDelegate().showLoader()
}
Alamofire.upload(
multipartFormData: { multipartFormData in
if let imageData = UIImageJPEGRepresentation(image, 0.5) {
multipartFormData.append(imageData, withName: "Image.jpg")
}
for (key, value) in params! {
let data = value as! String
multipartFormData.append(data.data(using: String.Encoding.utf8)!, withName: key)
print(multipartFormData)
}
},
to: strURL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
//let datastring = String(data: response, encoding: String.Encoding.utf8)
// print(datastring)
}
case .failure(let encodingError):
print(encodingError)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
let error : NSError = encodingError as NSError
failure(error)
}
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { (response) -> Void in
if response.result.isSuccess
{
let resJson = JSON(response.result.value!)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if response.result.isFailure
{
let error : Error = response.result.error! as Error
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
}
case .failure(let encodingError):
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
let error : NSError = encodingError as NSError
failure(error)
}
}
)
}
else
{
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
}
==================================
Call Method
let aParams : [String : String] = [
"ReqCode" : Constants.kRequestCodeLogin,
]
WebServiceHelper.postWebServiceCall(Constants.BaseURL, params: aParams as [String : AnyObject]?, isShowLoader: true, success: { (responceObj) in
if "\(responceObj["RespCode"])" != "1"
{
let alert = UIAlertController(title: Constants.kAppName, message: "\(responceObj["RespMsg"])", preferredStyle: UIAlertControllerStyle.alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
}
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
else
{
let aParams : [String : String] = [
"Password" : self.dictAddLogin[AddLoginConstants.kPassword]!,
]
CommonMethods.saveCustomObject(aParams as AnyObject?, key: Constants.kLoginData)
}
}, failure:
{ (error) in
CommonMethods.showAlertWithError(Constants.kALERT_TITLE_Error, strMessage: error.localizedDescription,withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
})
}

How can I write a generic wrapper for alamofire request in swift?

How can I write a generic wrapper for alamofire request ?
How can I convert the POST and GET function to Generic function in the following code?
I need to have generic request functions that show different behaviors depending on the type of data received.
Also, Can the response be generic?
My non-generic code is fully outlined below
import Foundation
import Alamofire
import SwiftyJSON
// for passing string body
extension String: ParameterEncoding {
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
request.httpBody = data(using: .utf8, allowLossyConversion: false)
return request
}
}
public class ConnectionManager {
func Post (FirstName: String, LastName: String, NationalID: String, NationalCode: String, BirthDate: Date,Image: String,isFemale: Bool,Age:Int64,Avg:Double, completion: #escaping CompletionHandler) {
let body: [String:Any] = [
"FirstName":FirstName,
"LastName": LastName,
"NationalID": NationalID,
"NationalCode": NationalCode,
"BirthDate":BirthDate,
"Image": Image,
"isFemale": isFemale,
"Age": Age,
"Avg": Avg
]
Alamofire.request(BASE_URL, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseJSON { (response) in
if response.response?.statusCode == 200 {
guard let data = response.result.value else { return }
print(data)
completion(true)
} else {
print("error reg auth service")
guard let er = response.result.value else { return }
print(er)
completion(false)
debugPrint(response.result.error as Any)
}
}
}
func get(FirstName: String, LastName: String, NationalID: String, NationalCode: String, BirthDate: Date,Image: String,isFemale: Bool,Age:Int64,Avg:Double, completion: #escaping CompletionHandler) -> [Person] {
let body: [String:Any] = [
"FirstName":FirstName,
"LastName": LastName,
"NationalID": NationalID,
"NationalCode": NationalCode,
"BirthDate":BirthDate,
"Image": Image,
"isFemale": isFemale,
"Age": Age,
"Avg": Avg
]
Alamofire.request(BASE_URL, method: .get, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseJSON { (response) in
if response.response?.statusCode == 200 {
print("no error login in authservice")
guard let data = response.result.value else { return }
print(data)
completion(true)
}
else if response.response?.statusCode == 400 {
print("error 400 login in authservice")
guard let er = response.result.value else { return }
print(er)
debugPrint(response.result.error as Any)
completion(false)
} else {
print("error ## login in authservice")
guard let er = response.result.value else { return }
print(er)
debugPrint(response.result.error as Any)
completion(false)
}
}
return [Person]()
}
}
The best idea is to use the URLRequestConvertible Alamofires protocol and create your own protocol and simple structs for every API request:
import Foundation
import Alamofire
protocol APIRouteable: URLRequestConvertible {
var baseURL: String { get }
var path: String { get }
var method: HTTPMethod { get }
var parameters: Parameters? { get }
var encoding: ParameterEncoding { get }
}
extension APIRouteable {
var baseURL: String { return URLs.baseURL }
// MARK: - URLRequestConvertible
func asURLRequest() throws -> URLRequest {
let url = try baseURL.asURL().appendingPathComponent(path)
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = method.rawValue
return try encoding.encode(urlRequest, with: parameters)
}
}
and request can look like this:
struct GetBooks: APIRouteable {
var path: String
var method: HTTPMethod
var parameters: Parameters?
var encoding: ParameterEncoding
}
and inside the APIClient prepare the generic method:
func perform<T: Decodable>(_ apiRoute: APIRouteable,
completion: #escaping (Result<T>) -> Void) {
let dataRequest = session
.request(apiRoute)
dataRequest
.validate(statusCode: 200..<300)
.responseDecodable(completionHandler: { [weak dataRequest] (response: DataResponse<T>) in
dataRequest.map { debugPrint($0) }
let responseData = response.data ?? Data()
let string = String(data: responseData, encoding: .utf8)
print("Repsonse string: \(string ?? "")")
switch response.result {
case .success(let response):
completion(.success(response))
case .failure(let error):
completion(.failure(error))
}
})
}
and use it:
func getBooks(completion: #escaping (Result<[Book]>) -> Void) {
let getBooksRoute = APIRoute.GetBooks()
perform(getBooksRoute, completion: completion)
}