From 6af656effddabe958b7ed1aafede3ff106a97f6c Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Wed, 4 Apr 2018 18:11:46 +0300 Subject: [PATCH] add static versions of methods, add base method for override request --- Swift/Methods.swift.twig | 14 ++++++++++++-- Swift/NetworkService.swift.twig | 5 +++++ Swift/blocks/method/method-func.twig | 12 +++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Swift/Methods.swift.twig b/Swift/Methods.swift.twig index fb9472f..ef4952c 100644 --- a/Swift/Methods.swift.twig +++ b/Swift/Methods.swift.twig @@ -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 %} } diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index 3938085..d55ea7b 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -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(with parameters: ApiRequestParameters) -> Single { + return rxRequest(with: parameters).map { $0.model }.asSingle() + } + } {{ "\n" }} \ No newline at end of file diff --git a/Swift/blocks/method/method-func.twig b/Swift/blocks/method/method-func.twig index 075ffee..e7b31ee 100644 --- a/Swift/blocks/method/method-func.twig +++ b/Swift/blocks/method/method-func.twig @@ -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 %} } \ No newline at end of file