Moya is an elegant network abstraction layer which abstracts API endpoints gracefully with enum. However, it would become massive when the application is getting larger. Whenever you add an endpoint, you should write code at least 5 places: enum case, method, path, parameters and parameterEncoding property. It makes you scroll down again and again. Besides, if you would like to set different HTTP header fields, you should customize endpointClosure of MoyaProvider.
If you’re as lazy as I am, MoyaSugar is for you.
At a Glance
Forget about method, path, parameters, parameterEncoding and endpointClosure. Use route, params, httpHeaderFields instead.
Returns the request URL. It retuens defaultURL as default, which is the combination of baseURL and path. Implement this property to return custom url. See #6 for detail.
Note: If the MoyaSugarProvider is initialized with an endpointClosure parameter, HTTP header fields of endpointClosure will be used. This is how to extend the target’s HTTP header fields in endpointClosure:
let endpointClosure: (GitHubAPI) -> Endpoint<GitHubAPI> = { target in
let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
return defaultEndpoint
.adding(newHTTPHeaderFields: ["Authorization": "MySecretToken"])
}
MoyaSugar
Syntactic sugar for Moya.
Why?
Moya is an elegant network abstraction layer which abstracts API endpoints gracefully with
enum
. However, it would become massive when the application is getting larger. Whenever you add an endpoint, you should write code at least 5 places: enum case,method
,path
,parameters
andparameterEncoding
property. It makes you scroll down again and again. Besides, if you would like to set different HTTP header fields, you should customizeendpointClosure
ofMoyaProvider
.If you’re as lazy as I am, MoyaSugar is for you.
At a Glance
Forget about
method
,path
,parameters
,parameterEncoding
andendpointClosure
. Useroute
,params
,httpHeaderFields
instead.Use
MoyaSugarProvider
instead ofMoyaProvider
.Complete Example
APIs
🔗
protocol SugarTargetType
🔗
var route: Route
Returns
Route
which contains HTTP method and URL path information.Example:
🔗
var url: URL
Returns the request URL. It retuens
defaultURL
as default, which is the combination ofbaseURL
andpath
. Implement this property to return custom url. See #6 for detail.🔗
var params: Parameters?
Returns
Parameters
which contains parameter encoding and values.Example:
Note: If the
MoyaSugarProvider
is initialized with anendpointClosure
parameter, HTTP header fields ofendpointClosure
will be used. This is how to extend the target’s HTTP header fields inendpointClosure
:Requirements
Same with Moya
Installation
Using CocoaPods:
Contributing
License
MoyaSugar is under MIT license. See the LICENSE file for more info.