public async void WriteOneDSEventsAsync(UUnitTestContext testContext)
#endif
    {
        var event1 = new PlayFabEvent()
        {
            Name = "Event_1", EventType = PlayFabEventType.Lightweight
        };

        event1.SetProperty("Prop-A", true);
        event1.SetProperty("Prop-B", "hello");
        event1.SetProperty("Prop-C", 123);

        var event2 = new PlayFabEvent()
        {
            Name = "Event_2", EventType = PlayFabEventType.Lightweight
        };

        event2.SetProperty("Prop-A", false);
        event2.SetProperty("Prop-B", "good-bye");
        event2.SetProperty("Prop-C", 456);

        var request = new WriteEventsRequest
        {
            Events = new List <EventContents>
            {
                event1.EventContents,
                event2.EventContents
            }
        };

        var oneDSEventsApi = new OneDSEventsAPI();

        // get OneDS authentication from PlayFab
        var configRequest = new TelemetryIngestionConfigRequest();

#if TPL_35
        var authTask = OneDSEventsAPI.GetTelemetryIngestionConfigAsync(configRequest).Await();
#else
        var authTask = await OneDSEventsAPI.GetTelemetryIngestionConfigAsync(configRequest);
#endif

        var response = authTask.Result;

        testContext.NotNull(authTask.Result, "Failed to get OneDS authentication info from PlayFab");
        oneDSEventsApi.SetCredentials("o:" + authTask.Result.TenantId, authTask.Result.IngestionKey, authTask.Result.TelemetryJwtToken, authTask.Result.TelemetryJwtHeaderKey, authTask.Result.TelemetryJwtHeaderPrefix);

        // call OneDS events API
#if TPL_35
        var writeTask = oneDSEventsApi.WriteTelemetryEventsAsync(request, null, new Dictionary <string, string>()).Await();
#else
        var writeTask = await oneDSEventsApi.WriteTelemetryEventsAsync(request, null, new Dictionary <string, string>());
#endif

        testContext.NotNull(writeTask);
        testContext.IsNull(writeTask.Error, "Failed to send a batch of custom OneDS events");
        testContext.NotNull(writeTask.Result, "Failed to send a batch of custom OneDS events. Result is null!");
        testContext.EndTest(UUnitFinishState.PASSED, "");
    }
Exemplo n.º 2
0
        /// <summary>
        /// This method is called by pipeline for each available input item (a batch of events)
        /// </summary>
        /// <param name="batch">The input item (a batch of events).</param>
        protected override void OnNextInputItem(TitleEventBatch batch)
        {
            // Send a batch and wait for result
            var writeEventsRequest = new WriteEventsRequest
            {
                Events = batch.Events.Cast <PlayFabEmitEventRequest>().Select(x => x.Event.EventContents).ToList()
            };

            if (!oneDSEventsApi.IsOneDSAuthenticated)
            {
                var authResult = OneDSEventsAPI.GetTelemetryIngestionConfigAsync(new TelemetryIngestionConfigRequest());
                try
                {
                    authResult.Wait(cts.Token);
                    var response = authResult.Result.Result;
                    if (response != null)
                    {
                        oneDSEventsApi.SetCredentials("o:" + response.TenantId, response.IngestionKey, response.TelemetryJwtToken, response.TelemetryJwtHeaderKey, response.TelemetryJwtHeaderPrefix);
                    }
                    else
                    {
                        throw new Exception($"Failed to get OneDS authentication token from PlayFab service");
                    }
                }
                catch (Exception e)
                {
                    // Cancel result promises that will never be fulfilled
                    // and move on to the next batch.
                    foreach (var request in batch.Events)
                    {
                        PlayFabEmitEventRequest eventRequest = (PlayFabEmitEventRequest)request;
                        eventRequest.ResultPromise?.SetCanceled();
                    }

                    logger.Error($"Exception in OnNextInputItem {e.Source} with message: {e}.");
                }
            }

            Task <PlayFabResult <WriteEventsResponse> > apiTask = oneDSEventsApi.WriteTelemetryEventsAsync(writeEventsRequest);

            try
            {
                apiTask.Wait(cts.Token);
                FulfillPromises(batch.Events, apiTask.Result);
            }
            catch (Exception e)
            {
                // Cancel result promises that will never be fulfilled
                // and move on to the next batch.
                foreach (var request in batch.Events)
                {
                    PlayFabEmitEventRequest eventRequest = (PlayFabEmitEventRequest)request;
                    eventRequest.ResultPromise?.SetCanceled();
                }

                logger.Error($"Exception in OnNextInputItem {e.Source} with message: {e}. This was an unhandled exception, please contact the dev team.");
            }
        }