Example #1
0
        /// <summary>
        /// Write batches of custom events to OneDS (One Data Collector).
        /// The payload of custom events must be of OneDSEventData type.
        /// </summary>
        public async Task <PlayFabResult <WriteEventsResponse> > WriteTelemetryEventsAsync(WriteEventsRequest request, object customData = null, Dictionary <string, string> extraHeaders = null)
        {
            if (request.Events.Count == 0)
            {
                var apiMethodResult = new PlayFabResult <WriteEventsResponse>
                {
                    Error = new PlayFabError
                    {
                        Error        = PlayFabErrorCode.ContentNotFound,
                        ErrorMessage = "No events found in request. Please make sure to provide at least one event in the batch.",
                        HttpStatus   = "OneDSError"
                    },
                    CustomData = customData
                };

                return(apiMethodResult);
            }

            if (!this.IsOneDSAuthenticated)
            {
                var apiMethodResult = new PlayFabResult <WriteEventsResponse>
                {
                    Error = new PlayFabError
                    {
                        Error        = PlayFabErrorCode.AuthTokenDoesNotExist,
                        ErrorMessage = "OneDS API client is not authenticated. Please make sure OneDS credentials are set.",
                        HttpStatus   = "OneDSError"
                    },
                    CustomData = customData
                };

                return(apiMethodResult);
            }

            // get transport plugin for OneDS
            var transport = PluginManager.GetPlugin <IOneDSTransportPlugin>(PluginContract.PlayFab_Transport, PluginManager.PLUGIN_TRANSPORT_ONEDS);

            // compose batch using One Collector's Common Schema 3.0
            byte[] serializedBatch;
            using (var ms = new MemoryStream())
            {
                foreach (var eventContents in request.Events)
                {
                    var csEvent = new OneDSCsEvent();
                    csEvent.data = new List <OneDSEventData>();
                    var data = eventContents.Payload as OneDSEventData;
                    if (data != null)
                    {
                        csEvent.data.Add(data);
                    }

                    csEvent.name = eventContents.Name;

                    csEvent.iKey     = this.oneDSProjectIdIkey;
                    csEvent.time     = DateTime.UtcNow.Ticks;
                    csEvent.ver      = "3.0";
                    csEvent.baseType = string.Empty;
                    csEvent.flags    = 1;

                    // serialize CS 3.0 event using bond
                    BondHelper.Serialize(csEvent, ms);
                }

#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
                ms.Position = 0;
                byte[] packageBytes = ms.ToArray();
                ms.SetLength(0);

                // Gzip content
                using (var gZipStream = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    gZipStream.Write(packageBytes, 0, packageBytes.Length);
                }
#endif

                serializedBatch = ms.ToArray();
            }

            // send serialized binary content of batch
            var headers = new Dictionary <string, string>();
            headers["APIKey"]  = this.oneDSIngestionKey;
            headers["Tickets"] = "\"" + oneDSHeaderJwtTicketKey + "\": \"" + oneDSHeaderJwtTicketPrefix + ":" + oneDSJwtToken + "\"";
            if (extraHeaders != null)
            {
                foreach (var extraHeader in extraHeaders)
                {
                    headers.Add(extraHeader.Key, extraHeader.Value);
                }
            }

            PlayFabResult <WriteEventsResponse> result = null;
            transport.DoPost(serializedBatch, headers, httpResult =>
            {
                if (httpResult is PlayFabError)
                {
                    var error = (PlayFabError)httpResult;
                    result    = new PlayFabResult <WriteEventsResponse> {
                        Error = error, CustomData = customData
                    };
                    return;
                }
                result = new PlayFabResult <WriteEventsResponse> {
                    Result = new WriteEventsResponse(), CustomData = customData
                };
            });

            await WaitWhile(() => result == null, 100);

            return(result);
        }