-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClient.swift
More file actions
192 lines (181 loc) · 7.23 KB
/
Copy pathClient.swift
File metadata and controls
192 lines (181 loc) · 7.23 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import Foundation
import GraphQL
/// Client is an open-ended implementation of the client side of the protocol. It parses and adds callbacks for each type of server respose.
public actor Client<InitPayload: Equatable & Codable> {
let messenger: Messenger
let onConnectionError: (ConnectionErrorResponse, Client) async throws -> Void
let onConnectionAck: (ConnectionAckResponse, Client) async throws -> Void
let onConnectionKeepAlive: (ConnectionKeepAliveResponse, Client) async throws -> Void
let onData: (DataResponse, Client) async throws -> Void
let onError: (ErrorResponse, Client) async throws -> Void
let onComplete: (CompleteResponse, Client) async throws -> Void
let encoder = GraphQLJSONEncoder()
let decoder = JSONDecoder()
/// Create a new client.
///
/// - Parameters:
/// - messenger: The messenger to bind the client to.
/// - onConnectionError: The callback run on receipt of a `connection_error` message
/// - onConnectionAck: The callback run on receipt of a `connection_ack` message
/// - onConnectionKeepAlive: The callback run on receipt of a `connection_ka` message
/// - onData: The callback run on receipt of a `data` message
/// - onError: The callback run on receipt of an `error` message
/// - onComplete: The callback run on receipt of a `complete` message
public init(
messenger: Messenger,
onConnectionError: @escaping (ConnectionErrorResponse, Client) async throws -> Void = {
_,
_ in
},
onConnectionAck: @escaping (ConnectionAckResponse, Client) async throws -> Void = { _, _ in
},
onConnectionKeepAlive:
@escaping (ConnectionKeepAliveResponse, Client) async throws -> Void = { _, _ in },
onData: @escaping (DataResponse, Client) async throws -> Void = { _, _ in },
onError: @escaping (ErrorResponse, Client) async throws -> Void = { _, _ in },
onComplete: @escaping (CompleteResponse, Client) async throws -> Void = { _, _ in }
) {
self.messenger = messenger
self.onConnectionError = onConnectionError
self.onConnectionAck = onConnectionAck
self.onConnectionKeepAlive = onConnectionKeepAlive
self.onData = onData
self.onError = onError
self.onComplete = onComplete
}
/// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
/// - Parameter incoming: The server message sequence that the client should react to.
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws
where A.Element == Data {
for try await message in incoming {
try await respond(to: message)
}
}
private func respond(to message: Data) async throws {
let response: Response
do {
response = try decoder.decode(Response.self, from: message)
} catch {
try await messenger.error(.noType())
return
}
switch response.type {
case .GQL_CONNECTION_ERROR:
let connectionErrorResponse: ConnectionErrorResponse
do {
connectionErrorResponse = try decoder.decode(
ConnectionErrorResponse.self,
from: message
)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR, error: error)
)
return
}
try await onConnectionError(connectionErrorResponse, self)
case .GQL_CONNECTION_ACK:
let connectionAckResponse: ConnectionAckResponse
do {
connectionAckResponse = try decoder.decode(
ConnectionAckResponse.self,
from: message
)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR, error: error)
)
return
}
try await onConnectionAck(connectionAckResponse, self)
case .GQL_CONNECTION_KEEP_ALIVE:
let connectionKeepAliveResponse: ConnectionKeepAliveResponse
do {
connectionKeepAliveResponse = try decoder.decode(
ConnectionKeepAliveResponse.self,
from: message
)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_CONNECTION_KEEP_ALIVE, error: error)
)
return
}
try await onConnectionKeepAlive(connectionKeepAliveResponse, self)
case .GQL_DATA:
let dataResponse: DataResponse
do {
dataResponse = try decoder.decode(DataResponse.self, from: message)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_DATA, error: error)
)
return
}
try await onData(dataResponse, self)
case .GQL_ERROR:
let errorResponse: ErrorResponse
do {
errorResponse = try decoder.decode(ErrorResponse.self, from: message)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_ERROR, error: error)
)
return
}
try await onError(errorResponse, self)
case .GQL_COMPLETE:
let completeResponse: CompleteResponse
do {
completeResponse = try decoder.decode(CompleteResponse.self, from: message)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_COMPLETE, error: error)
)
return
}
try await onComplete(completeResponse, self)
default:
try await messenger.error(.invalidType())
}
}
/// Send a `connection_init` request through the messenger
public func sendConnectionInit(payload: InitPayload) async throws {
try await messenger.send(
encoder.encode(
ConnectionInitRequest(
payload: payload
)
)
)
}
/// Send a `start` request through the messenger
public func sendStart(payload: GraphQLRequest, id: String) async throws {
try await messenger.send(
encoder.encode(
StartRequest(
payload: payload,
id: id
)
)
)
}
/// Send a `stop` request through the messenger
public func sendStop(id: String) async throws {
try await messenger.send(
encoder.encode(
StopRequest(
id: id
)
)
)
}
/// Send a `connection_terminate` request through the messenger
public func sendConnectionTerminate() async throws {
try await messenger.send(
encoder.encode(
ConnectionTerminateRequest()
)
)
}
}