/
PlayFabUnityHttp.cs
273 lines (238 loc) · 10.1 KB
/
PlayFabUnityHttp.cs
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
#if UNITY_2017_2_OR_NEWER
using PlayFab.SharedModels;
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace PlayFab.Internal
{
public class PlayFabUnityHttp : ITransportPlugin
{
private bool _isInitialized = false;
private readonly int _pendingWwwMessages = 0;
public bool IsInitialized { get { return _isInitialized; } }
public void Initialize() { _isInitialized = true; }
public void Update() { }
public void OnDestroy() { }
public void SimpleGetCall(string fullUrl, Action<byte[]> successCallback, Action<string> errorCallback)
{
PlayFabHttp.instance.StartCoroutine(SimpleCallCoroutine("get", fullUrl, null, successCallback, errorCallback));
}
public void SimplePutCall(string fullUrl, byte[] payload, Action<byte[]> successCallback, Action<string> errorCallback)
{
PlayFabHttp.instance.StartCoroutine(SimpleCallCoroutine("put", fullUrl, payload, successCallback, errorCallback));
}
public void SimplePostCall(string fullUrl, byte[] payload, Action<byte[]> successCallback, Action<string> errorCallback)
{
PlayFabHttp.instance.StartCoroutine(SimpleCallCoroutine("post", fullUrl, payload, successCallback, errorCallback));
}
private static IEnumerator SimpleCallCoroutine(string method, string fullUrl, byte[] payload, Action<byte[]> successCallback, Action<string> errorCallback)
{
if (payload == null)
{
using (UnityWebRequest www = UnityWebRequest.Get(fullUrl))
{
if (PlayFabSettings.staticSettings.CompressResponses)
{
www.SetRequestHeader("Accept-Encoding", "gzip");
if (PlayFabSettings.staticSettings.DecompressWithDownloadHandler)
{
www.downloadHandler = new GzipDownloadHandler();
}
}
#if UNITY_2017_2_OR_NEWER
yield return www.SendWebRequest();
#else
yield return www.Send();
#endif
if (!string.IsNullOrEmpty(www.error))
errorCallback(www.error);
else
successCallback(www.downloadHandler.data);
};
}
else
{
UnityWebRequest request;
if (method == "put")
{
request = UnityWebRequest.Put(fullUrl, payload);
}
else
{
request = new UnityWebRequest(fullUrl, "POST");
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(payload);
request.SetRequestHeader("Content-Type", "application/json");
if (PlayFabSettings.staticSettings.CompressResponses)
{
request.SetRequestHeader("Accept-Encoding", "gzip");
request.downloadHandler = PlayFabSettings.staticSettings.DecompressWithDownloadHandler ? new GzipDownloadHandler()
: new DownloadHandlerBuffer();
}
else
{
request.downloadHandler = new DownloadHandlerBuffer();
}
}
#if UNITY_2017_2_OR_NEWER
#if !UNITY_2019_1_OR_NEWER
request.chunkedTransfer = false; // can be removed after Unity's PUT will be more stable
#endif
yield return request.SendWebRequest();
#else
yield return request.Send();
#endif
#if UNITY_2020_1_OR_NEWER
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
#else
if (request.isNetworkError || request.isHttpError)
#endif
{
errorCallback(request.error);
}
else
{
successCallback(request.downloadHandler.data);
}
request.Dispose();
}
}
public void MakeApiCall(object reqContainerObj)
{
CallRequestContainer reqContainer = (CallRequestContainer)reqContainerObj;
reqContainer.RequestHeaders["Content-Type"] = "application/json";
// Start the www corouting to Post, and get a response or error which is then passed to the callbacks.
PlayFabHttp.instance.StartCoroutine(Post(reqContainer));
}
private IEnumerator Post(CallRequestContainer reqContainer)
{
#if PLAYFAB_REQUEST_TIMING
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
var startTime = DateTime.UtcNow;
#endif
using var www = new UnityWebRequest(reqContainer.FullUrl)
{
uploadHandler = new UploadHandlerRaw(reqContainer.Payload),
method = "POST"
};
if (reqContainer.settings.CompressResponses)
{
www.SetRequestHeader("Accept-Encoding", "gzip");
www.downloadHandler = reqContainer.settings.DecompressWithDownloadHandler ? new GzipDownloadHandler()
: new DownloadHandlerBuffer();
}
else
{
www.downloadHandler = new DownloadHandlerBuffer();
}
foreach (var headerPair in reqContainer.RequestHeaders)
{
if (!string.IsNullOrEmpty(headerPair.Key) && !string.IsNullOrEmpty(headerPair.Value))
www.SetRequestHeader(headerPair.Key, headerPair.Value);
else
Debug.LogWarning("Null header: " + headerPair.Key + " = " + headerPair.Value);
}
#if UNITY_2017_2_OR_NEWER
yield return www.SendWebRequest();
#else
yield return www.Send();
#endif
#if PLAYFAB_REQUEST_TIMING
stopwatch.Stop();
var timing = new PlayFabHttp.RequestTiming {
StartTimeUtc = startTime,
ApiEndpoint = reqContainer.ApiEndpoint,
WorkerRequestMs = (int)stopwatch.ElapsedMilliseconds,
MainThreadRequestMs = (int)stopwatch.ElapsedMilliseconds
};
PlayFabHttp.SendRequestTiming(timing);
#endif
if (!string.IsNullOrEmpty(www.error))
{
OnError(www.error, reqContainer);
}
else
{
try
{
byte[] responseBytes = www.downloadHandler.data;
string responseText = System.Text.Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);
OnResponse(responseText, reqContainer);
}
catch (Exception e)
{
OnError("Unhandled error in PlayFabUnityHttp: " + e, reqContainer);
}
}
www.Dispose();
}
public int GetPendingMessages()
{
return _pendingWwwMessages;
}
public void OnResponse(string response, CallRequestContainer reqContainer)
{
try
{
#if PLAYFAB_REQUEST_TIMING
var startTime = DateTime.UtcNow;
#endif
var serializer = PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer);
var httpResult = serializer.DeserializeObject<HttpResponseObject>(response);
if (httpResult.code == 200)
{
// We have a good response from the server
reqContainer.JsonResponse = serializer.SerializeObject(httpResult.data);
reqContainer.DeserializeResultJson();
reqContainer.ApiResult.Request = reqContainer.ApiRequest;
reqContainer.ApiResult.CustomData = reqContainer.CustomData;
PlayFabHttp.instance.OnPlayFabApiResult(reqContainer);
#if !DISABLE_PLAYFABCLIENT_API
PlayFabDeviceUtil.OnPlayFabLogin(reqContainer.ApiResult, reqContainer.settings, reqContainer.instanceApi);
#endif
try
{
PlayFabHttp.SendEvent(reqContainer.ApiEndpoint, reqContainer.ApiRequest, reqContainer.ApiResult, ApiProcessingEventType.Post);
}
catch (Exception e)
{
Debug.LogException(e);
}
try
{
reqContainer.InvokeSuccessCallback();
}
catch (Exception e)
{
Debug.LogException(e);
}
}
else
{
if (reqContainer.ErrorCallback != null)
{
reqContainer.Error = PlayFabHttp.GeneratePlayFabError(reqContainer.ApiEndpoint, response, reqContainer.CustomData);
PlayFabHttp.SendErrorEvent(reqContainer.ApiRequest, reqContainer.Error);
reqContainer.ErrorCallback(reqContainer.Error);
}
}
}
catch (Exception e)
{
Debug.LogException(e);
}
}
public void OnError(string error, CallRequestContainer reqContainer)
{
reqContainer.JsonResponse = error;
if (reqContainer.ErrorCallback != null)
{
reqContainer.Error = PlayFabHttp.GeneratePlayFabError(reqContainer.ApiEndpoint, reqContainer.JsonResponse, reqContainer.CustomData);
PlayFabHttp.SendErrorEvent(reqContainer.ApiRequest, reqContainer.Error);
reqContainer.ErrorCallback(reqContainer.Error);
}
}
}
}
#endif