forked from KyoheiG3/grpc-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.swift
137 lines (129 loc) · 6.99 KB
/
client.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//-{% for service in file.services %}
//-{% for method in service.methods %}
//-{% if method|methodIsUnary %}
//-{% include "client-call-unary.swift" %}
//-{% endif %}
//-{% if method|methodIsServerStreaming %}
//-{% include "client-call-serverstreaming.swift" %}
//-{% endif %}
//-{% if method|methodIsClientStreaming %}
//-{% include "client-call-clientstreaming.swift" %}
//-{% endif %}
//-{% if method|methodIsBidiStreaming %}
//-{% include "client-call-bidistreaming.swift" %}
//-{% endif %}
//-{% endfor %}
/// Instantiate {{ .|serviceclass:file,service }}Client, then call methods of this protocol to make API calls.
{{ access }} protocol {{ .|serviceclass:file,service }}: ServiceClient {
//-{% for method in service.methods %}
//-{% if method|methodIsUnary %}
/// Synchronous. Unary.
func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }}
/// Asynchronous. Unary.
func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }}
//-{% endif %}
//-{% if method|methodIsServerStreaming %}
/// Asynchronous. Server-streaming.
/// Send the initial message.
/// Use methods on the returned object to get streamed responses.
func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
//-{% endif %}
//-{% if method|methodIsClientStreaming %}
/// Asynchronous. Client-streaming.
/// Use methods on the returned object to stream messages and
/// to close the connection and wait for a final response.
func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
//-{% endif %}
//-{% if method|methodIsBidiStreaming %}
/// Asynchronous. Bidirectional-streaming.
/// Use methods on the returned object to stream messages,
/// to wait for replies, and to close the connection.
func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
//-{% endif %}
//-{% endfor %}
}
{{ access }} final class {{ .|serviceclass:file,service }}Client: ServiceClientBase, {{ .|serviceclass:file,service }} {
//-{% for method in service.methods %}
//-{% if method|methodIsUnary %}
/// Synchronous. Unary.
{{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }} {
return try {{ .|call:file,service,method }}Base(channel)
.run(request: request, metadata: metadata)
}
/// Asynchronous. Unary.
{{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }} {
return try {{ .|call:file,service,method }}Base(channel)
.start(request: request, metadata: metadata, completion: completion)
}
//-{% endif %}
//-{% if method|methodIsServerStreaming %}
/// Asynchronous. Server-streaming.
/// Send the initial message.
/// Use methods on the returned object to get streamed responses.
{{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
return try {{ .|call:file,service,method }}Base(channel)
.start(request: request, metadata: metadata, completion: completion)
}
//-{% endif %}
//-{% if method|methodIsClientStreaming %}
/// Asynchronous. Client-streaming.
/// Use methods on the returned object to stream messages and
/// to close the connection and wait for a final response.
{{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
return try {{ .|call:file,service,method }}Base(channel)
.start(metadata: metadata, completion: completion)
}
//-{% endif %}
//-{% if method|methodIsBidiStreaming %}
/// Asynchronous. Bidirectional-streaming.
/// Use methods on the returned object to stream messages,
/// to wait for replies, and to close the connection.
{{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
return try {{ .|call:file,service,method }}Base(channel)
.start(metadata: metadata, completion: completion)
}
//-{% endif %}
//-{% endfor %}
}
//-{% if generateTestStubs %}
class {{ .|serviceclass:file,service }}TestStub: ServiceClientTestStubBase, {{ .|serviceclass:file,service }} {
//-{% for method in service.methods %}
//-{% if method|methodIsUnary %}
var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []
var {{ method|methodDescriptorName|lowercase }}Responses: [{{ method|output }}] = []
func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }} {
{{ method|methodDescriptorName|lowercase }}Requests.append(request)
defer { {{ method|methodDescriptorName|lowercase }}Responses.removeFirst() }
return {{ method|methodDescriptorName|lowercase }}Responses.first!
}
func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }} {
fatalError("not implemented")
}
//-{% endif %}
//-{% if method|methodIsServerStreaming %}
var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []
var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
{{ method|methodDescriptorName|lowercase }}Requests.append(request)
defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
return {{ method|methodDescriptorName|lowercase }}Calls.first!
}
//-{% endif %}
//-{% if method|methodIsClientStreaming %}
var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
return {{ method|methodDescriptorName|lowercase }}Calls.first!
}
//-{% endif %}
//-{% if method|methodIsBidiStreaming %}
var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
return {{ method|methodDescriptorName|lowercase }}Calls.first!
}
//-{% endif %}
//-{% endfor %}
}
//-{% endif %}
//-{% endfor %}