MoyaNC is an abstraction over abstraction (thanks to Moya 🖤🖤🖤) allowing you not to worry about the data mapping layer and just use your requests. The Codable protocol is used for JSON data mapping, all that is needed is to comply with this protocol and specify the type! Additionally, there is the ability to easily and simply cache any requests.
To run the example project, clone the repo, and run pod install
from the Example directory first. After some setup, using MoyaNC is really simple. You can access an API like this:
client = DefaultMoyaNC()
// type 'Test' must be Codable
client.request(.zen) { (result: Result<Test>) in
switch result {
case let .success(test):
// do something with the finished object
case let .failure(error):
// do something with error
}
}
That's a basic example. Many API requests need parameters.
Moya provides extensions for:
- Cache: Allows you to cache request data using a flexible caching policy.
Need to add
pod 'MoyaNetworkClient/Cache'
to the Podfile. - FutureResult: A Future is used to retrieve the result of a concurrent, asynchronous operation. It is a key building block in asynchronous, non-blocking code.
Need to add
pod 'MoyaNetworkClient/Future'
to the Podfile.
A simple example to use caching requests:
Definition of caching at the API level:
enum TestAPI {
case .zen
}
extension TestAPI: MoyaTargetType, CacheTarget {
var cachePolicy: MoyaCachePolicy {
return .returnCacheDataElseLoad
}
}
Definition of caching at the request level:
client = DefaultMoyaNC()
client.request(.zen, cache: .returnCacheDataElseLoad)
Here is an example of the kinds of complex logic possible with Futures:
client = DefaultMoyaNC()
// type 'Test' must be Codable
client.request(.zen)
.observeSuccess { (test: Test) in /* do something with the finished object */ }
.observeError { error in /* do something with error */) }
.execute()
For MoyaNetworkClient, use the following entry in your Podfile:
pod 'MoyaNetworkClient'
Then run pod install
.
In any file you'd like to use Moya in, don't forget to
import the framework with import MoyaNetworkClient
.
To integrate using Apple's Swift package manager, add the following as a dependency to your Package.swift
:
.package(url: "https://github.com/BarredEwe/MoyaNetworkClient.git", .upToNextMajor(from: "1.0.0"))
and then specify "MoyaNetworkClient"
as a dependency of the Target in which you wish to use MoyaNetworkClient.
MoyaNC 3.0:
- Now the validation of the request occurs by the standard Moya parameter
var validationType: ValidationType
BarredEwe, barredEwe@gmail.com
MoyaNC is released under an MIT license. See License for more information.