Skip to content

Commit 0e24ea7

Browse files
committed
swift
1 parent c71e8ec commit 0e24ea7

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ import NIO
1010
import {{spec.title | caseUcfirst}}Models
1111

1212
open class {{ service.name | caseUcfirst }}: Service {
13+
{% if service.globalParams | length %}
14+
{% for parameter in service.globalParams %}
15+
var {{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.type | typeName }}{% if not parameter.required %}?{% endif %}
16+
{% endfor %}
17+
18+
19+
public init(_ client: Client, {% for parameter in service.globalParams %}_ {{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.type | typeName }}{% if not parameter.required %}? = nil{% endif %}{% if not loop.last %}, {% endif %}{% endfor %})
20+
{
21+
{% for parameter in service.globalParams %}
22+
self.{{ parameter.name | caseCamel | escapeKeyword }} = {{ parameter.name | caseCamel | escapeKeyword }}
23+
{% endfor %}
24+
super.init(client)
25+
}
26+
27+
{% endif %}
1328
{% for method in service.methods %}
1429
///
1530
/// {{ method.title }}
@@ -28,7 +43,7 @@ open class {{ service.name | caseUcfirst }}: Service {
2843
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
2944
{% endif %}
3045
open func {{ method.name | caseCamel }}(
31-
{% for parameter in method.parameters.all %}
46+
{% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}
3247
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.type | typeName | raw }}{% if not parameter.required %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes %},{% endif %}
3348

3449
{% endfor %}
@@ -40,7 +55,7 @@ open class {{ service.name | caseUcfirst }}: Service {
4055
{% for parameter in method.parameters.path %}
4156
path = path.replacingOccurrences(
4257
of: "{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}",
43-
with: {% if method.parameters.path %}{{ parameter.name | caseCamel | escapeKeyword }}{% else %}""{% endif %}
58+
with: {% if method.parameters.path %}{% if parameter.isGlobal %}self.{% endif %}{{ parameter.name | caseCamel | escapeKeyword }}{% else %}""{% endif %}
4459

4560
)
4661
{% endfor %}
@@ -50,7 +65,7 @@ open class {{ service.name | caseUcfirst }}: Service {
5065
let params: [String: Any?] = [:]
5166
{% endif %}
5267
{% for parameter in method.parameters.query | merge(method.parameters.body) %}
53-
"{{ parameter.name }}": {{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last or _self.methodNeedsSecurityParameters(method) %},{% endif %}
68+
"{{ parameter.name }}": {% if parameter.isGlobal %}self.{% endif %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last or _self.methodNeedsSecurityParameters(method) %},{% endif %}
5469

5570
{% endfor %}
5671
{% if _self.methodNeedsSecurityParameters(method) %}
@@ -138,7 +153,7 @@ open class {{ service.name | caseUcfirst }}: Service {
138153
{{ method.description | swiftComment }}
139154
///
140155
{% endif %}
141-
{% for parameter in method.parameters.all %}
156+
{% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}
142157
/// @param {{ parameter.type | typeName | raw}} {{ parameter.name | caseCamel }}
143158
{% endfor %}
144159
/// @throws Exception
@@ -149,7 +164,7 @@ open class {{ service.name | caseUcfirst }}: Service {
149164
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
150165
{% endif %}
151166
open func {{ method.name | caseCamel }}(
152-
{% for parameter in method.parameters.all %}
167+
{% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}
153168
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.type | typeName | raw }}{% if not parameter.required %}? = nil{% endif %},
154169
{% endfor %}
155170
{% if 'multipart/form-data' in method.consumes %}
@@ -160,7 +175,7 @@ open class {{ service.name | caseUcfirst }}: Service {
160175
Task {
161176
do {
162177
let result = try await {{ method.name | caseCamel }}(
163-
{% for parameter in method.parameters.all %}
178+
{% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}
164179
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last or 'multipart/form-data' in method.consumes %},{% endif %}
165180

166181
{% endfor %}

tests/languages/swift-client/Tests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Tests: XCTestCase {
2525
.addHeader(key: "Origin", value: "http://localhost")
2626
.setSelfSigned()
2727

28-
let foo = Foo(client)
28+
let foo = Foo(client, "string")
2929
let bar = Bar(client)
3030
let general = General(client)
3131
let realtime = Realtime(client)
@@ -41,19 +41,19 @@ class Tests: XCTestCase {
4141
var mock: Mock
4242

4343
// Foo Tests
44-
mock = try await foo.get(x: "string", y: 123, z: ["string in array"])
44+
mock = try await foo.get(y: 123, z: ["string in array"])
4545
print(mock.result)
4646

47-
mock = try await foo.post(x: "string", y: 123, z: ["string in array"])
47+
mock = try await foo.post(y: 123, z: ["string in array"])
4848
print(mock.result)
4949

50-
mock = try await foo.put(x: "string", y: 123, z: ["string in array"])
50+
mock = try await foo.put(y: 123, z: ["string in array"])
5151
print(mock.result)
5252

53-
mock = try await foo.patch(x: "string", y: 123, z: ["string in array"])
53+
mock = try await foo.patch(y: 123, z: ["string in array"])
5454
print(mock.result)
5555

56-
mock = try await foo.delete(x: "string", y: 123, z: ["string in array"])
56+
mock = try await foo.delete(y: 123, z: ["string in array"])
5757
print(mock.result)
5858

5959

tests/languages/swift-server/Tests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@ class Tests: XCTestCase {
2424
.addHeader(key: "Origin", value: "http://localhost")
2525
.setSelfSigned()
2626

27-
let foo = Foo(client)
27+
let foo = Foo(client, "string")
2828
let bar = Bar(client)
2929
let general = General(client)
3030

3131
var mock: Mock
3232

3333
// Foo Tests
34-
mock = try await foo.get(x: "string", y: 123, z: ["string in array"])
34+
mock = try await foo.get(y: 123, z: ["string in array"])
3535
print(mock.result)
3636

37-
mock = try await foo.post(x: "string", y: 123, z: ["string in array"])
37+
mock = try await foo.post(y: 123, z: ["string in array"])
3838
print(mock.result)
3939

40-
mock = try await foo.put(x: "string", y: 123, z: ["string in array"])
40+
mock = try await foo.put(y: 123, z: ["string in array"])
4141
print(mock.result)
4242

43-
mock = try await foo.patch(x: "string", y: 123, z: ["string in array"])
43+
mock = try await foo.patch(y: 123, z: ["string in array"])
4444
print(mock.result)
4545

46-
mock = try await foo.delete(x: "string", y: 123, z: ["string in array"])
46+
mock = try await foo.delete(y: 123, z: ["string in array"])
4747
print(mock.result)
4848

4949

0 commit comments

Comments
 (0)