diff --git a/Swift/Methods.swift.twig b/Swift/Methods.swift.twig index ef4952c..eceb90f 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..2ef26f8 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -3,7 +3,23 @@ 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, + parameters: Parameters?, + requestEncoding: ParameterEncoding?, + requestHeaders: HTTPHeaders?) -> ApiRequestParameters + + {% for method in methods %} + {%- include 'blocks/method/method-declaration.twig' with { method: method, isStatic: false } -%} + {% endfor %} +} + +class {{ serviceName }}: NetworkService, {{ protocolName }} { static let apiBaseUrl = "{{ apiUrl }}" 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 %} }