forked from NavidK0/SimpleGraphQL-For-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtils.cs
More file actions
446 lines (394 loc) · 16.7 KB
/
Copy pathHttpUtils.cs
File metadata and controls
446 lines (394 loc) · 16.7 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.Networking;
using Debug = UnityEngine.Debug;
namespace SimpleGraphQL
{
public class GraphQLResponse
{
public int statusCode;
public string requestName;
public string requestURL;
public string requestAimlabsID;
public string responseContent;
public long totalTime;
}
[PublicAPI]
public static class HttpUtils
{
private static ClientWebSocket _webSocket;
/// <summary>
/// Called when the websocket receives subscription data.
/// </summary>
public static event Action<string> SubscriptionDataReceived;
public static Dictionary<string, Action<string>> SubscriptionDataReceivedPerChannel;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void PreInit()
{
_webSocket?.Dispose();
SubscriptionDataReceived = null;
SubscriptionDataReceivedPerChannel = new Dictionary<string, Action<string>>();
}
/// <summary>
/// For when the WebSocket needs to be disposed and reset.
/// </summary>
public static void Dispose()
{
_webSocket?.Dispose();
_webSocket = null;
}
/// <summary>
/// POST a query to the given endpoint url.
/// </summary>
/// <param name="url">The endpoint url.</param>
/// <param name="request">The GraphQL request</param>
/// <param name="authScheme">The authentication scheme to be used.</param>
/// <param name="authToken">The actual auth token.</param>
/// <param name="headers">Any headers that should be passed in</param>
/// <param name="debug">Prints Debug information on request/response</param>
/// <returns></returns>
public static async Task<GraphQLResponse> PostRequest(
string url,
Request request,
JsonSerializerSettings serializerSettings = null,
Dictionary<string, string> headers = null,
string authToken = null,
string authScheme = null,
bool debug = false
)
{
var uri = new Uri(url);
string payloadString = await Task.Run(() => request.ToJson(false, serializerSettings));
byte[] payload = Encoding.UTF8.GetBytes(payloadString);
string payloadStringDisplayable = payloadString.Replace("\\r\\n", "\n");
using (UnityWebRequest webRequest = new UnityWebRequest(uri, "POST"))
{
webRequest.uploadHandler = new UploadHandlerRaw(payload);
webRequest.downloadHandler = new DownloadHandlerBuffer();
// ADDING HEADERS ----------------------------------------------
var webRequestHeaders = new Dictionary<string, string>();
if (authToken != null)
{
webRequestHeaders.Add("Authorization", $"{authScheme} {authToken}");
}
webRequestHeaders.Add("Content-Type", "application/json");
if (headers != null)
{
foreach (var header in headers)
{
webRequestHeaders.Add(header.Key, header.Value);
}
}
foreach (var header in webRequestHeaders)
{
webRequest.SetRequestHeader(header.Key, header.Value);
}
//Send the request then wait here until it returns
try
{
if (debug)
{
Debug.Log($"Firing SimpleGraphQL POST Request {request.OperationName}" +
$"\n\nThread: {Thread.CurrentThread.ManagedThreadId}" +
"\n\nURL: \n " + uri.ToString() +
"\n\nHeaders: \n " + webRequestHeaders.ToString() +
$"\n\nThread: {Thread.CurrentThread.ManagedThreadId}" +
"\n\nContent: \n" + payloadStringDisplayable);
}
var startTime = DateTime.Now;
var operation = webRequest.SendWebRequest();
TaskCompletionSource<bool> requestCompleted = new();
operation.completed += _ => requestCompleted.TrySetResult(true);
await requestCompleted.Task;
if (webRequest.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError("Error While Sending: " + webRequest.error);
throw new UnityWebRequestException(webRequest);
}
var executionTime = DateTime.Now - startTime;
var responseContent = webRequest.downloadHandler.text;
var aimlabsRequestHeader = webRequest.GetResponseHeaders().FirstOrDefault(header => header.Key.StartsWith("Aimlabs-Request-Id"));
var aimlabsRequestID = (aimlabsRequestHeader.Value != null) ? aimlabsRequestHeader.Value : "(ID NOT FOUND)";
if (debug)
{
Debug.Log($"Received SimpleGraphQL POST Response {request.OperationName}" +
$"\n\nThread: {Thread.CurrentThread.ManagedThreadId}" +
"\n\nTime in ms: \n " + executionTime.Milliseconds +
"\n\nHeaders: \n " + webRequest.GetResponseHeaders().ToString() +
"\n\nContent: \n" + responseContent +
"\n\nRequest URL: \n " + uri.ToString() +
"\n\nRequest Headers: \n " + webRequestHeaders.ToString() +
"\n\nRequest Content: \n" + payloadStringDisplayable);
}
else
{
if (request?.OperationName != null && webRequest.GetResponseHeaders() != null)
{
Debug.Log($"Received GraphQL Response for {request.OperationName}, id: {aimlabsRequestID}");
}
}
return new GraphQLResponse()
{
statusCode = (int)webRequest.responseCode,
requestName = request.OperationName,
requestURL = uri.ToString(),
requestAimlabsID = aimlabsRequestID,
responseContent = responseContent,
totalTime = executionTime.Milliseconds
};
}
catch (Exception e)
{
#if UNITY_EDITOR
Debug.LogError("[SimpleGraphQL] " + e);
#endif
throw new UnityWebRequestException(webRequest);
}
}
}
public static bool IsWebSocketReady() =>
_webSocket?.State == WebSocketState.Connecting || _webSocket?.State == WebSocketState.Open;
/// <summary>
/// Connect to the GraphQL server. Call is necessary in order to send subscription queries via WebSocket.
/// </summary>
/// <param name="url"></param>
/// <param name="authScheme"></param>
/// <param name="authToken"></param>
/// <param name="headers"></param>
/// <param name="protocol"></param>
/// <returns></returns>
public static async Task WebSocketConnect(
string url,
Dictionary<string, string> headers = null,
string authToken = null,
string authScheme = null,
string protocol = "graphql-ws"
)
{
url = url.Replace("http", "ws");
var uri = new Uri(url);
_webSocket = new ClientWebSocket();
_webSocket.Options.AddSubProtocol(protocol);
var payload = new Dictionary<string, string>();
if(protocol == "graphql-transport-ws") {
payload["content-type"] = "application/json";
} else {
_webSocket.Options.SetRequestHeader("Content-Type", "application/json");
}
if (authToken != null) {
if(protocol == "graphql-transport-ws") {
// set Authorization as payload
payload["Authorization"] = $"{authScheme} {authToken}";
} else {
_webSocket.Options.SetRequestHeader("Authorization", $"{authScheme} {authToken}");
}
}
if (headers != null)
{
foreach (KeyValuePair<string, string> header in headers)
{
_webSocket.Options.SetRequestHeader(header.Key, header.Value);
}
}
try
{
Debug.Log("Websocket is connecting");
await _webSocket.ConnectAsync(uri, CancellationToken.None);
var json = JsonConvert.SerializeObject(
new
{
type = "connection_init",
payload = payload
},
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}
);
Debug.Log("Websocket is starting");
// Initialize the socket at the server side
await _webSocket.SendAsync(
new ArraySegment<byte>(Encoding.UTF8.GetBytes(json)),
WebSocketMessageType.Text,
true,
CancellationToken.None
);
Debug.Log("Websocket is updating");
// Start listening to the websocket for data.
WebSocketUpdate();
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
/// <summary>
/// Disconnect the websocket.
/// </summary>
/// <returns></returns>
public static async Task WebSocketDisconnect()
{
if (_webSocket?.State != WebSocketState.Open)
{
Debug.LogError("Attempted to disconnect from a socket that was not open!");
return;
}
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Socket closed.", CancellationToken.None);
}
/// <summary>
/// Subscribe to a query.
/// </summary>
/// <param name="id">Used to identify the subscription. Must be unique per query.</param>
/// <param name="request">The subscription query.</param>
/// <returns>true if successful</returns>
public static async Task<bool> WebSocketSubscribe(string id, Request request)
{
if (!IsWebSocketReady())
{
Debug.LogError("Attempted to subscribe to a query without connecting to a WebSocket first!");
return false;
}
string json = JsonConvert.SerializeObject(
new
{
id,
type = _webSocket.SubProtocol == "graphql-transport-ws" ? "subscribe" : "start",
payload = new
{
query = request.Query,
variables = request.Variables,
operationName = request.OperationName
}
},
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}
);
await _webSocket.SendAsync(
new ArraySegment<byte>(Encoding.UTF8.GetBytes(json)),
WebSocketMessageType.Text,
true,
CancellationToken.None
);
return true;
}
/// <summary>
/// Unsubscribe from this query.
/// </summary>
/// <param name="id">Used to identify the subscription. Must be unique per query.</param>
/// <returns></returns>
public static async Task WebSocketUnsubscribe(string id)
{
if (!IsWebSocketReady())
{
Debug.LogError("Attempted to unsubscribe to a query without connecting to a WebSocket first!");
return;
}
var type = _webSocket.SubProtocol == "graphql-transport-ws" ? "complete" : "stop";
await _webSocket.SendAsync(
new ArraySegment<byte>(Encoding.UTF8.GetBytes($@"{{""type"":""{type}"",""id"":""{id}""}}")),
WebSocketMessageType.Text,
true,
CancellationToken.None
);
}
private static async void WebSocketUpdate()
{
while (true)
{
ArraySegment<byte> buffer;
buffer = WebSocket.CreateClientBuffer(1024, 1024);
if (buffer.Array == null)
{
throw new WebSocketException("Buffer array is null!");
}
WebSocketReceiveResult wsReceiveResult;
var jsonBuild = new StringBuilder();
do
{
wsReceiveResult = await _webSocket.ReceiveAsync(buffer, CancellationToken.None);
jsonBuild.Append(Encoding.UTF8.GetString(buffer.Array, buffer.Offset, wsReceiveResult.Count));
} while (!wsReceiveResult.EndOfMessage);
var jsonResult = jsonBuild.ToString();
if (jsonResult.IsNullOrEmpty()) return;
JObject jsonObj;
try
{
jsonObj = JObject.Parse(jsonResult);
}
catch (JsonReaderException e)
{
throw new ApplicationException(e.Message);
}
var msgType = (string)jsonObj["type"];
var id = (string)jsonObj["id"];
switch (msgType)
{
case "connection_error":
{
throw new WebSocketException("Connection error. Error: " + jsonResult);
}
case "connection_ack":
{
Debug.Log($"Websocket connection acknowledged ({id}).");
continue;
}
case "data":
case "next":
{
JToken jToken = jsonObj["payload"];
if (jToken != null)
{
throw new WebSocketException("Connection error. Error: " + jsonResult);
}
continue;
}
case "error":
{
throw new WebSocketException("Handshake error. Error: " + jsonResult);
}
case "complete":
{
Debug.Log("Server sent complete, it's done sending data.");
break;
}
case "ka":
{
// stayin' alive, stayin' alive
continue;
}
case "subscription_fail":
{
throw new WebSocketException("Subscription failed. Error: " + jsonResult);
}
case "ping":
{
await _webSocket.SendAsync(
new ArraySegment<byte>(Encoding.UTF8.GetBytes($@"{{""type"":""pong""}}")),
WebSocketMessageType.Text,
true,
CancellationToken.None
);
continue;
}
}
break;
}
}
}
}