Skip to content

Commit de6de48

Browse files
committed
Refactor service template for generics and improve formatting
1 parent 57d291d commit de6de48

File tree

1 file changed

+63
-58
lines changed

1 file changed

+63
-58
lines changed
Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
{% macro parameter(parameter) %}{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{% if not parameter.required %}? = null{% endif %}{% endmacro %}
2-
{% macro method_parameters(parameters, consumes) %}
3-
{% if parameters.all|length > 0 %}{% for parameter in parameters.all %}{{ '\n\t\t' }}{{ _self.parameter(parameter) }}{% if not loop.last %}{{ ',' }}{% endif %}{% endfor %}{% if 'multipart/form-data' in consumes %}, onProgress: ((UploadProgress) -> Unit)? = null{% endif %}{% endif %}
4-
{% endmacro %}
5-
{% macro methodNeedsSecurityParameters(method) %}
6-
{% if (method.type == "webAuth" or method.type == "location") and method.security|length > 0 %}{{ true }}{% else %}{{false}}{% endif %}
7-
{% endmacro %}
8-
{% macro resultType(namespace, method) %}
9-
{% if method.type == "webAuth" %}Bool{% elseif method.type == "location" %}ByteArray{% elseif not method.responseModel or method.responseModel == 'any' %}Any{% else %}{{ namespace | caseDot}}.models.{{method.responseModel | caseUcfirst}}{% endif %}
10-
{% endmacro %}
111
package {{ sdk.namespace | caseDot }}.services
122

133
import android.net.Uri
@@ -19,12 +9,13 @@ import androidx.activity.ComponentActivity
199
{% endif %}
2010
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
2111
import okhttp3.Cookie
22-
import okhttp3.Response
2312
{% if (service.features.webAuth or service.features.location) %}
2413
import okhttp3.HttpUrl
2514
import okhttp3.HttpUrl.Companion.toHttpUrl
2615
{% endif %}
2716
import java.io.File
17+
import kotlin.reflect.KClass
18+
import kotlin.reflect.typeOf
2819

2920
class {{ service.name | caseUcfirst }} : Service {
3021

@@ -34,34 +25,44 @@ class {{ service.name | caseUcfirst }} : Service {
3425
/**
3526
* {{ method.title }}
3627
*
37-
{% if method.description %}
38-
{{ method.description|comment1 }}
28+
* {{ method.description | raw | replace({"\n": "", "\r": ""}) }}
3929
*
40-
{% endif %}
41-
{% for parameter in method.parameters.all %}
30+
{%~ for parameter in method.parameters.all %}
4231
* @param {{ parameter.name | caseCamel }} {{ parameter.description | raw }}
43-
{% endfor %}
44-
* {% if method.type != "webAuth" %}@return [{{ _self.resultType(sdk.namespace, method) }}]{% endif %}
45-
32+
{%~ endfor %}
33+
{%~ if method.type != "webAuth" %}
34+
* @return [{{ method | returnType(spec, sdk.namespace | caseDot) | raw }}]
35+
{%~ endif %}
4636
*/
37+
{%~ if method.parameters.all | reduce((carry, param) => carry or not param.required) %}
4738
@JvmOverloads
39+
{%~ endif %}
4840
@Throws({{ spec.title | caseUcfirst }}Exception::class)
49-
suspend fun {{ method.name | caseCamel }}({% if method.type == "webAuth" %}{{ '\n\t\t' }}activity: ComponentActivity{% if method.parameters.all | length > 0 %}, {% endif %}{% endif %}{{ _self.method_parameters(method.parameters, method.consumes) }}{% if method.parameters.all|length > 0 %}{{ '\n\t' }}{% endif %}){% if method.type != "webAuth" %}: {{ _self.resultType(sdk.namespace, method) }}{% endif %} {
50-
val path = "{{ method.path }}"{% for parameter in method.parameters.path %}.replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel }}){% endfor %}
41+
suspend fun {% if method.responseModel | hasGenericType(spec) %}{{ '<T>' | raw }} {% endif %}{{ method.name | caseCamel }}(
42+
{%~ if method.type == "webAuth" %}
43+
activity: ComponentActivity,
44+
{%~ endif %}
45+
{%~ for parameter in method.parameters.all %}
46+
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required %}? = null{% endif %},
47+
{%~ endfor %}
48+
{%~ if method.responseModel | hasGenericType(spec) %}
49+
nestedType: Class<T>,
50+
{%~ endif %}
51+
{%~ if 'multipart/form-data' in method.consumes %}
52+
onProgress: ((UploadProgress) -> Unit)? = null
53+
{%~ endif %}
54+
){% if method.type != "webAuth" %}: {{ method | returnType(spec, sdk.namespace | caseDot) | raw }}{% endif %} {
55+
val path = "{{ method.path }}"
56+
{%~ for parameter in method.parameters.path %}
57+
.replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel }})
58+
{%~ endfor %}
5159

5260
val params = mutableMapOf<String, Any?>(
53-
{% for parameter in method.parameters.query | merge(method.parameters.body) %}
54-
"{{ parameter.name }}" to {{ parameter.name | caseCamel }}{% if not loop.last or _self.methodNeedsSecurityParameters(method) %},{% endif %}
55-
56-
{% endfor %}
57-
{% if _self.methodNeedsSecurityParameters(method) %}
58-
{% for node in method.security %}
59-
{% for key,header in node|keys %}
60-
"{{header|caseLower}}" to client.config["{{header|caseLower}}"]{% if not loop.last %},{% endif %}
61-
62-
{% endfor %}{% endfor %}{% endif %}
61+
{%~ for parameter in method.parameters.query | merge(method.parameters.body) %}
62+
"{{ parameter.name }}" to {{ parameter.name | caseCamel }},
63+
{%~ endfor %}
6364
)
64-
{% if method.type == 'webAuth' %}
65+
{%~ if method.type == 'webAuth' %}
6566
val query = mutableListOf<String>()
6667
params.forEach {
6768
when (it.value) {
@@ -104,60 +105,64 @@ class {{ service.name | caseUcfirst }} : Service {
104105
listOf(cookie)
105106
)
106107
}
107-
{% elseif method.type == 'location' %}
108+
{%~ elseif method.type == 'location' %}
108109
return client.call(
109110
"{{ method.method | caseUpper }}",
110111
path,
111112
params = params,
112-
responseType = {{ _self.resultType(sdk.namespace, method) }}::class.java
113+
responseType = {{ method | returnType(spec, sdk.namespace | caseDot) | raw }}::class.java
113114
)
114-
{% else %}
115+
{%~ else %}
115116
val headers = mutableMapOf(
116-
{{ method.headers|map((header, key) => " \"#{key}\" to \"#{header}\"")|join(',\n')|raw }}
117+
{%~ for key, header in method.headers %}
118+
"{{ key }}" to "{{ header }}",
119+
{%~ endfor %}
117120
)
118-
{% if method.responseModel %}
119-
val converter: (Map<String, Any>) -> {{ _self.resultType(sdk.namespace, method) }} = {
120-
{% if method.responseModel == 'any' %}
121+
{%~ if method.responseModel %}
122+
val converter: (Map<String, Any>) -> {{ method | returnType(spec, sdk.namespace | caseDot) | raw }} = {
123+
{%~ if method.responseModel == 'any' %}
121124
it
122-
{% else %}
123-
{{ _self.resultType(sdk.namespace, method) }}.from(map = it)
124-
{% endif %}
125+
{%~ else %}
126+
{{ sdk.namespace | caseDot}}.models.{{ method.responseModel | caseUcfirst }}.from(map = it{% if method.responseModel | hasGenericType(spec) %}, nestedType{% endif %})
127+
{%~ endif %}
125128
}
126-
{% endif %}
127-
{% if 'multipart/form-data' in method.consumes %}
129+
{%~ endif %}
130+
{%~ if 'multipart/form-data' in method.consumes %}
128131
val idParamName: String? = {% if method.parameters.all | filter(p => p.isUploadID) | length > 0 %}{% for parameter in method.parameters.all | filter(parameter => parameter.isUploadID) %}"{{ parameter.name }}"{% endfor %}{% else %}null{% endif %}
129132

130-
{% for parameter in method.parameters.all %}
131-
{% if parameter.type == 'file' %}
133+
{%~ for parameter in method.parameters.all %}
134+
{%~ if parameter.type == 'file' %}
132135
val paramName = "{{ parameter.name }}"
133-
{% endif %}
134-
{% endfor %}
136+
{%~ endif %}
137+
{%~ endfor %}
135138
return client.chunkedUpload(
136139
path,
137140
headers,
138141
params,
139-
responseType = {{ _self.resultType(sdk.namespace, method) }}::class.java,
140-
{% if method.responseModel %}
142+
responseType = {{ method | returnType(spec, sdk.namespace | caseDot) | raw }}::class.java,
143+
{%~ if method.responseModel %}
141144
converter,
142-
{% endif %}
145+
{%~ endif %}
143146
paramName,
144147
idParamName,
145148
onProgress,
146149
)
147-
{% else %}
150+
{%~ else %}
148151
return client.call(
149152
"{{ method.method | caseUpper }}",
150153
path,
151154
headers,
152155
params,
153-
responseType = {{ _self.resultType(sdk.namespace, method) }}::class.java,
154-
{% if method.responseModel %}
156+
{%~ if method.responseModel | hasGenericType(spec) %}
157+
responseType = (typeOf<{{ method | returnType(spec, sdk.namespace | caseDot) | raw }}>().classifier!! as KClass<{{ method | returnType(spec, sdk.namespace | caseDot) | raw }}>).java,
158+
{%~ else %}
159+
responseType = {{ method | returnType(spec, sdk.namespace | caseDot) | raw }}::class.java,
160+
{%~ endif %}
161+
{%~ if method.responseModel %}
155162
converter,
156-
{% endif %}
163+
{%~ endif %}
157164
)
158-
{% endif %}
159-
{% endif %}
165+
{%~ endif %}
166+
{%~ endif %}
160167
}
161-
162-
{% endfor %}
163168
}

0 commit comments

Comments
 (0)