public void MakeApiCall(CallRequestContainer reqContainer) { //Set headers var headers = new Dictionary <string, string> { { "Content-Type", "application/json" } }; if (reqContainer.AuthKey == AuthType.DevSecretKey) { headers.Add("X-SecretKey", DevKey); } else if (reqContainer.AuthKey == AuthType.LoginSession) { headers.Add("X-Authorization", AuthKey); } headers.Add("X-ReportErrorAsSuccess", "true"); headers.Add("X-PlayFabSDK", PlayFabSettings.VersionString); #if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL if (PlayFabSettings.CompressApiData) { headers.Add("Content-Encoding", "GZIP"); headers.Add("Accept-Encoding", "GZIP"); using (var stream = new MemoryStream()) { using (GZipStream zipstream = new GZipStream(stream, CompressionMode.Compress, CompressionLevel.BestCompression)) { zipstream.Write(reqContainer.Payload, 0, reqContainer.Payload.Length); } reqContainer.Payload = stream.ToArray(); } } #endif //Debug.LogFormat("Posting {0} to Url: {1}", req.Trim(), url); var www = new WWW(reqContainer.FullUrl, reqContainer.Payload, headers); #if PLAYFAB_REQUEST_TIMING var stopwatch = System.Diagnostics.Stopwatch.StartNew(); #endif // Start the www corouting to Post, and get a response or error which is then passed to the callbacks. Action <string> wwwSuccessCallback = (response) => { try { #if PLAYFAB_REQUEST_TIMING var startTime = DateTime.UtcNow; #endif var httpResult = JsonWrapper.DeserializeObject <HttpResponseObject>(response, PlayFabUtil.ApiSerializerStrategy); if (httpResult.code == 200) { // We have a good response from the server reqContainer.JsonResponse = JsonWrapper.SerializeObject(httpResult.data, PlayFabUtil.ApiSerializerStrategy); reqContainer.DeserializeResultJson(); reqContainer.ApiResult.Request = reqContainer.ApiRequest; reqContainer.ApiResult.CustomData = reqContainer.CustomData; #if !DISABLE_PLAYFABCLIENT_API ClientModels.UserSettings userSettings = null; var res = reqContainer.ApiResult as ClientModels.LoginResult; var regRes = reqContainer.ApiResult as ClientModels.RegisterPlayFabUserResult; if (res != null) { userSettings = res.SettingsForUser; AuthKey = res.SessionTicket; } else if (regRes != null) { userSettings = regRes.SettingsForUser; AuthKey = regRes.SessionTicket; } if (userSettings != null && AuthKey != null && userSettings.NeedsAttribution) { PlayFabIdfa.OnPlayFabLogin(); } var cloudScriptUrl = reqContainer.ApiResult as ClientModels.GetCloudScriptUrlResult; if (cloudScriptUrl != null) { PlayFabSettings.LogicServerUrl = cloudScriptUrl.Url; } #endif try { PlayFabHttp.SendEvent(reqContainer.ApiRequest, reqContainer.ApiResult, ApiProcessingEventType.Post); } catch (Exception e) { Debug.LogException(e); } #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 try { reqContainer.InvokeSuccessCallback(); } catch (Exception e) { Debug.LogException(e); } } else { if (reqContainer.ErrorCallback != null) { reqContainer.Error = PlayFabHttp.GeneratePlayFabError(response, reqContainer.CustomData); PlayFabHttp.SendErrorEvent(reqContainer.ApiRequest, reqContainer.Error); reqContainer.ErrorCallback(reqContainer.Error); } } } catch (Exception e) { Debug.LogException(e); } }; Action <string> wwwErrorCallback = (errorCb) => { if (reqContainer.ErrorCallback != null) { reqContainer.Error = PlayFabHttp.GeneratePlayFabErrorGeneric(errorCb, null, reqContainer.CustomData); PlayFabHttp.SendErrorEvent(reqContainer.ApiRequest, reqContainer.Error); reqContainer.ErrorCallback(reqContainer.Error); } }; PlayFabHttp.instance.StartCoroutine(Post(www, wwwSuccessCallback, wwwErrorCallback)); }
private static void ProcessJsonResponse(CallRequestContainer reqContainer) { try { var httpResult = JsonWrapper.DeserializeObject <HttpResponseObject>(reqContainer.JsonResponse, PlayFabUtil.ApiSerializerStrategy); #if PLAYFAB_REQUEST_TIMING reqContainer.Timing.WorkerRequestMs = (int)reqContainer.Stopwatch.ElapsedMilliseconds; #endif //This would happen if playfab returned a 500 internal server error or a bad json response. if (httpResult == null) { reqContainer.Error = PlayFabHttp.GeneratePlayFabErrorGeneric(reqContainer.JsonResponse, null, reqContainer.CustomData); reqContainer.HttpState = HttpRequestState.Error; lock (ResultQueue) { //Queue The result callbacks to run on the main thread. ResultQueue.Enqueue(() => { PlayFabHttp.SendErrorEvent(reqContainer.ApiRequest, reqContainer.Error); if (reqContainer.ErrorCallback != null) { reqContainer.ErrorCallback(reqContainer.Error); } }); } return; } if (httpResult.code != 200) { reqContainer.Error = PlayFabHttp.GeneratePlayFabError(reqContainer.JsonResponse, reqContainer.CustomData); reqContainer.HttpState = HttpRequestState.Error; lock (ResultQueue) { //Queue The result callbacks to run on the main thread. ResultQueue.Enqueue(() => { PlayFabHttp.SendErrorEvent(reqContainer.ApiRequest, reqContainer.Error); if (reqContainer.ErrorCallback != null) { reqContainer.ErrorCallback(reqContainer.Error); } }); } return; } reqContainer.JsonResponse = JsonWrapper.SerializeObject(httpResult.data, PlayFabUtil.ApiSerializerStrategy); reqContainer.DeserializeResultJson(); // Assigns Result with a properly typed object reqContainer.ApiResult.Request = reqContainer.ApiRequest; reqContainer.ApiResult.CustomData = reqContainer.CustomData; #if !DISABLE_PLAYFABCLIENT_API UserSettings userSettings = null; var res = reqContainer.ApiResult as LoginResult; var regRes = reqContainer.ApiResult as RegisterPlayFabUserResult; if (res != null) { userSettings = res.SettingsForUser; _authKey = res.SessionTicket; } else if (regRes != null) { userSettings = res.SettingsForUser; _authKey = regRes.SessionTicket; } if (userSettings != null && _authKey != null && userSettings.NeedsAttribution) { lock (ResultQueue) { ResultQueue.Enqueue(PlayFabIdfa.OnPlayFabLogin); } } var cloudScriptUrl = reqContainer.ApiResult as GetCloudScriptUrlResult; if (cloudScriptUrl != null) { PlayFabSettings.LogicServerUrl = cloudScriptUrl.Url; } #endif lock (ResultQueue) { //Queue The result callbacks to run on the main thread. ResultQueue.Enqueue(() => { #if PLAYFAB_REQUEST_TIMING reqContainer.Stopwatch.Stop(); reqContainer.Timing.MainThreadRequestMs = (int)reqContainer.Stopwatch.ElapsedMilliseconds; PlayFabHttp.SendRequestTiming(reqContainer.Timing); #endif try { PlayFabHttp.SendEvent(reqContainer.ApiRequest, reqContainer.ApiResult, ApiProcessingEventType.Post); reqContainer.InvokeSuccessCallback(); } catch (Exception e) { Debug.LogException(e); } }); } } catch (Exception e) { Debug.LogException(e); reqContainer.HttpState = HttpRequestState.Error; } }