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 %}
11
1
package {{ sdk .namespace | caseDot }}.services
12
2
13
3
import android.net.Uri
@@ -19,12 +9,13 @@ import androidx.activity.ComponentActivity
19
9
{% endif %}
20
10
import {{ sdk .namespace | caseDot }}.exceptions.{{ spec .title | caseUcfirst }}Exception
21
11
import okhttp3.Cookie
22
- import okhttp3.Response
23
12
{% if (service .features .webAuth or service .features .location ) %}
24
13
import okhttp3.HttpUrl
25
14
import okhttp3.HttpUrl.Companion.toHttpUrl
26
15
{% endif %}
27
16
import java.io.File
17
+ import kotlin.reflect.KClass
18
+ import kotlin.reflect.typeOf
28
19
29
20
class {{ service .name | caseUcfirst }} : Service {
30
21
@@ -34,34 +25,44 @@ class {{ service.name | caseUcfirst }} : Service {
34
25
/**
35
26
* {{ method .title }}
36
27
*
37
- {% if method .description %}
38
- {{ method .description | comment1 }}
28
+ * {{ method .description | raw | replace({" \n" : " " , " \r" : " " }) }}
39
29
*
40
- {% endif %}
41
- {% for parameter in method .parameters .all %}
30
+ {%~ for parameter in method .parameters .all %}
42
31
* @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 %}
46
36
*/
37
+ {%~ if method .parameters .all | reduce((carry , param ) => carry or not param .required ) %}
47
38
@JvmOverloads
39
+ {%~ endif %}
48
40
@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 %}
51
59
52
60
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 %}
63
64
)
64
- {% if method .type == ' webAuth' %}
65
+ {%~ if method .type == ' webAuth' %}
65
66
val query = mutableListOf<String >()
66
67
params.forEach {
67
68
when (it.value) {
@@ -104,60 +105,64 @@ class {{ service.name | caseUcfirst }} : Service {
104
105
listOf(cookie)
105
106
)
106
107
}
107
- {% elseif method .type == ' location' %}
108
+ {%~ elseif method .type == ' location' %}
108
109
return client.call(
109
110
"{{ method .method | caseUpper }}",
110
111
path,
111
112
params = params,
112
- responseType = {{ _self. resultType ( sdk .namespace , method ) }}::class.java
113
+ responseType = {{ method | returnType( spec , sdk .namespace | caseDot ) | raw }}::class.java
113
114
)
114
- {% else %}
115
+ {%~ else %}
115
116
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 %}
117
120
)
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' %}
121
124
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 %}
125
128
}
126
- {% endif %}
127
- {% if ' multipart/form-data' in method .consumes %}
129
+ {%~ endif %}
130
+ {%~ if ' multipart/form-data' in method .consumes %}
128
131
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 %}
129
132
130
- {% for parameter in method .parameters .all %}
131
- {% if parameter .type == ' file' %}
133
+ {%~ for parameter in method .parameters .all %}
134
+ {%~ if parameter .type == ' file' %}
132
135
val paramName = "{{ parameter .name }}"
133
- {% endif %}
134
- {% endfor %}
136
+ {%~ endif %}
137
+ {%~ endfor %}
135
138
return client.chunkedUpload(
136
139
path,
137
140
headers,
138
141
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 %}
141
144
converter,
142
- {% endif %}
145
+ {%~ endif %}
143
146
paramName,
144
147
idParamName,
145
148
onProgress,
146
149
)
147
- {% else %}
150
+ {%~ else %}
148
151
return client.call(
149
152
"{{ method .method | caseUpper }}",
150
153
path,
151
154
headers,
152
155
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 %}
155
162
converter,
156
- {% endif %}
163
+ {%~ endif %}
157
164
)
158
- {% endif %}
159
- {% endif %}
165
+ {%~ endif %}
166
+ {%~ endif %}
160
167
}
161
-
162
- {% endfor %}
163
168
}
0 commit comments