Merge pull request #56 from TouchInstinct/feature/network_protocol

Feature/network protocol
This commit is contained in:
Andrey Ovsyannikov 2018-12-20 19:04:06 +03:00 committed by GitHub
commit 2abb3050e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 7 deletions

View File

@ -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" }}
{{ "\n" }}

View File

@ -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<T: Decodable>(with parameters: ApiRequestParameters, decoder: JSONDecoder) -> Single<T>
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 }}"

View File

@ -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 }}>

View File

@ -26,6 +26,6 @@
requestEncoding: requestEncoding,
requestHeaders: requestHeaders)
return apiRequest(with: parameters)
return apiRequest(with: parameters, decoder: JSONDecoder())
{%- endif %}
}