Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/can-sahin/moyapromise
PromiseKit-oriented 'DataService' layer on top of Moya
https://github.com/can-sahin/moyapromise
dataservice moya promisekit swift
Last synced: 16 days ago
JSON representation
PromiseKit-oriented 'DataService' layer on top of Moya
- Host: GitHub
- URL: https://github.com/can-sahin/moyapromise
- Owner: Can-Sahin
- License: mit
- Created: 2017-11-01T12:05:57.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-02-08T11:47:15.000Z (over 3 years ago)
- Last Synced: 2024-10-04T16:26:36.131Z (about 1 month ago)
- Topics: dataservice, moya, promisekit, swift
- Language: Swift
- Homepage:
- Size: 324 KB
- Stars: 12
- Watchers: 1
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MoyaPromise
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
### CocoaPodsAdd following pod to your podfile
```rb
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 `MoyaTarget`s 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
```swift
let carDataService = CarDataService()
carDataService.getCarItem("carId").done{carItem in
print(carItem.Name)
}.catch{error in
print(error)
}
```where **CarDataService** is
```swift
public class CarDataService: DefaultDataService{
public func getCarItem(_ id:String) -> Promise {
return self.request(target: CarAPI.Car(id)).asParsedObject().promise
}
public func getDriver(_ carId:String ) -> Promise {
return self.request(target: CarAPI.Driver(carId)).asString().promise
}
}
```
Handle various errors either thrown by you or by the network during the request
```swift
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[Moya]:
[PromiseKit]: