Skip to content

Commit a0a0c55

Browse files
committed
Add default overload for generic service methods
1 parent 8d6b4a4 commit a0a0c55

File tree

3 files changed

+47
-56
lines changed

3 files changed

+47
-56
lines changed

templates/swift/Sources/Models/Model.swift.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class {{ definition | modelType(spec) | raw }} {
1414
{%~ endfor %}
1515
{%~ if definition.additionalProperties %}
1616
/// Additional properties
17-
let data: T
17+
public let data: T
1818
{%~ endif %}
1919

2020
init(

templates/swift/Sources/Services/Service.swift.twig

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import {{spec.title | caseUcfirst}}Models
55

66
/// {{ service.description }}
77
open class {{ service.name | caseUcfirst }}: Service {
8+
89
{%~ for method in service.methods %}
910
///
1011
/// {{ method.title }}
1112
///
1213
{%~ if method.description %}
13-
{{ method.description | swiftComment }}
14+
{{~ method.description | swiftComment }}
1415
///
1516
{%~ endif %}
1617
{%~ for parameter in method.parameters.all %}
@@ -49,6 +50,7 @@ open class {{ service.name | caseUcfirst }}: Service {
4950
{%~ endfor %}
5051
]
5152
{%~ endif %}
53+
5254
{%~ if method.responseModel %}
5355
let converter: ([String: Any]) -> {{ method | returnType(spec) | raw }} = { dict in
5456
{%~ if method.responseModel == 'any' %}
@@ -57,6 +59,7 @@ open class {{ service.name | caseUcfirst }}: Service {
5759
return {{ spec.title | caseUcfirst}}Models.{{method.responseModel | caseUcfirst}}.from(map: dict)
5860
{%~ endif %}
5961
}
62+
6063
{%~ endif %}
6164
{%~ if 'multipart/form-data' in method.consumes %}
6265
{{~ include('swift/base/requests/file.twig') }}
@@ -65,53 +68,45 @@ open class {{ service.name | caseUcfirst }}: Service {
6568
{%~ endif %}
6669
{%~ endif %}
6770
}
71+
{%~ if method.responseModel | hasGenericType(spec) %}
6872

69-
{% endfor %}
73+
///
74+
/// {{ method.title }}
75+
///
76+
{%~ if method.description %}
77+
{{~ method.description | swiftComment }}
78+
///
79+
{%~ endif %}
80+
{%~ for parameter in method.parameters.all %}
81+
/// @param {{ parameter | typeName | raw}} {{ parameter.name | caseCamel }}
82+
{%~ endfor %}
83+
/// @throws Exception
84+
/// @return array
85+
///
86+
{%~ if method.type == "webAuth" %}
87+
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
88+
{%~ endif %}
89+
open func {{ method.name | caseCamel }}(
90+
{%~ for parameter in method.parameters.all %}
91+
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName | raw }}{% if not parameter.required %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes %},{% endif %}
7092

71-
{#{% for method in service.methods %}#}
72-
{# ///#}
73-
{# /// {{ method.title }}#}
74-
{# ///#}
75-
{#{% if method.description %}#}
76-
{#{{ method.description | swiftComment }}#}
77-
{# ///#}
78-
{#{% endif %}#}
79-
{#{% for parameter in method.parameters.all %}#}
80-
{# /// @param {{ parameter | typeName | raw}} {{ parameter.name | caseCamel }}#}
81-
{#{% endfor %}#}
82-
{# /// @throws Exception#}
83-
{# /// @return array#}
84-
{# ///#}
85-
{# @available(*, deprecated, message: "Use the async overload instead")#}
86-
{#{% if method.type == "webAuth" %}#}
87-
{# @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)#}
88-
{#{% endif %}#}
89-
{# open func {{ method.name | caseCamel }}(#}
90-
{#{% for parameter in method.parameters.all %}#}
91-
{# {{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName | raw }}{% if not parameter.required %}? = nil{% endif %},#}
92-
{#{% endfor %}#}
93-
{#{% if 'multipart/form-data' in method.consumes %}#}
94-
{# onProgress: ((UploadProgress) -> Void)? = nil,#}
95-
{#{% endif %}#}
96-
{# completion: ((Result<{{ _self.resultType(spec, method) }}, {{ spec.title | caseUcfirst}}Error>) -> Void)? = nil#}
97-
{# ) {#}
98-
{# Task {#}
99-
{# do {#}
100-
{# let result = try await {{ method.name | caseCamel }}(#}
101-
{#{% for parameter in method.parameters.all %}#}
102-
{# {{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last or 'multipart/form-data' in method.consumes %},{% endif %}#}
93+
{%~ endfor %}
94+
{%~ if 'multipart/form-data' in method.consumes %}
95+
onProgress: ((UploadProgress) -> Void)? = nil
96+
{%~ endif %}
97+
) async throws -> {{ method | returnType(spec, '[String: AnyCodable]') | raw }} {
98+
return try await {{ method.name | caseCamel }}(
99+
{%~ for parameter in method.parameters.all %}
100+
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last %},{% endif %}
103101

104-
{#{% endfor %}#}
105-
{#{% if 'multipart/form-data' in method.consumes %}#}
106-
{# onProgress: onProgress#}
107-
{#{% endif %}#}
108-
{# )#}
109-
{# completion?(.success(result))#}
110-
{# } catch {#}
111-
{# completion?(.failure(error as! {{ spec.title | caseUcfirst}}Error))#}
112-
{# }#}
113-
{# }#}
114-
{# }#}
102+
{%~ endfor %}
103+
{%~ if 'multipart/form-data' in method.consumes %}
104+
onProgress: onProgress
105+
{%~ endif %}
106+
)
107+
}
108+
{%~ endif %}
109+
110+
{% endfor %}
115111

116-
{#{% endfor %}#}
117-
}
112+
}

templates/swift/base/params.twig

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
{% if method.parameters.path -%} var
2-
{%- else -%} let
3-
{%- endif %} path: String = "{{ method.path }}"
1+
let path: String = "{{ method.path }}"
42
{%~ for parameter in method.parameters.path %}
5-
path = path.replacingOccurrences(
6-
of: "{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}",
7-
with: {{ parameter.name | caseCamel | escapeKeyword }}
8-
)
3+
.replacingOccurrences(of: "{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}", with: {{ parameter.name | caseCamel | escapeKeyword }})
94
{%~ endfor %}
5+
106
{%~ if method.parameters.query | merge(method.parameters.body) | length <= 0 %}
117
let params: [String: Any] = [:]
128
{%~ else %}
@@ -18,4 +14,4 @@
1814

1915
{%~ endfor %}
2016
]
21-
{%- endif %}
17+
{%~ endif %}

0 commit comments

Comments
 (0)