예제 #1
0
        /// <summary>
        /// Flush the queue, clear timer and send POST network call to VWO servers.
        /// </summary>
        /// <param name="manual">manual Boolean specifying flush is triggered manual or not.</param>
        /// <returns>Boolean value specifying flush was successful or not.</returns>
        public bool flush(bool manual)
        {
            var batchMetadata = HttpRequestBuilder.GetJsonString(this.batchQueue);

            if (batchQueue.Count == 0)
            {
                LogDebugMessage.EventQueueEmpty(file);
            }
            if (manual)
            {
                if (batchQueue.Count > 0)
                {
                    LogDebugMessage.BeforeFlushing(file, "manually", batchQueue.Count.ToString(), accountId.ToString(), "Timer will be cleared and registered again", batchMetadata);
                    Task <bool> response = sendPostCall();
                    LogDebugMessage.AfterFlushing(file, "manually", batchQueue.Count.ToString(), batchMetadata);
                    disposeData();
                    return(true);
                }
                clearRequestTimer();
                return(true);
            }
            else
            {
                if (batchQueue.Count > 0 && !isBatchProcessing)
                {
                    isBatchProcessing = true;

                    LogDebugMessage.BeforeFlushing(file, "", batchQueue.Count.ToString(), accountId.ToString(), "", batchMetadata);
                    Task <bool> response = sendPostCall();
                    LogDebugMessage.AfterFlushing(file, "", batchQueue.Count.ToString(), batchMetadata);
                    disposeData();
                    return(true);
                }
                clearRequestTimer();
                return(true);
            }
        }
예제 #2
0
        /// <summary>
        /// Send Post network call to VWO servers.
        /// </summary>
        /// <returns>Boolean value specifying flush was successful or not.</returns>
        private async Task <bool> sendPostCall()
        {
            string     PayLoad    = HttpRequestBuilder.GetJsonString(this.batchQueue);
            var        ApiRequest = ServerSideVerb.EventBatching(this.accountId, this.isDevelopmentMode, this.apikey);
            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Add("Authorization", this.apikey);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var data = new StringContent(PayLoad, Encoding.UTF8, "application/json");

            try
            {
                HttpResponseMessage response = await httpClient.PostAsync(ApiRequest.Uri, data);

                response.EnsureSuccessStatusCode();
                if (response.StatusCode == System.Net.HttpStatusCode.OK && response.StatusCode < System.Net.HttpStatusCode.Ambiguous)
                {
                    if (flushCallback != null)
                    {
                        flushCallback.onFlush(null, PayLoad);
                    }
                    LogInfoMessage.ImpressionSuccess(file, ApiRequest.logUri?.ToString());
                    return(true);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.RequestEntityTooLarge)
                {
                    if (flushCallback != null)
                    {
                        flushCallback.onFlush("Payload size too large", PayLoad);
                    }
                    LogDebugMessage.BatchEventLimitExceeded(file, ApiRequest.logUri?.ToString(), this.accountId.ToString(), eventsPerRequest.ToString());
                    LogErrorMessage.ImpressionFailed(file, ApiRequest.logUri?.ToString());

                    return(false);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                {
                    if (flushCallback != null)
                    {
                        flushCallback.onFlush("Account id not found, no request app id found, or invalid API key", PayLoad);
                    }
                    LogErrorMessage.BulkNotProcessed(file);
                    LogErrorMessage.ImpressionFailed(file, ApiRequest.logUri?.ToString());
                    return(false);
                }
                else
                {
                    LogErrorMessage.BulkNotProcessed(file);
                    LogErrorMessage.ImpressionFailed(file, ApiRequest.logUri?.ToString());
                    if (flushCallback != null)
                    {
                        flushCallback.onFlush("Invalid call", PayLoad);
                    }

                    return(false);
                }
            }
            catch (HttpRequestException ex)
            {
                LogErrorMessage.BulkNotProcessed(file);
                LogErrorMessage.UnableToDisplayHttpRequest(file, ex.StackTrace);
                if (flushCallback != null)
                {
                    flushCallback.onFlush("HttpRequest Exception", PayLoad);
                }
                return(false);
            }
        }