Swiftpack.co -  Rahul-Mayani/RRCombineAlamofireAPI as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Rahul-Mayani/RRCombineAlamofireAPI
Alamofire API Request by Combine framework
.package(url: "https://github.com/Rahul-Mayani/RRCombineAlamofireAPI.git", from: "1.0.0")

RRCombineAlamofireAPI

License: MIT iOS

SPM

Swift 5

Alamofire API Request by Combine framework

Installation

Dependency

Alamofire

Manually

  1. Download and drop Source folder with files in your project.
  2. Add your API end point URL in your project.
  3. Congratulations!

SPM (Swift Package Manager)

In Xcode, use the menu File > Swift Packages > Add Package Dependency... and enter the package URL https://github.com/Rahul-Mayani/RRCombineAlamofireAPI.

Usage example

To run the example project, clone the repo, and run spm from the Example directory first.


/// Uses
let createCustomSession = Session()
let request =  RRCombineAlamofireAPI.shared
                  .setSessionManager(createCustomSession) //`Session` creates and manages Alamofire's `Request` types during their lifetimes.
                  .setHttpMethod(.get) // httpMethod: GET, POST, PUT & DELETE
                  .setURL("Your API URL")
                  .setHeaders([:]) // a dictionary of parameters to apply to a `HTTPHeaders`.
                  .setParameter([:]) // a dictionary of parameters to apply to a `URLRequest`.

request.subscribe(on: DispatchQueue.global())
    .receive(on: DispatchQueue.main)
    .sink { (completion) in
       switch completion {
       case .finished:
           break
       case .failure(let error):
           print(error.localizedDescription)
       }
    } receiveValue: { (response) in
        /// The response of data type is Data.
        /// <#T##Here: decode JSON Data into your custom model structure / class#>
        print(response)
    }
    .cancel()


/// Example 1
/// Loader start
let userIds = [1, 2, 3]
Just(userIds)
    .setFailureType(to: Error.self)
    .flatMap { (values) -> AnyPublisher<[User], Error> in
        let tasks = values.publisher.flatMap { userId in
                         RRCombineAlamofireAPI.shared.setURL("https://jsonplaceholder.typicode.com/users/\(userId)")
                                .map { $0 }
                                .decode(type: User.self, decoder: JSONDecoder())
                                .setDeferred()
                    }
        return Publishers.MergeMany(tasks).collect().setDeferred()
    }
    .subscribeAndReceivedData { (allUsers) in
        print("Got users:")
        /// Loader stop
    }

/// Example 2
/// Loader start
RRCombineAlamofireAPI.shared.setURL("https://jsonplaceholder.typicode.com/users/1")
    .flatMap { response -> AnyPublisher<Data, Error> in
        let data = User.decodeJsonData(response)
        print(data?.username ?? "")
        return RRCombineAlamofireAPI.shared.setURL("https://jsonplaceholder.typicode.com/users/2")
                //.delay(for: .seconds(1), scheduler: RunLoop.main)
                .setDeferred()
    }
    .subscribeAndReceivedData { (response) in
        guard let data = response as? Data else { return }
        let user = User.decodeJsonData(data)
        print(user?.username ?? "")
        /// Loader stop
    }


It's not part of SPM
////  subscribeAndReceivedData & Deferred as Publisher extension functions for reuse
extension Publisher {
    // MARK: - Subscribe And Received Data From Server -
    func subscribeAndReceivedData(_ qos: DispatchQoS = .background, data: @escaping((Any) -> ())) {
        subscribe(on: DispatchQueue( label: "rrcombine.queue.\(qos)", qos: qos, attributes: [.concurrent], target: nil))
            .receive(on: DispatchQueue.main)
            .sink { (completion) in
                switch completion {
                case .finished:
                    break
                case .failure(let error):
                    print(error.localizedDescription)
                    /// UIAlertController
                }
                /// Loader stop
            } receiveValue: { response in
                data(response)
            }.cancel()
    }
    // MARK: - Deferred -
    func setDeferred() -> AnyPublisher<Self.Output, Self.Failure> {
        Deferred { self }
            .eraseToAnyPublisher()
    }
}

Contribute

We would love you for the contribution to RRCombineAlamofireAPI, check the LICENSE file for more info.

License

RRCombineAlamofireAPI is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

Alamofire API Request by Combine framework
4 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API