/// <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 = false) { var countlyResponse = new CountlyResponse(); try { if (addToRequestQueue) { throw new Exception("Request added to queue."); } 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; #if UNITY_EDITOR //Log to Unity Console if (_config.EnableConsoleErrorLogging) { Debug.Log(countlyResponse.IsSuccess); } #endif return(countlyResponse); } } catch (Exception ex) { addToRequestQueue = true; countlyResponse.ErrorMessage = ex.Message; } if (addToRequestQueue) { var requestModel = new CountlyRequestModel(false, uri, data, DateTime.UtcNow); AddRequestToQueue(requestModel); } return(countlyResponse); }
private void AddRequestToQueue(CountlyRequestModel request) { if (_requestRepo.Count == _config.StoredRequestLimit) { _requestRepo.Dequeue(); } _requestRepo.Enqueue(request); }
/// <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); }
private void ProcessRequest(CountlyRequestModel model) { if (model.IsRequestGetType) { Task.Run(() => GetAsync(model.RequestUrl)); } else { Task.Run(() => PostAsync(model.RequestUrl, model.RequestData)); } }
public static RequestEntity ConvertRequestModelToRequestEntity(CountlyRequestModel model, long id) { var json = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); model.Id = id; return(new RequestEntity { Id = id, Json = json }); }
/// <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 = false) { var countlyResponse = new CountlyResponse(); try { if (addToRequestQueue) { throw new Exception("Request added to queue."); } 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; #if UNITY_EDITOR //Log to Unity Console if (_config.EnableConsoleErrorLogging) { Debug.Log(countlyResponse.IsSuccess); } #endif return(countlyResponse); } } catch (Exception ex) { addToRequestQueue = true; countlyResponse.ErrorMessage = ex.Message; } if (addToRequestQueue) { var requestModel = new CountlyRequestModel(true, url, null, DateTime.UtcNow); AddRequestToQueue(requestModel); } return(countlyResponse); }
/// <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); }