Exemplo n.º 1
0
        /// <summary>
        ///     Makes an Asynchronous GET request to the Countly server.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="addToRequestQueue"></param>
        /// <returns></returns>
        internal async Task <CountlyResponse> GetAsync(string url, bool addToRequestQueue = true)
        {
            if (_config.EnableConsoleLogging)
            {
                Debug.Log("[Countly] RequestCountlyHelper GetAsync: " + url);
            }

            if (_config.EnableTestMode)
            {
                return(new CountlyResponse
                {
                    IsSuccess = true
                });
            }

            var countlyResponse = new CountlyResponse();

            if (!addToRequestQueue)
            {
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(url);
                    using (var response = (HttpWebResponse)await request.GetResponseAsync())
                        using (var stream = response.GetResponseStream())
                            using (var reader = new StreamReader(stream))
                            {
                                var res = await reader.ReadToEndAsync();

                                countlyResponse.IsSuccess = !string.IsNullOrEmpty(res);
                                countlyResponse.Data      = res;
                            }
                }
                catch (Exception ex)
                {
                    addToRequestQueue            = true;
                    countlyResponse.ErrorMessage = ex.Message;
                }
            }

            if (addToRequestQueue)
            {
                var requestModel = new CountlyRequestModel(true, url, null, DateTime.UtcNow);
                AddRequestToQueue(requestModel);

                if (_config.EnableConsoleLogging)
                {
                    Debug.Log("[Countly] RequestCountlyHelper: Added to Request Queue");
                }
            }

            if (_config.EnableConsoleLogging)
            {
                Debug.Log("[Countly] RequestCountlyHelper request: " + url + " response: " + countlyResponse.ToString());
            }

            return(countlyResponse);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Makes an Asynchronous POST request to the Countly server.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <param name="addToRequestQueue"></param>
        /// <returns></returns>
        private async Task <CountlyResponse> PostAsync(string uri, string data, bool addToRequestQueue = true)
        {
            if (_config.EnableConsoleLogging)
            {
                Debug.Log("[Countly] RequestCountlyHelper Post: " + uri + data);
            }

            if (_config.EnableTestMode)
            {
                return(new CountlyResponse
                {
                    IsSuccess = true
                });
            }


            var countlyResponse = new CountlyResponse();

            if (!addToRequestQueue)
            {
                try
                {
                    var dataBytes = Encoding.UTF8.GetBytes(data);

                    var request = (HttpWebRequest)WebRequest.Create(uri);
                    request.ContentLength = dataBytes.Length;
                    request.ContentType   = "application/json";
                    request.Method        = "POST";


                    using (var requestBody = request.GetRequestStream())
                    {
                        await requestBody.WriteAsync(dataBytes, 0, dataBytes.Length);
                    }

                    using (var response = (HttpWebResponse)await request.GetResponseAsync())
                        using (var stream = response.GetResponseStream())
                            using (var reader = new StreamReader(stream))
                            {
                                var res = await reader.ReadToEndAsync();

                                countlyResponse.IsSuccess = !string.IsNullOrEmpty(res);
                                countlyResponse.Data      = res;
                            }
                }
                catch (Exception ex)
                {
                    addToRequestQueue            = true;
                    countlyResponse.ErrorMessage = ex.Message;
                }
            }

            if (addToRequestQueue)
            {
                var requestModel = new CountlyRequestModel(false, uri, data, DateTime.UtcNow);
                AddRequestToQueue(requestModel);

                if (_config.EnableConsoleLogging)
                {
                    Debug.Log("[Countly] RequestCountlyHelper: Added to Request Queue");
                }
            }

            if (_config.EnableConsoleLogging)
            {
                Debug.Log("[Countly] RequestCountlyHelper request: " + uri + " response: " + countlyResponse.ToString());
            }

            return(countlyResponse);
        }