From 0c9cd2b9f7c1aed3009980e7cfb3b40b7fd98ec4 Mon Sep 17 00:00:00 2001 From: Madhas Date: Thu, 20 Dec 2018 14:44:11 +0300 Subject: [PATCH 1/7] add generated network protocol generate methods in extension of the protocol --- Swift/Methods.swift.twig | 8 ++++---- Swift/NetworkService.swift.twig | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Swift/Methods.swift.twig b/Swift/Methods.swift.twig index ef4952c..d6541d1 100644 --- a/Swift/Methods.swift.twig +++ b/Swift/Methods.swift.twig @@ -2,9 +2,9 @@ import LeadKit import RxSwift import Alamofire -{% set serviceName = concat(networkServiceName, "NetworkService") -%} +{% set protocolName = concat(networkServiceName, "NetworkProtocol") -%} -extension {{ serviceName }} { +extension {{ protocolName }} { {% for method in methods %} {%- include 'blocks/method/method-func.twig' with { method: method, isStatic: false } %} @@ -12,11 +12,11 @@ extension {{ serviceName }} { {% endfor %} } -extension Singleton where Self: {{ serviceName }} { +extension Singleton where Self: {{ protocolName }} { {% for method in methods %} {%- include 'blocks/method/method-func.twig' with { method: method, isStatic: true } %} {% endfor %} } -{{ "\n" }} \ No newline at end of file +{{ "\n" }} diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index b0949bf..8648afe 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -1,9 +1,29 @@ +{%- import "blocks/method/method-func.twig" as methodUtils -%} + import LeadKit import RxSwift import Alamofire {% set serviceName = concat(networkServiceName, "NetworkService") -%} -class {{ serviceName }}: NetworkService { +{% set protocolName = concat(networkServiceName, "NetworkProtocol") -%} + +protocol {{ protocolName }} { + + func apiRequest(with parameters: ApiRequestParameters, decoder: JSONDecoder) -> Single + func apiRequestParameters(relativeUrl: String, + method: HTTPMethod = .get, + parameters: Parameters? = nil, + requestEncoding: ParameterEncoding? = nil, + requestHeaders: HTTPHeaders? = nil) -> ApiRequestParameters + + {% for method in methods %} + {{ isStatic ? "static " : "" }}func {{ methodUtils.funcName }}({%- if methodUtils.hasBody -%}{{ methodUtils.bodyParamName }}: {{ methodUtils.bodyTypeName }},{{ "\n " }}{%- endif -%} + requestEncoding: ParameterEncoding? = nil, + requestHeaders: HTTPHeaders? = nil) -> Single<{{ method.responseType.type.typeName }}> + {% endfor %} +} + +class {{ serviceName }}: NetworkService, {{ protocolName }} { static let apiBaseUrl = "{{ apiUrl }}" From 6ad74c3e00b19c0e4c160d0fe6653d3b02726a4f Mon Sep 17 00:00:00 2001 From: Madhas Date: Thu, 20 Dec 2018 15:53:10 +0300 Subject: [PATCH 2/7] remove default arguments --- Swift/NetworkService.swift.twig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index 8648afe..749c173 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -11,10 +11,10 @@ protocol {{ protocolName }} { func apiRequest(with parameters: ApiRequestParameters, decoder: JSONDecoder) -> Single func apiRequestParameters(relativeUrl: String, - method: HTTPMethod = .get, - parameters: Parameters? = nil, - requestEncoding: ParameterEncoding? = nil, - requestHeaders: HTTPHeaders? = nil) -> ApiRequestParameters + method: HTTPMethod, + parameters: Parameters? + requestEncoding: ParameterEncoding?, + requestHeaders: HTTPHeaders?) -> ApiRequestParameters {% for method in methods %} {{ isStatic ? "static " : "" }}func {{ methodUtils.funcName }}({%- if methodUtils.hasBody -%}{{ methodUtils.bodyParamName }}: {{ methodUtils.bodyTypeName }},{{ "\n " }}{%- endif -%} From cd8c08e29b10093cb5056ee9c88c02c693b77257 Mon Sep 17 00:00:00 2001 From: Madhas Date: Thu, 20 Dec 2018 16:04:46 +0300 Subject: [PATCH 3/7] fix --- Swift/NetworkService.swift.twig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index 749c173..99c660a 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -12,12 +12,12 @@ protocol {{ protocolName }} { func apiRequest(with parameters: ApiRequestParameters, decoder: JSONDecoder) -> Single func apiRequestParameters(relativeUrl: String, method: HTTPMethod, - parameters: Parameters? + parameters: Parameters?, requestEncoding: ParameterEncoding?, requestHeaders: HTTPHeaders?) -> ApiRequestParameters - {% for method in methods %} - {{ isStatic ? "static " : "" }}func {{ methodUtils.funcName }}({%- if methodUtils.hasBody -%}{{ methodUtils.bodyParamName }}: {{ methodUtils.bodyTypeName }},{{ "\n " }}{%- endif -%} + {% for method in methodUtils.methods %} + {{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ "\n " }}{%- endif -%} requestEncoding: ParameterEncoding? = nil, requestHeaders: HTTPHeaders? = nil) -> Single<{{ method.responseType.type.typeName }}> {% endfor %} From 254516312acffbc01839f01fe347dddcd1e195e4 Mon Sep 17 00:00:00 2001 From: Madhas Date: Thu, 20 Dec 2018 16:19:03 +0300 Subject: [PATCH 4/7] method declaration --- Swift/Methods.swift.twig | 2 +- Swift/NetworkService.swift.twig | 8 ++------ Swift/blocks/method/method-declaration.twig | 12 ++++++++++++ Swift/blocks/method/method-func.twig | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 Swift/blocks/method/method-declaration.twig diff --git a/Swift/Methods.swift.twig b/Swift/Methods.swift.twig index d6541d1..eceb90f 100644 --- a/Swift/Methods.swift.twig +++ b/Swift/Methods.swift.twig @@ -16,7 +16,7 @@ extension Singleton where Self: {{ protocolName }} { {% for method in methods %} {%- include 'blocks/method/method-func.twig' with { method: method, isStatic: true } %} - + {% endfor %} } {{ "\n" }} diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index 99c660a..bdebb7b 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -1,5 +1,3 @@ -{%- import "blocks/method/method-func.twig" as methodUtils -%} - import LeadKit import RxSwift import Alamofire @@ -16,10 +14,8 @@ protocol {{ protocolName }} { requestEncoding: ParameterEncoding?, requestHeaders: HTTPHeaders?) -> ApiRequestParameters - {% for method in methodUtils.methods %} - {{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ "\n " }}{%- endif -%} - requestEncoding: ParameterEncoding? = nil, - requestHeaders: HTTPHeaders? = nil) -> Single<{{ method.responseType.type.typeName }}> + {% for method in methods %} + {%- include 'blocks/method/method-declaration.twig' with { method: method, isStatic: false } %} {% endfor %} } diff --git a/Swift/blocks/method/method-declaration.twig b/Swift/blocks/method/method-declaration.twig new file mode 100644 index 0000000..d963da6 --- /dev/null +++ b/Swift/blocks/method/method-declaration.twig @@ -0,0 +1,12 @@ +{%- import '../../macroses/common.utils.twig' as utils -%} + +{%- if (method.bodyType is not null) -%} + {%- set bodyParamName = utils.decapitalize(method.bodyType.type.typeName) -%} + {%- set bodyTypeName = method.bodyType.type.typeName -%} + + {%- set hasBody = true -%} +{%- endif -%} + +{%- set funcName = utils.decapitalize(method.name) -%} + +{{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ " " }}{%- endif -%}requestEncoding: ParameterEncoding?, requestHeaders: HTTPHeaders?) -> Single<{{ method.responseType.type.typeName }}> diff --git a/Swift/blocks/method/method-func.twig b/Swift/blocks/method/method-func.twig index 018dc50..f7d8c07 100644 --- a/Swift/blocks/method/method-func.twig +++ b/Swift/blocks/method/method-func.twig @@ -26,6 +26,6 @@ requestEncoding: requestEncoding, requestHeaders: requestHeaders) - return apiRequest(with: parameters) + return apiRequest(with: parameters, decoder: JSONDecoder()) {%- endif %} } From 3e4da17a8371b90063a00f9d89bacade5a5d054d Mon Sep 17 00:00:00 2001 From: Madhas Date: Thu, 20 Dec 2018 16:47:21 +0300 Subject: [PATCH 5/7] fix --- Swift/NetworkService.swift.twig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index bdebb7b..eeecdc8 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -2,6 +2,8 @@ import LeadKit import RxSwift import Alamofire +{% import "Methods.swift.twig" as Methods %} + {% set serviceName = concat(networkServiceName, "NetworkService") -%} {% set protocolName = concat(networkServiceName, "NetworkProtocol") -%} @@ -14,7 +16,7 @@ protocol {{ protocolName }} { requestEncoding: ParameterEncoding?, requestHeaders: HTTPHeaders?) -> ApiRequestParameters - {% for method in methods %} + {% for method in Methods.methods %} {%- include 'blocks/method/method-declaration.twig' with { method: method, isStatic: false } %} {% endfor %} } From 0f8d1fc2758c0943fd508f050bdead025b692aa7 Mon Sep 17 00:00:00 2001 From: Madhas Date: Thu, 20 Dec 2018 17:43:46 +0300 Subject: [PATCH 6/7] fix --- Swift/NetworkService.swift.twig | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index eeecdc8..bdebb7b 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -2,8 +2,6 @@ import LeadKit import RxSwift import Alamofire -{% import "Methods.swift.twig" as Methods %} - {% set serviceName = concat(networkServiceName, "NetworkService") -%} {% set protocolName = concat(networkServiceName, "NetworkProtocol") -%} @@ -16,7 +14,7 @@ protocol {{ protocolName }} { requestEncoding: ParameterEncoding?, requestHeaders: HTTPHeaders?) -> ApiRequestParameters - {% for method in Methods.methods %} + {% for method in methods %} {%- include 'blocks/method/method-declaration.twig' with { method: method, isStatic: false } %} {% endfor %} } From ae58fc94e259fcb6e31361f65ae483eac8322365 Mon Sep 17 00:00:00 2001 From: Madhas Date: Thu, 20 Dec 2018 17:52:03 +0300 Subject: [PATCH 7/7] formatting --- Swift/NetworkService.swift.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index bdebb7b..2ef26f8 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -15,7 +15,7 @@ protocol {{ protocolName }} { requestHeaders: HTTPHeaders?) -> ApiRequestParameters {% for method in methods %} - {%- include 'blocks/method/method-declaration.twig' with { method: method, isStatic: false } %} + {%- include 'blocks/method/method-declaration.twig' with { method: method, isStatic: false } -%} {% endfor %} }