public void SendAsync(Projects project, object record) { Task.Run(async() => { try { var deviceInfo = _deviceInfoProvider.GetDeviceInfo(); var model = new PandoraModel { Identifier = $"{deviceInfo.Id}{SecurityProvider.GetHashString(project.ToString())}", Record = new Record { DeviceInfo = deviceInfo, Data = record } }; var modelData = await _jsonProvider.SerializeAsync(model); // If there is network connection we try to send statistic data. if (_networkProvider.IsConnected()) { // Trying to get data that wasn't sent to the server by some reason. var notSendData = _fileSystemProvider.ReadFile(project.ToString()); var data = modelData; if (!string.IsNullOrEmpty(notSendData)) { data = modelData.Insert(0, notSendData); } var content = new StringContent(data, Encoding.UTF8, "application/json"); var result = await _httpClient.PostAsync(Constants.ApiStatisticBaseUrl, content); if (result.IsSuccessStatusCode) { _fileSystemProvider.DeleteFile(project.ToString()); } else { _fileSystemProvider.AppendToFile(project.ToString(), modelData); } } else { _fileSystemProvider.AppendToFile(project.ToString(), modelData); } } catch { // Ignore any exception. } }); }
private async Task <T> GetDataAsync <T>(string cacheKey, string url) where T : class { T result = null; var cacheData = _cacheDataProvider.Get(cacheKey); try { if (string.IsNullOrEmpty(cacheData)) { if (_networkProvider.IsConnected()) { var requestUrl = url.Contains(Constants.ApiAutoKey) ? $"{Constants.ApiAutoBaseUrl}{url}" : $"{Constants.ApiAutoBaseUrl}{url}?api_key={Constants.ApiAutoKey}"; var response = await _httpClient.GetAsync(requestUrl); if (response.IsSuccessStatusCode) { cacheData = await response.Content.ReadAsStringAsync(); _cacheDataProvider.Set(cacheKey, cacheData); result = await _jsonProvider.DeserializeAsync <T>(cacheData); } else { _statisticDataProvider.SendAsync(Projects.AutoLogs, response); } } } else { result = await _jsonProvider.DeserializeAsync <T>(cacheData); } } catch (Exception ex) { _statisticDataProvider.SendAsync(Projects.AutoLogs, ex); } return(result); }