Simple Swift Requests
Introduction
MoyaNC is an abstraction over abstraction (thanks to Moya
Example
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.
Extensions
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.
Cache
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)
FutureResult
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()
Installation
CocoaPods
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
.
Swift Package Manager
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.
Migration Guides
MoyaNC 3.0:
- Now the validation of the request occurs by the standard Moya parameter
var validationType: ValidationType
Author
BarredEwe, [email protected]
License
MoyaNC is released under an MIT license. See License for more information.