/// <summary>
        /// Notifies the user that a Add Events request has failed.
        /// </summary>
        ///
        /// <param name="serverResponse">A container for information on the response from the server. Only
        /// failed responses can be passed into this method.</param>
        /// <param name="request"> The request that was sent to the server.</param>
        /// <param name="callback">The error callback.</param>
        private void NotifyAddEventsError(ServerResponse serverResponse, AddEventsRequest request, Action <AddEventsRequest, AddEventsError> errorCallback)
        {
            ReleaseAssert.IsTrue(serverResponse.Result != HttpResult.Success || serverResponse.HttpResponseCode != SuccessHttpResponseCode, "Input server request must describe an error.");

            switch (serverResponse.Result)
            {
            case HttpResult.Success:
                m_logging.LogVerboseMessage("Add Events request failed with http response code: " + serverResponse.HttpResponseCode);
                break;

            case HttpResult.CouldNotConnect:
                m_logging.LogVerboseMessage("Add Events request failed becuase a connection could be established.");
                break;

            default:
                m_logging.LogVerboseMessage("Add Events request failed for an unknown reason.");
                throw new ArgumentException("Invalid value for server response result.");
            }

            AddEventsError error = new AddEventsError(serverResponse);

            m_taskScheduler.ScheduleMainThreadTask(() =>
            {
                errorCallback(request, error);
            });
        }
        /// <summary>
        /// Notifies the user that a Add Events request was successful.
        /// </summary>
        ///
        /// <param name="serverResponse">A container for information on the response from the server. Only
        /// successful responses can be passed into this method.</param>
        /// <param name="request"> The request that was sent to the server.</param>
        /// <param name="callback">The success callback.</param>
        private void NotifyAddEventsSuccess(ServerResponse serverResponse, AddEventsRequest request, Action <AddEventsRequest> successCallback)
        {
            ReleaseAssert.IsTrue(serverResponse.Result == HttpResult.Success && serverResponse.HttpResponseCode == SuccessHttpResponseCode, "Input server request must describe a success.");

            m_logging.LogVerboseMessage("AddEvents request succeeded.");

            m_taskScheduler.ScheduleMainThreadTask(() =>
            {
                successCallback(request);
            });
        }
Example #3
0
        /// <summary>
        /// Records one or more custom metrics event that occurred within the context of a
        /// session. The posted body to this method should be a json encoded array of
        /// individual custom events. Events are validated against the custom event
        /// definitions created within the ChilliConnect dashboard. If any events are
        /// invalid, the request will not be processed and an InvalidRequest response
        /// returned. The data property of the response will contain a JSON structure that
        /// indicates the number of events successfully processed as well as the number
        /// failed in addition to specific error messages for each failed event as well as
        /// that events index within the original upload. If the provided events are valid,
        /// an empty json object will be returned.
        /// </summary>
        ///
        /// <param name="events">An array of events.</param>
        /// <param name="successCallback">The delegate which is called if the request was successful.</param>
        /// <param name="errorCallback">The delegate which is called if the request was unsuccessful. Provides
        /// a container with information on what went wrong.</param>
        public void AddEvents(IList <MetricsEvent> events, Action <AddEventsRequest> successCallback, Action <AddEventsRequest, AddEventsError> errorCallback)
        {
            m_logging.LogVerboseMessage("Sending Add Events request.");

            var metricsAccessToken = m_dataStore.GetString("MetricsAccessToken");
            var request            = new AddEventsRequest(events, metricsAccessToken);

            m_serverRequestSystem.SendImmediateRequest(request, (IImmediateServerRequest sentRequest, ServerResponse serverResponse) =>
            {
                ReleaseAssert.IsTrue(request == sentRequest, "Received request is not the same as the one sent!");

                if (serverResponse.Result == HttpResult.Success && serverResponse.HttpResponseCode == SuccessHttpResponseCode)
                {
                    NotifyAddEventsSuccess(serverResponse, request, successCallback);
                }
                else
                {
                    NotifyAddEventsError(serverResponse, request, errorCallback);
                }
            });
        }