public Task<ResponseMessage> SendHistory(History history)
 {
     if (APIInvalid)
     {
         LastCallResult = NetworkResult.AuthenticationFailed;
         return Task.FromResult(new ResponseMessage() { IsSuccess = false });
     }
     if (FailNetwork)
     {
         LastCallResult = NetworkResult.NetworkError;
         throw new IOException();
     }
     if (UnknownError)
     {
         LastCallResult = NetworkResult.UnknownError;
         throw new Exception("ups");
     }
     if (history != null)
     {
         HistoryEvents.AddRange(history.Events);
         HistoryActions.AddRange(history.Actions);
     }
     LastCallResult = NetworkResult.Success;
     return Task.FromResult(new ResponseMessage() {IsSuccess = true});
 }
        public async Task<bool> FlushHistory()
        {
            try
            {
                History history = new History();
                history.Actions = await Storage.GetUndeliveredActions();
                history.Events = await Storage.GetUndeliveredEvents();

                if ((history.Events != null && history.Events.Count > 0) || (history.Actions != null && history.Actions.Count > 0))
                {
                    var responseMessage = await ExecuteCall(async () => await ServiceManager.ApiConnction.SendHistory(history));

                    if (responseMessage.IsSuccess)
                    {
                        if (history.Events != null && history.Events.Count > 0)
                        {
                            await Storage.SetEventsAsDelivered(history.Events);
                        }

                        if (history.Actions != null && history.Actions.Count > 0)
                        {
                            await Storage.SetActionsAsDelivered(history.Actions);
                        }
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error while sending history: " + ex.Message, ex);
            }
            return false;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends the given History object to the cloud.
        /// </summary>
        /// <param name="history">History object to send.</param>
        public async Task<ResponseMessage> SendHistory(History history)
        {
            try
            {
                Logger.Trace("SendHistory");
                System.Net.Http.HttpClient apiConnection = new System.Net.Http.HttpClient();
                apiConnection.DefaultRequestHeaders.Add(Constants.XApiKey, Configuration.ApiKey);
                apiConnection.DefaultRequestHeaders.Add(Constants.Xiid, SdkData.DeviceId);
                apiConnection.DefaultRequestHeaders.Add(Constants.AdvertisementIdentifierHeader,
                    string.IsNullOrEmpty(Configuration.UserId) ? Windows.System.UserProfile.AdvertisingManager.AdvertisingId : Configuration.UserId);
                apiConnection.DefaultRequestHeaders.TryAddWithoutValidation(Constants.XUserAgent, UserAgentBuilder.BuildUserAgentJson());
                string serializeObject = JsonConvert.SerializeObject(history);
                var content = new StringContent(serializeObject, Encoding.UTF8, "application/json");

                    System.Net.Http.HttpResponseMessage responseMessage = await apiConnection.PostAsync(new Uri(Configuration.LayoutUri), content);

                Logger.Trace("SendHistory HttpCode: {0}", responseMessage.StatusCode);
                if (responseMessage.IsSuccessStatusCode)
                {
                    LastCallResult = NetworkResult.Success;
                    return new ResponseMessage()
                    {
                        Content = responseMessage.Content.ToString(),
                        Header = responseMessage.Headers.ToString(),
                        StatusCode = Convert(responseMessage.StatusCode),
                        IsSuccess = responseMessage.IsSuccessStatusCode
                    };
                }
                LastCallResult = NetworkResult.UnknownError;
                return new ResponseMessage() {StatusCode = Convert(responseMessage.StatusCode), IsSuccess = responseMessage.IsSuccessStatusCode};
            }
            catch (Exception ex)
            {
                Logger.Error("SendHistory Error", ex);
                LastCallResult = NetworkResult.NetworkError;
                return new ResponseMessage() {IsSuccess = false};
            }
        }