add static versions of methods, add base method for override request

This commit is contained in:
Ivan Smolin 2018-04-04 18:11:46 +03:00
parent c5afc6f33c
commit 6af656effd
3 changed files with 26 additions and 5 deletions

View File

@ -2,10 +2,20 @@ import LeadKit
import RxSwift
import Alamofire
extension NetworkService {
{% set serviceName = concat(networkServiceName, "NetworkService") -%}
extension {{ serviceName }} {
{% for method in methods %}
{%- include 'blocks/method/method-func.twig' with { method: method } %}
{%- include 'blocks/method/method-func.twig' with { method: method, isStatic: false } %}
{% endfor %}
}
extension Singleton where Self: {{ serviceName }} {
{% for method in methods %}
{%- include 'blocks/method/method-func.twig' with { method: method, isStatic: true } %}
{% endfor %}
}

View File

@ -1,4 +1,5 @@
import LeadKit
import RxSwift
{% set serviceName = concat(networkServiceName, "NetworkService") -%}
class {{ serviceName }}: NetworkService, ConfigurableNetworkService {
@ -11,5 +12,9 @@ class {{ serviceName }}: NetworkService, ConfigurableNetworkService {
self.init(sessionManager: {{ serviceName }}.sessionManager)
}
func apiRequest<T: ImmutableMappable>(with parameters: ApiRequestParameters) -> Single<T> {
return rxRequest(with: parameters).map { $0.model }.asSingle()
}
}
{{ "\n" }}

View File

@ -11,15 +11,21 @@
{%- set funcName = utils.decapitalize(method.name) -%}
/// {{ method.description }}
func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ "\n " }}{%- endif -%}
{{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ "\n " }}{%- endif -%}
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil) -> Single<{{ method.responseType.type.typeName }}> {
let parameters = ApiRequestParameters(url: "{{ method.url }}",
{% if isStatic -%}
return shared.{{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyParamName }},{{ "\n " }}{%- endif -%}
encoding: encoding,
headers: headers)
{%- else %}
let parameters = ApiRequestParameters(url: "{{ method.url }}",
method: .{{ methodType }},
parameters: {% if hasBody -%}{{ bodyParamName }}.toJSON(){%- else -%}nil{%- endif -%},
encoding: encoding,
headers: headers)
return rxRequest(with: parameters).map { $0.model }.asSingle()
return apiRequest(with: parameters)
{%- endif %}
}