Alamofire Promise make generic request method - swift

I am trying to implement a generic method which is used to get data from API call.
Here is My code.
class Http {
class func asyncGetRequest()->Promise<List> {
return Promise {
resolve in
Alamofire.request(GET_PROFFESSIONAL).responseData().done { response in
guard let blog = try? JSONDecoder().decode(List.self, from: response.data) else {
let error = MyError(message: "Error: Couldn't decode")
resolve.reject(error)
return
}
resolve.fulfill(blog as List)
}.ensure {
}.catch { error in
resolve.reject(error)
}
}
}
}
I would like to make a generic method for all request. In my code, I made promise type List
and JSONDecoder().decode(List.self), Here I would like to make List type dynamic. How can we do that? I would like to reuse asyncGetRequest method for all the API calls. I want to pass request URL (GET_PROFFESSIONAL) and List class type as dynamic.

You could use Generics and write something like this:
class Http {
class func asyncGetRequest<ListType: Decodable>() -> Promise<ListType> {
return Promise { resolve in
Alamofire.request(GET_PROFFESSIONAL).responseData().done { response in
guard let blog = try? JSONDecoder().decode(ListType.self, from: response.data) else {
let error = MyError(message: "Error: Couldn't decode")
resolve.reject(error)
return
}
resolve.fulfill(blog as ListType)
}.ensure {
}.catch { error in
resolve.reject(error)
}
}
}
}

Alamofire (5.2.2)
PromiseKit (6.13.1)
struct Network {
static func get<Mode: Codable>(url: String) -> Promise<Mode> {
return Promise { seal in
AF.request(url, method: .get, encoding: URLEncoding.default, headers: nil)
.response { response in
if let error = response.error {
seal.reject(error)
return
}
guard let data = response.data else { return }
do {
let items = try JSONDecoder().decode(Mode.self, from: data)
seal.resolve(items, nil)
} catch {
seal.reject(error)
}
}
}
}
}

Related

Convert Alamofire Completion handler to Async/Await | Swift 5.5, *

I have the current function which works. I'm using it with completion handler:
func getTokenBalances(completion: #escaping (Bool) -> Void) {
guard let url = URL(string: "someApiUrlFromLostandFound") else {
print("Invalid URL")
completion(false)
return
}
AF.request(url, method: .get).validate().responseData(completionHandler: { data in
do {
guard let data = data.data else {
print("Response Error:", data.error as Any)
completion(false)
return
}
let apiJsonData = try JSONDecoder().decode(TokenBalanceClassAModel.self, from: data)
DispatchQueue.main.async {
self.getTokenBalancesModel = apiJsonData.data.items
completion(true)
}
} catch {
print("ERROR:", error)
completion(false)
}
})
}
How can I convert it to the new async/await functionality of swift 5.5?
This is what I've tried:
func getTokenBalances3() async {
let url = URL(string: "someApiUrlFromLostandFound")
let apiRequest = await withCheckedContinuation { continuation in
AF.request(url!, method: .get).validate().responseData { apiRequest in
continuation.resume(returning: apiRequest)
}
}
let task1 = Task {
do {
// Decoder is not asynchronous
let apiJsonData = try JSONDecoder().decode(SupportedChainsClassAModel.self, from: apiRequest.data!)
// Working data -> print(String(apiJsonData.data.items[0].chain_id!))
} catch {
print("ERROR:", error)
}
}
let result1 = await task1.value
print(result1) // values are not printed
}
But I'm not getting the value at the end on the print statement.
I'm kind of lost in the process, I'd like to convert my old functions, with this example it would help a lot.
EDIT:
The Answer below works, but I found my own solution while the Alamofire team implements async:
func getSupportedChains() async throws -> [AllChainsItemsClassAModel] {
var allChains: [AllChainsItemsClassAModel] = [AllChainsItemsClassAModel]()
let url = URL(string: covalentHqUrlConnectionsClassA.getCovalenHqAllChainsUrl())
let apiRequest = await withCheckedContinuation { continuation in
AF.request(url!, method: .get).validate().responseData { apiRequest in
continuation.resume(returning: apiRequest)
}
}
do {
let data = try JSONDecoder().decode(AllChainsClassAModel.self, from: apiRequest.data!)
allChains = data.data.items
} catch {
print("error")
}
return allChains
}
First of all, your structure is wrong. Do not start with your original code and wrap all of it in the continuation block. Just make a version of AF.request itself that's wrapped in a continuation block. For example, the JSON decoding is not something that should be part of what's being wrapped; it is what comes after the result of networking returns to you — it is the reason why you want to turn AF.request into an async function to begin with.
Second, as the error message tells you, resolve the generic, either by the returning into an explicit return type, or by stating the type as part of the continuation declaration.
So, for example, what I would do is just minimally wrap AF.request in an async throws function, where if we get the data we return it and if we get an error we throw it:
func afRequest(url:URL) async throws -> Data {
try await withUnsafeThrowingContinuation { continuation in
AF.request(url, method: .get).validate().responseData { response in
if let data = response.data {
continuation.resume(returning: data)
return
}
if let err = response.error {
continuation.resume(throwing: err)
return
}
fatalError("should not get here")
}
}
}
You'll notice that I didn't need to resolve the generic continuation type because I've declared the function's return type. (This is why I pointed you to my explanation and example in my online tutorial on this topic; did you read it?)
Okay, so the point is, now it is trivial to call that function within the async/await world. A possible basic structure is:
func getTokenBalances3() async {
let url = // ...
do {
let data = try await self.afRequest(url:url)
print(data)
// we've got data! okay, so
// do something with the data, like decode it
// if you declare this method as returning the decoded value,
// you could return it
} catch {
print(error)
// we've got an error! okay, so
// do something with the error, like print it
// if you declare this method as throwing,
// you could rethrow it
}
}
Finally I should add that all of this effort is probably wasted anyway, because I would expect the Alamofire people to be along with their own async versions of all their asynchronous methods, any time now.
Personally I think swallowing errors inside a network call is a bad idea, the UI should receive all errors and make the choice accordingly.
Here is an example of short wrapper around responseDecodable, that produces an async response.
public extension DataRequest {
#discardableResult
func asyncDecodable<T: Decodable>(of type: T.Type = T.self,
queue: DispatchQueue = .main,
dataPreprocessor: DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor,
decoder: DataDecoder = JSONDecoder(),
emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods) async throws -> T {
return try await withCheckedThrowingContinuation({ continuation in
self.responseDecodable(of: type, queue: queue, dataPreprocessor: dataPreprocessor, decoder: decoder, emptyResponseCodes: emptyResponseCodes, emptyRequestMethods: emptyRequestMethods) { response in
switch response.result {
case .success(let decodedResponse):
continuation.resume(returning: decodedResponse)
case .failure(let error):
continuation.resume(throwing: error)
}
}
})
}
}
This is a mix between my Answer and the one that matt provided. There will probably be an easier and cleaner implementation once the Alamofire team implements async but at least for now I'm out of the call backs hell...
func afRequest(url: URL) async throws -> Data {
try await withUnsafeThrowingContinuation { continuation in
AF.request(url, method: .get).validate().responseData { response in
if let data = response.data {
continuation.resume(returning: data)
return
}
if let err = response.error {
continuation.resume(throwing: err)
return
}
fatalError("Error while doing Alamofire url request")
}
}
}
func getSupportedChains() async -> [AllChainsItemsClassAModel] {
var allChains: [AllChainsItemsClassAModel] = [AllChainsItemsClassAModel]()
let url = URL(string: covalentHqUrlConnectionsClassA.getCovalenHqAllChainsUrl())
do {
let undecodedData = try await self.afRequest(url: url!)
let decodedData = try JSONDecoder().decode(AllChainsClassAModel.self, from: undecodedData)
allChains = decodedData.data.items
} catch {
print(error)
}
return allChains
}

RxSwift equivalent for switchmap

In RxJS you can use the value from a observable for a new observable. For example:
this.authService.login(username, password).pipe(
switchMap((success: boolean) => {
if(success) {
return this.contactService.getLoggedInContact()
} else {
return of(null)
}
})
).subscribe(contact => {
this.contact = contact
})
But now I have to do a project in Swift and I want to achieve the same thing. I can get the two methods working, but using the result of the first observable for the second observable is something i can't get working. The switchMap pipe is something that does not exist in RxSwift and I cannot find the equivalent.
I've tried mapping the result of the login function to the observable and then flatmapping it, but unfortunately that didn't work.
What is the best way to do this in Swift without using a subscribe in a subscribe?
EDIT I've tried flat map:
APIService.login(email: "username", password: "password")
.flatMapLatest { result -> Observable<Contact> in
if result {
return APIService.getLoggedInContact()
} else {
return .of()
}
}.subscribe(onNext: {result in
print("Logged in contact: \(result)")
}, onError: {Error in
print(Error)
}).disposed(by: disposeBag)
But unfortunately that didn't work, I get an error Thread 1: EXC_BAD_ACCESS (code=1, address=0x13eff328c)
EDIT2:
This is the login function
static func login(email: String, password: String) -> Observable<Bool> {
return Observable<String>.create { (observer) -> Disposable in
Alamofire.request(self.APIBASEURL + "/contact/login", method: .post, parameters: [
"email": email,
"password": password
], encoding: JSONEncoding.default).validate().responseJSON(completionHandler: {response in
if (response.result.isSuccess) {
guard let jsonData = response.data else {
return observer.onError(CustomError.api)
}
let decoder = JSONDecoder()
let apiResult = try? decoder.decode(ApiLogin.self, from: jsonData)
return observer.onNext(apiResult!.jwt)
} else {
return self.returnError(response: response, observer: observer)
}
})
return Disposables.create()
}.map{token in
return KeychainWrapper.standard.set(token, forKey: "authToken")
}
}
This is the getLoggedInContact function
static func getLoggedInContact() -> Observable<Contact> {
return Observable.create { observer -> Disposable in
Alamofire.request(self.APIBASEURL + "/contact/me", method: .get, headers: self.getAuthHeader())
.validate().responseJSON(completionHandler: {response in
if (response.result.isSuccess) {
guard let jsonData = response.data else {
return observer.onError(CustomError.api)
}
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(.apiNewsDateResult)
let apiResult = try? decoder.decode(Contact.self, from: jsonData)
return observer.onNext(apiResult!)
} else {
return self.returnError(response: response, observer: observer)
}
})
return Disposables.create()
}
}
There is operator flatMapLatest which does exactly the same as switchMap in RxJS.
You can find usage example here

handling json response Observable swift

I have an application which uses SwiftyJSON and works. How ever, I now want to expand the project and refactor the codes but I am having a bit of issue as I am now switching to Codable and I need to be able to mapJSON from any path and not a hard coded path. Currently my jsonResponse looks like this
/// handle the network response and map to JSON
/// - returns: Observable<JSON>
func handleResponseMapJSON() -> Observable<Result<JSON, ORMError>> {
return self.map { representor in
guard let response = representor as? Moya.Response else {
return .failure(ORMError.ORMNoRepresentor)
}
guard ((200...299) ~= response.statusCode) else {
return .failure(ORMError.ORMNotSuccessfulHTTP)
}
guard let json = JSON.init(rawValue: response.data),
json != JSON.null,
let code = json["code"].int else {
return .failure(ORMError.ORMParseJSONError)
}
guard code == BizStatus.BizSuccess.rawValue else {
let message: String = {
let json = JSON.init(data: response.data)
guard let msg = json["status"].string else { return "" }
return msg
}()
log(message, .error)
return .failure(ORMError.ORMBizError(resultCode: "\(code)", resultMsg: message))
}
return .success(json["result"])
}
}
how do I eliminate the passage of hardcoded json[""] value. Any help is appreciated
I suggest you try something like this:
protocol ResponseType: Codable {
associatedtype ResultType
var status: String { get }
var code: Int { get }
var result: ResultType { get }
}
func handleResponseMap<T, U>(for type: U.Type) -> (Any) -> Result<T, ORMError> where U: ResponseType, T == U.ResultType {
return { representor in
guard let response = representor as? Moya.Response else {
return .failure(.ORMNoRepresentor)
}
guard ((200...299) ~= response.statusCode) else {
return .failure(.ORMNotSuccessfulHTTP)
}
return Result {
try JSONDecoder().decode(U.self, from: response.data)
}
.mapError { _ in ORMError.ORMParseJSONError }
.flatMap { (response) -> Result<T, ORMError> in
guard response.code == BizStatus.BizSuccess.rawValue else {
log(response.status, .error)
return Result.failure(ORMError.ORMBizError(resultCode: "\(response.code)", resultMsg: response.status))
}
return Result.success(response.result)
}
}
}
Then you can map directly to your Codable type:
let result = self.map(handleResponseMap(for: MyResponse.self))
In the above, result will end up being an Observable<Result<ResultType, ORMError>>
I would like to do extension over PrimitiveSequenceType to treat it as Single
import Foundation
import RxSwift
import Moya
public extension PrimitiveSequenceType where TraitType == SingleTrait, ElementType == Response {
func map<T>(_ type: T.Type, using decoder: JSONDecoder? = nil) -> PrimitiveSequence<TraitType, T> where T: Decodable {
return self.map { data -> T in
let decoder = decoder ?? JSONDecoder()
return try decoder.decode(type, from: data.data)
}
}
}
and you can simply use it like:
return PokeAPIEndPoints.shared.provider.rx
.request(.specieDetails(id: specieId))
.filterSuccessfulStatusCodes()
.map(SpecieDetails.self)

unable to upload file RXSwift Moya multipart

I'm using Moya for handling communication between my swift application and api, I'm able to post and get data but unable to post file to api server, following is my code
enum TestApi {
...
case PostTest(obj: [String: AnyObject])
...
}
extension TestApi: TargetType {
var baseURL: NSURL {
switch self {
case .PostTest:
return NSURL(string: "http://192.168.9.121:3000")!
}
}
var path: String {
switch self {
case .PostTest:
return "/api/file"
}
}
var method: Moya.Method {
switch self {
case .PostTest:
return .POST
}
}
var parameters: [String: AnyObject]? {
switch self {
case .PostTest(let obj):
return ["image": obj["image"]!]
}
}
var sampleData: NSData {
return "".dataUsingEncoding(NSUTF8StringEncoding)!
}
var multipartBody: [MultipartFormData]? {
switch self {
case .PostTest(let multipartData):
guard let image = multipartData["image"] as? [NSData] else { return[] }
let formData: [MultipartFormData] = image.map{MultipartFormData(provider: .Data($0), name: "images", mimeType: "image/jpeg", fileName: "photo.jpg")}
return formData
default:
return []
}
}
}
and following is the way I called
return testApiProvider.request(.PostTest(obj: _file)).debug().mapJSON().map({ JSON -> EKResponse? in
return Mapper<EKResponse>().map(JSON)
})
I dont receive no response and no hit was sent to the api server
Multipart body is deprecated in Moya 8.0.0. Instead of that use Task for uploading.
Check this issue:
Moya multipart upload target
the subscription is missing in the calling code. This is not really a Moya problem, but problem with Reactive Extensions. the following .subscribeNext { _ in } fixed my issue
return testApiProvider
.request(.PostTest(obj: _file))
.debug()
.mapJSON()
.map({ JSON -> EKResponse? in
return Mapper<EKResponse>().map(JSON)
})
.subscribeNext { _ in }

swift, alamofire cancel previous request

I have an NetworkRequest class, where all my alamofire requests made:
class NetworkRequest {
static let request = NetworkRequest()
var currentRequest: Alamofire.Request?
let dataManager = DataManager()
let networkManager = NetworkReachabilityManager()
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
func downloadData<T: Film>(slug: String, provider: String, section: String, dynamic: String, anyClass: T, completion: ([T]?) -> Void ) {
var token: String = ""
if LOGGED_IN == true {
token = "\(NSUserDefaults.standardUserDefaults().valueForKey(TOKEN)!)"
}
let headers = [
"Access": "application/json",
"Authorization": "Bearer \(token)"
]
let dataUrl = "\(BASE_URL)\(slug)\(provider)\(section)\(dynamic)"
print(headers)
print(dataUrl)
if networkManager!.isReachable {
currentRequest?.cancel()
dispatch_async(dispatch_get_global_queue(priority, 0)) {
if let url = NSURL(string: dataUrl) {
let request = Alamofire.request(.GET, url, headers: headers)
request.validate().responseJSON { response in
switch response.result {
case .Success:
if let data = response.result.value as! [String: AnyObject]! {
let receivedData = self.dataManager.parseDataToFilms(data, someClass: anyClass)
completion(receivedData)
}
case .Failure(let error):
print("Alamofire error: \(error)")
if error.code == 1001 {
self.goToNoConnectionVC()
}
print("canceled")
}
}
}
}
} else {
goToNoConnectionVC()
}
}
}
And I need to cancel previous downloadData request, when the new one starts, tried to cancel using currentRequest?.cancel(), but it doesn't help.
Already tried to cancelOperations using NSOperationsBlock, but it doesn't cancels current operation.
I block UI now, so that user can't send another request. But this is not correct, causes some errors later...
Pls, help
Now on Alamofire 4 the Alamofire.Manager.sharedInstance.session is not available you should use this solution:
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
dataTasks.forEach { $0.cancel() }
uploadTasks.forEach { $0.cancel() }
downloadTasks.forEach { $0.cancel() }
}
and if you want to cancel (suspend, resume) a particular request you can check the request url in your .forEach block like this:
dataTasks.forEach {
if ($0.originalRequest?.url?.absoluteString == url) {
$0.cancel()
}
}
Found needed solution:
func stopAllSessions() {
Alamofire.Manager.sharedInstance.session.getAllTasksWithCompletionHandler { tasks in
tasks.forEach { $0.cancel() }
}
}
Update for Alamofire 5
func stopAllSessions() {
AF.session.getTasksWithCompletionHandler { (sessionDataTask, uploadData, downloadData) in
sessionDataTask.forEach { $0.cancel() }
uploadData.forEach { $0.cancel() }
downloadData.forEach { $0.cancel() }
}
}
If you want to cancel the request, you need to trace the requests made and try to cancel it. You can store it in an array and cancel every previous request stored.
In your code you create a request:
let request = Alamofire.request(.GET, url, headers: headers)
but you try to cancel the currentRequest?.cancel() that is never valued.
Swift 5
To cancel all requests use
Alamofire.Session.default.session.getTasksWithCompletionHandler({ dataTasks, uploadTasks, downloadTasks in
dataTasks.forEach { $0.cancel() }
uploadTasks.forEach { $0.cancel() }
downloadTasks.forEach { $0.cancel() }
})
To cancel a request with a particular url use
Alamofire.Session.default.session.getTasksWithCompletionHandler({ dataTasks, uploadTasks, downloadTasks in
dataTasks.forEach {
if ($0.originalRequest?.url?.absoluteString == "www.google.com") {
$0.cancel()
}
}
uploadTasks.forEach {
if ($0.originalRequest?.url?.absoluteString == "www.google.com") {
$0.cancel()
}
}
downloadTasks.forEach {
if ($0.originalRequest?.url?.absoluteString == "www.google.com") {
$0.cancel()
}
}
})