Alamofire publish decodable - ios

I have a HTTP request to my server like this.
func loginUser( username: String, password: String )->AnyPublisher<UserModel, Error>{
let url = URL( string: "\(Credentials.BASE_URL)auth/login")!
let userSignInModel = SignInModel(username: username, password: password)
return AF.request(url,
method: .post,
parameters: userSignInModel,
encoder: JSONParameterEncoder.default)
.validate()
.publishDecodable(type: UserModel.self)
.value()
.mapError{$0 as Error}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
and get response like this
self.dataManager.loginUser(username: self.logInUsername, password: self.logInPassword)
.sink { (response) in
print( response )
switch response {
case .failure( let error ):
self.createAlert(with: error.localizedDescription,
for: .loginAlert,
responseCode: error.asAFError?.responseCode)
case .finished:
break
}
} receiveValue: { (userModel) in
self.token = userModel.token
self.userID = userModel.user.id
}.store(in: &cancellableSet)
but the problem is that I am not able to get error message from the server, how it can be done?

There are several different approaches to parsing responses which return success or response values. Perhaps the simplest is to map any initial failures to your own error type which parses the information you need. For example, given this error type:
struct NetworkError: Error {
let initialError: AFError
let backendError: BackendError?
}
Where BackendError encapsulates the information returned from the backend. Then you can map the response from Alamofire.
AF.request(url,
method: .post,
parameters: userSignInModel)
.validate()
.publishDecodable(type: UserModel.self)
.map { response in
response.mapError { error in
// Check to see if it's an error that should have backend info.
// Assuming it is, parse the BackendError.
let backendError = response.data.flatMap { try? JSONDecoder().decode(BackendError.self, from: $0) }
return NetworkError(initialError: error, backendError: backendError)
}
}
Other alternatives include an enum-based response type that separates your success and failures, or your own response serializer which does the error parsing internally.

Related

Alamofire + Combine: Get the HTTP response status code

I am currently using Alamofire which contains Combine support and using it following way:
let request = AF.request(endpoint)
...
request
.publishDecodable(type: T.self, decoder: decoder)
.value()
.eraseToAnyPublisher()
This will publish result and AFError but from subscriber's .sink, I can't find anywhere to get the HTTP status code. What's the best way to get the status code in subscriber?
If you want the response code, don't erase the DataPublisher using .value(). Instead, use the DataResponse you get from the various publish methods, which includes all of the various response information, including status code. You can then .map it into whatever type you need.
For Swift 5.X and Xcode 12.4
For debugging purposes you can intercept the response right before the Combine publisher (publishDecodable()) and get some of the elements of the URL Response, with :
session.request(signedRequest)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
}
The easy MVVM way:
func fetchChats() -> AnyPublisher<ChatListModel, AFError> {
let url = URL(string: "Your_URL")!
AF.request(url, method: .get)
.validate()
.publishDecodable(type: ChatListModel.self)
.value()
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
Later in viewModel
private var subscriptions: Set<AnyCancellable> = []
// some func
dataManager.fetchChats()
.sink {[weak self] completion in
guard let self = self else { return }
switch completion {
case .failure(let error):
switch error.responseCode {
case 401:
//do something with code
default:
print(error.responseCode)
}
print("All errors:\(error)")
case .finished:
break
}
} receiveValue: {[weak self] message in
guard let self = self else { return }
self.message = message
}
.store(in: &subscriptions)

Extract response data from api call in alamofire

i've read many documents about getting the response object from the api , but cant derive how to extract the data. I've used alamofire for api call . The api call is like
AF.request("http://10.177.41.163:9000/signup",
method: .post,
parameters: parameters,
encoder: JSONParameterEncoder.default).responseJSON{ response in
print(response.result)
in my print statement i get the responses as
`success({
error = {
code = PC05;
message = "User already exsists";
};
payload = {
};
success = 0;
})`
which is fine , but i want to extract suppose the error code, how to achieve that? and in general how to extract data from responses in swift.
You need to decode the jSON response, so create a Model to decode this, of course also check the response if it was successful before, you'll set an example:
Declare yourself a model:
struct RootResponse : Decodable {
var error: ErrorStruct
var payload: PayloadStruct
var success: Int
}
struct ErrorStruct: Decodable {
var code: String
var message: String
}
struct PayloadStruct : Decodable {
var ?
}
Once you've declared your model based on your jSON response, then you can switch to the function that makes the request to you:
AF.request("http://10.177.41.163:9000/signup",
method: .post,
parameters: parameters,
encoder: JSONParameterEncoder.default).responseJSON{ response in
print(response.result)
switch response.result {
case .success:
if let data = response.data {
print(data)
// Convert This in JSON
do {
let responseDecoded = try JSONDecoder().decode(RootResponse.self, from: data)
print("USER: ", responseDecoded.error.code, "Etc...")
}catch let error as NSError{
print(error)
}
}
case .failure(let error):
print("Error:", error)
}
}
I hope that's been helpful.
AF.request("http://10.177.41.163:9000/signup",
method: .post,
parameters: parameters,
encoder: JSONParameterEncoder.default).responseJSON{ response in
print(response.result)
let responseJSON = JSON(response.result) // response converted in json format
let statusCode = responseJSON["error"]["code"].stringValue // You can get status code
Note : Install pod 'SwiftyJSON' to convert response.result in JSON format.

Parsing JSON Lines with Alamofire/Codable

Is it possible to parse JSON lines with Alamofire and codable?
Here is my code right now.
Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseString {(response) in
switch response.result {
case .success(let value):
print ("response is \(value)")
case .failure(let error):
print ("error is \(error)")
}
}
This prints all the JSON lines as a string but I want to serialize the response as an array of JSON. How would I do that? The problem with JSON lines is that it returns each set of json on a separate line so it is not familiar to alamofire.
Here is what I tried as if this was traditional JSON which obviously it is not so this did not work:
Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON {(response) in
switch response.result {
case .success(let value):
print ("response is \(value)")
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
let data = try! JSONSerialization.data(withJSONObject: value)
do {
let logs = try decoder.decode([Logs].self, from: data)
completion(logs)
} catch let error {
print ("error parsing get logs: \(error)")
}
case .failure(let error):
print ("failed get logs: \(error) ** \(response.result.value ?? "")")
}
}
For anybody unfamiliar with json lines here is the official format info: http://jsonlines.org
{"_logtype":"syslogline","_ingester":"agent","_ip":"40.121.203.183","pid":5573,"program":"docker","_host":"k8s-master-5A226838-0","logsource":"k8s-master-5A226838-0","_app":"syslog","_file":"/var/log/syslog","_line":"docker[5573]: I0411 00:18:39.644199 6124 conversion.go:134] failed to handle multiple devices for container. Skipping Filesystem stats","_ts":1491869920198,"timestamp":"2017-04-11T00:18:39.000Z","_id":"804760774821019649"}
{"_logtype":"syslogline","_ingester":"agent","_ip":"40.121.203.183","pid":5573,"program":"docker","_host":"k8s-master-5A226838-0","logsource":"k8s-master-5A226838-0","_app":"syslog","_file":"/var/log/syslog","_line":"docker[5573]: I0411 00:18:39.644167 6124 conversion.go:134] failed to handle multiple devices for container. Skipping Filesystem stats","_ts":1491869920198,"timestamp":"2017-04-11T00:18:39.000Z","_id":"804760774821019648"}
{"_logtype":"syslogline","_ingester":"agent","_ip":"40.121.203.183","pid":5573,"program":"docker","_host":"k8s-master-5A226838-0","logsource":"k8s-master-5A226838-0","_app":"syslog","_file":"/var/log/syslog","_line":"docker[5573]: I0411 00:18:37.053730 6124 operation_executor.go:917] MountVolume.SetUp succeeded for volume \"kubernetes.io/secret/6f322c04-e1d2-11e6-bca0-000d3a111245-default-token-swb07\" (spec.Name: \"default-token-swb07\") pod \"6f322c04-e1d2-11e6-bca0-000d3a111245\" (UID: \"6f322c04-e1d2-11e6-bca0-000d3a111245\").","_ts":1491869917193,"timestamp":"2017-04-11T00:18:37.000Z","_id":"804760762212941824"}
Here is an example of writing custom DataSerializer in Alamofire that you could use for decoding your Decodable object.
I am using an example from random posts json url https://jsonplaceholder.typicode.com/posts
Here is an example of Post class and custom serializer class PostDataSerializer.
struct Post: Decodable {
let userId: Int
let id: Int
let title: String
let body: String
}
struct PostDataSerializer: DataResponseSerializerProtocol {
enum PostDataSerializerError: Error {
case InvalidData
}
var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<[Post]> {
return { request, response, data, error in
if let error = error {
return .failure(error)
}
guard let data = data else {
return .failure(PostDataSerializerError.InvalidData)
}
do {
let jsonDecoder = JSONDecoder()
let posts = try jsonDecoder.decode([Post].self, from: data)
return .success(posts)
} catch {
return .failure(error)
}
}
}
}
You could simply hook this upto your Alamofire client which sends request to remote url like so,
let request = Alamofire.request("https://jsonplaceholder.typicode.com/posts")
let postDataSerializer = PostDataSerializer()
request.response(responseSerializer: postDataSerializer) { response in
print(response)
}
You could also do additional error checking for the error and http response code in serializeResponse getter of the custom serializer.
For you json line, it seems that each json object is separated with new line character in so called json line format. You could simply split the line with new line characters and decode each line to Log.
Here is Log class I created.
struct Log: Decodable {
let logType: String
let ingester: String
let ip: String
let pid: Int
let host: String
let logsource: String
let app: String
let file: String
let line: String
let ts: Float64
let timestamp: String
let id: String
enum CodingKeys: String, CodingKey {
case logType = "_logtype"
case ingester = "_ingester"
case ip = "_ip"
case pid
case host = "_host"
case logsource
case app = "_app"
case file = "_file"
case line = "_line"
case ts = "_ts"
case timestamp
case id = "_id"
}
}
And custom log serializer that you could use with your Alamofire. I have not handled error in the following serializer, I hope you can do it.
struct LogDataSerializer: DataResponseSerializerProtocol {
enum LogDataSerializerError: Error {
case InvalidData
}
var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<[Post]> {
return { request, response, data, error in
if let error = error {
return .failure(error)
}
guard let data = data else {
return .failure(LogDataSerializerError.InvalidData)
}
do {
let jsonDecoder = JSONDecoder()
let string = String(data: data, encoding: .utf8)!
let allLogs = string.components(separatedBy: .newlines)
.filter { $0 != "" }
.map { jsonLine -> Log? in
guard let data = jsonLine.data(using: .utf8) else {
return nil
}
return try? jsonDecoder.decode(Log.self, from: data)
}.flatMap { $0 }
return .success(allLogs)
} catch {
return .failure(error)
}
}
}
}
Alamofire is extensible, so I'd suggest writing your own response ResponseSerializer that can parse JSON by line. It seems that each line should parse fine, they're just not parseable together since the whole document isn't valid JSON.

How do I print the body of a http request in swift 3 using Alamofire

I'm trying to make a patch request with Alamofire, in swift 3, with a x-www-form-urlencoded body but it doesn't work. I want to print the body of the request so I can compare with the result when I make the request by postman.
Here is my code
let parameters: [String:AnyObject] = ["conversation[emails_attributes][0][content]":text as AnyObject]
alamofireManager.request(URL, method: .patch, parameters: parameters, encoding: URLEncoding(destination: .httpBody), headers: headers)
.response { httpResponse in
if let error = httpResponse.error {
print(error)
completionHandler(.defaultFailResponse(NSLocalizedString("An errror has occurred",comment:"General Error Title")))
} else {
print("HTTP: \(httpResponse.response?.statusCode)")
if let response = httpResponse.response{
switch response.statusCode {
case 200:
let jsonData = JSON(data: httpResponse.data!)
if (jsonData == nil) {
completionHandler(.defaultFailResponse(NSLocalizedString("Error on Data",comment:"Message error when data is null")))
return
}
print("Message Created")
completionHandler(.defaultSuccessResponse)
default:
completionHandler(.defaultFailResponse(NSLocalizedString("Connection Error",comment:"Connection failed")))
}
print("Failed")
}
}
}
I tried doing print("request body: \(request.HTTPBody)") but it doesn't work, the error message was
error: ambiguous reference to member 'request(_:method:parameters:encoding:headers:)'

Alamofire returns .Success on error HTTP status codes

I have a pretty simple scenario that I'm struggling with. I'm using Alamofire to register a user on a rest API. The first call to register is successful and the user can log in. The second call, when trying to register with the same email address should result in a HTTP status code 409 from the server. Alamofire, however, returns a .Success with an empty request and response. I have tested this this API with postman and it correctly returns a 409.
Why is Alamofire not returning .Failure(error), where the error has status code info etc?
Here is the call I run with the same input each time.
Alamofire.request(.POST, "http://localhost:8883/api/0.1/parent", parameters: registrationModel.getParentCandidateDictionary(), encoding: .JSON).response(completionHandler: { (req, res, d, e) -> Void in
print(req, res, d, e)
})
From the Alamofire manual:
Validation
By default, Alamofire treats any completed request to be successful,
regardless of the content of the response. Calling validate before a
response handler causes an error to be generated if the response had
an unacceptable status code or MIME type.
You can manually validate the status code using the validate method, again, from the manual:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { response in
print(response)
}
Or you can semi-automatically validate the status code and content-type using the validate with no arguments:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.validate()
.responseJSON { response in
switch response.result {
case .success:
print("Validation Successful")
case .failure(let error):
print(error)
}
}
If using response, you can check the NSHTTPURLResponse parameter:
Alamofire.request(urlString, method: .post, parameters: registrationModel.getParentCandidateDictionary(), encoding: JSONEncoding.default)
.response { response in
if response.response?.statusCode == 409 {
// handle as appropriate
}
}
By default, 4xx status codes aren't treated as errors, but you can use validate to treat it as an such and then fold it into your broader error handling:
Alamofire.request(urlString, method: .post, parameters: registrationModel.getParentCandidateDictionary(), encoding: JSONEncoding.default)
.validate()
.response() { response in
guard response.error == nil else {
// handle error (including validate error) here, e.g.
if response.response?.statusCode == 409 {
// handle 409 here
}
return
}
// handle success here
}
Or, if using responseJSON:
Alamofire.request(urlString, method: .post, parameters: registrationModel.getParentCandidateDictionary(), encoding: JSONEncoding.default)
.validate()
.responseJSON() { response in
switch response.result {
case .failure:
// handle errors (including `validate` errors) here
if let statusCode = response.response?.statusCode {
if statusCode == 409 {
// handle 409 specific error here, if you want
}
}
case .success(let value):
// handle success here
print(value)
}
}
The above is Alamofire 4.x. See previous rendition of this answer for earlier versions of Alamofire.
if you use validate() you'll loose the error message from server, if you want to keep it, see this answer https://stackoverflow.com/a/36333378/1261547
Here is my code for AlamoFire error catching:
switch response.result {
case .success(let value):
completion(.success(value))
case .failure(var error):
var errorString: String?
if let data = response.data {
if let json = try? (JSONSerialization.jsonObject(with: data, options: []) as! [String: String]) {
errorString = json["error"]
}
}
let error = MyError(str: errorString!)
let x = error as Error
print(x.localizedDescription)
completion(.failure(x))
}
and the MyError class difinition:
class MyError: NSObject, LocalizedError {
var desc = ""
init(str: String) {
desc = str
}
override var description: String {
get {
return "MyError: \(desc)"
}
}
//You need to implement `errorDescription`, not `localizedDescription`.
var errorDescription: String? {
get {
return self.description
}
}
}

Resources