Latest 1.0.0
Homepage https://github.com/Can-Sahin/MoyaPromise
License MIT
Platforms ios 8.0
Dependencies Alamofire, PromiseKit/Alamofire, PromiseKit, Moya
Authors

A ‘DataService’ layer implemented with PromiseKit on the top of Moya with extra functionalities using the power of PromiseKit, especially for JSON-oriented RestAPI calls.

What is it good for?

  • To use Moya requests with Promise pattern
    • Implement any custom desired generic functionality at any step of the execution
    • Custom error handling and propagation
  • To logically group, isolate and modify the collection of network requests
  • To strictly bind the targets with their providers
    Extra functionalities
    • Conditionally re-try a request
    • Response serialization
      • Out of the box Codable object, String, Data , [String: Any]serialization
      • Flexible to implement your own custom serialization
    • Example project provides an example Public Key Pinning implementation on Alamofire

Dependencies

Currently build upon following pods and version

Alamofire (4.7.0)

Moya (11.0.1)

PromiseKit (6.2.1)

Installation

CocoaPods

Add following pod to your podfile

pod 'MoyaPromise'

Then run pod install.

Don’t forget to
import the framework to swift files like import MoyaPromise.

Example

Example Project in the repository provides examples of how to do the requests and how to structure your code.
Its not ready-to-run since MoyaTargets are dummy ones. Its mainly for examining and providing the library use cases. To run it,

first pod install

then modify the MoyaTargets and modify ViewController‘s default functions to call various requests.

Some basic code examples

Example code

Make getCarItem request where the response is serialized to an object

let carDataService = CarDataService()
carDataService.getCarItem("carId").done{carItem in
     print(carItem.Name)
}.catch{error in
    print(error)
}

where CarDataService is

public class CarDataService: DefaultDataService<CarAPI>{
    public func getCarItem(_ id:String) -> Promise<DummyCarItem>  {
        return self.request(target: CarAPI.Car(id)).asParsedObject().promise
    }
    public func getDriver(_ carId:String ) -> Promise<String>  {
        return self.request(target: CarAPI.Driver(carId)).asString().promise
    }
}

Handle various errors either thrown by you or by the network during the request

carDataService.getCar("someId").done{carString in
        print(carString)
}.catch{error in
    // Custom error handling
    if let err = error as? SomeCustomError{
        if case .Unauthorized = err{
            print("Not authorized")
            // Go to login
        }
    }
}.always {
    //UIActivityIndicatorView.stopAnimating()
}

Check the example project to see more of the abilities

License

MIT

Latest podspec

{
    "name": "MoyaPromise",
    "version": "1.0.0",
    "summary": "PromiseKit-oriented 'DataService' layer on top of Moya",
    "description": "A 'DataService' layer implemented with PromiseKit on the top of Moya with extra functionalities using the power of PromiseKit, especially for JSON-oriented RestAPI calls. Includes several serialization, conditional re-try mechanism.",
    "homepage": "https://github.com/Can-Sahin/MoyaPromise",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Can-Sahin": "[email protected]"
    },
    "swift_version": "4.0",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/Can-Sahin/MoyaPromise.git",
        "tag": "1.0.0"
    },
    "source_files": "src/MoyaPromise/*.swift",
    "dependencies": {
        "Alamofire": [
            "~> 4.7.0"
        ],
        "PromiseKit/Alamofire": [
            "~> 6.0"
        ],
        "PromiseKit": [
            "~> 6.2.1"
        ],
        "Moya": [
            "~> 11.0.1"
        ]
    }
}

Pin It on Pinterest

Share This