forked from datadvance/DjangoChannelsGraphqlWs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
279 lines (226 loc) · 9.77 KB
/
client.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright (C) DATADVANCE, 2010-2021
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""GraphQL client."""
import asyncio
import textwrap
import time
import uuid
from .transport import GraphqlWsTransport
class GraphqlWsClient:
"""A client for the GraphQL WebSocket server.
The client implements a WebSocket-based GraphQL protocol. It is the
same protocol as the server implemented by the `GraphqlWsConsumer`
class. See its docstring for details and for the protocol
description.
This class implements only the protocol itself. The implementation
of the message delivery extracted into the separate interface
`GraphqlWsTransport`. So it is possible to use this client with
different network frameworks (e.g. Tornado, AIOHTTP).
NOTE: The `receive` method retrieves the first response received by
backend, when used with subscriptions it may either return
subscription data or some query result. The response type must be
checked outside the client manually.
Args:
transport: The `GraphqlWsTransport` instance used to send and
receive messages over the WebSocket connection.
"""
def __init__(self, transport: GraphqlWsTransport):
"""Constructor."""
assert isinstance(
transport, GraphqlWsTransport
), "The 'transport' must implement the 'GraphqlWsTransport' interface!"
self._transport = transport
self._is_connected = False
@property
def transport(self) -> GraphqlWsTransport:
"""Underlying network transport."""
return self._transport
@property
def connected(self) -> bool:
"""Indicate whether client is connected."""
return self._is_connected
async def connect_and_init(self, connect_only: bool = False) -> None:
"""Establish and initialize WebSocket GraphQL connection.
1. Establish WebSocket connection.
2. Initialize GraphQL connection. Skipped if connect_only=True.
"""
await self._transport.connect()
if not connect_only:
await self._transport.send({"type": "connection_init", "payload": ""})
resp = await self._transport.receive()
assert resp["type"] == "connection_ack", f"Unexpected response `{resp}`!"
self._is_connected = True
# Default value for `id`, because `None` is also a valid value.
AUTO = object()
async def send(self, *, msg_id=AUTO, msg_type=None, payload=None):
"""Send GraphQL message.
If any argument is `None` it is excluded from the message.
Args:
msg_id: The message identifier. Automatically generated by
default.
msg_type: The message type.
payload: The payload dict.
Returns:
The message identifier.
"""
if msg_id is self.AUTO:
msg_id = str(uuid.uuid4().hex)
message = {}
message.update({"id": msg_id} if msg_id is not None else {})
message.update({"type": msg_type} if msg_type is not None else {})
message.update({"payload": payload} if payload is not None else {})
await self._transport.send(message)
return msg_id
async def receive(
self, *, wait_id=None, assert_id=None, assert_type=None, raw_response=False
):
"""Receive GraphQL message checking its content.
Args:
wait_id: Wait until response with the given id received, all
intermediate responses will be skipped.
assert_id: Raise error if response id does not match value.
assert_type: Raise error if response type does not match
value.
raw_response: Whether return the entire response or the only
payload.
Returns:
The `payload` field of the message received or `None` or
the entire response if the raw_response flag is True.
"""
while True:
response = await self._transport.receive()
if self._is_keep_alive_response(response):
continue
if wait_id is None or response["id"] == wait_id:
break
if assert_type is not None:
assert response["type"] == assert_type, (
f"Type `{assert_type}` expected, but `{response['type']}` received!"
f" Response: {response}."
)
if assert_id is not None:
assert response["id"] == assert_id, "Response id != expected id!"
payload = response.get("payload", None)
if payload is not None and "errors" in payload:
raise GraphqlWsResponseError(response)
if not raw_response:
return payload
return response
async def execute(self, query, variables=None):
"""Execute query or mutation request and wait for the reply.
Args:
query: A GraphQL query string. We `dedent` it, so you do not
have to.
variables: Dict of variables (optional).
Returns:
Dictionary with the GraphQL response.
"""
msg_id = await self.start(query, variables=variables)
try:
resp = await self.receive(wait_id=msg_id)
finally:
# Consume 'complete' message.
await self.receive(wait_id=msg_id)
return resp
async def subscribe(self, query, *, variables=None, wait_confirmation=True):
"""Execute subscription request and wait for the confirmation.
Args:
query: A GraphQL string query. We `dedent` it, so you do not
have to.
variables: Dict of variables (optional).
wait_confirmation: If `True` wait for the subscription
confirmation message.
Returns:
The message identifier.
"""
msg_id = await self.start(query, variables=variables)
if wait_confirmation:
await self.receive(wait_id=msg_id)
return msg_id
async def start(self, query, *, variables=None):
"""Start GraphQL request. Responses must be checked explicitly.
Args:
query: A GraphQL string query. We `dedent` it, so you do not
have to.
variables: Dict of variables (optional).
Returns:
The message identifier.
"""
return await self.send(
msg_type="start",
payload={"query": textwrap.dedent(query), "variables": variables or {}},
)
async def finalize(self):
"""Disconnect and wait the transport to finish gracefully."""
await self._transport.disconnect()
self._is_connected = False
async def wait_response(self, response_checker, timeout=None):
"""Wait for particular response skipping all intermediate ones.
Useful when you need to need to wait until subscription reports
desired state or skip subscription messages between request and
response.
Args:
response_checker: Function with accepts GraphQL response as
single parameter and must return `True` for desired
response and `False` the other responses.
timeout: Seconds to wait until response is received.
Returns:
Response payload, same as `receive`.
Raises:
`asyncio.TimeoutError` when timeout is reached.
"""
if timeout is None:
timeout = self._transport.TIMEOUT
while timeout > 0:
start = time.monotonic()
try:
response = await self.receive()
if response_checker(response):
return response
except asyncio.TimeoutError:
# Ignore `receive` calls timeout until wait timeout
# is reached.
pass
timeout -= time.monotonic() - start
raise asyncio.TimeoutError
async def wait_disconnect(self, timeout=None):
"""Wait server to close the connection.
Args:
timeout: Seconds to wait the connection to close.
Raises:
`asyncio.TimeoutError` when timeout is reached.
"""
await self._transport.wait_disconnect(timeout)
self._is_connected = False
@staticmethod
def _is_keep_alive_response(response):
"""Check if received GraphQL response is keep-alive message."""
return response.get("type") == "ka"
class GraphqlWsResponseError(Exception):
"""Errors data from the GraphQL response."""
def __init__(self, response, message=None):
"""Exception constructor."""
super().__init__(self)
self.message = message
self.response = response
def __str__(self):
"""Nice string representation."""
return f"{self.message or 'Error in GraphQL response'}: {self.response}!"