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,parametersandparameterEncodingproperty. It makes you scroll down again and again. Besides, if you would like to set different HTTP header fields, you should customizeendpointClosureofMoyaProvider.If you’re as lazy as I am, MoyaSugar is for you.
At a Glance
Forget about
method,path,parameters,parameterEncodingandendpointClosure. Useroute,params,httpHeaderFieldsinstead.Use
MoyaSugarProviderinstead ofMoyaProvider.Complete Example
APIs
🔗
protocol SugarTargetType🔗
var route: RouteReturns
Routewhich contains HTTP method and URL path information.Example:
🔗
var url: URLReturns the request URL. It retuens
defaultURLas default, which is the combination ofbaseURLandpath. Implement this property to return custom url. See #6 for detail.🔗
var params: Parameters?Returns
Parameterswhich contains parameter encoding and values.Example:
Note: If the
MoyaSugarProvideris initialized with anendpointClosureparameter, HTTP header fields ofendpointClosurewill 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.