/// <summary> /// Initializes a new instance of the ConnectViewModel class. /// </summary> public ConnectViewModel() { _dialogHandler = new DialogHandler(); Endpoints = new ObservableCollection <ConfigurationWithId>(GlobalStore.Endpoints); SelectedIndex = Endpoints.IndexOf(Endpoints.FirstOrDefault(ep => ep.Equals(GlobalStore.SelectedEndpoint))); SelectCommand = new RelayCommand(async() => { if (SelectedIndex < 0) { return; } ClientResponseWithObject <Status> response = null; bool IsSuccessFul = false; await _dialogHandler.Show(new ProgressDialog(), "ConnectDialog", async(object s, DialogOpenedEventArgs arg) => { try { var statusManager = new StatusManager(Endpoints[SelectedIndex]); response = await statusManager.GetStatusAsync(); IsSuccessFul = response.IsSuccessful; } catch (Exception exception) { IsSuccessFul = false; } finally { arg.Session.Close(); } }); if (IsSuccessFul) { var tauVersion = Assembly.GetExecutingAssembly().GetName().Version; var apiVersion = Version.Parse(response.ApiVersion); var isVersionMismatch = tauVersion.Major != apiVersion.Major || tauVersion.Minor != apiVersion.Minor; object result = null; if (isVersionMismatch) { result = await _dialogHandler.Show(new CommonDialog { DataContext = new CommonDialogViewModel { Buttons = ButtonsEnum.YesNo, Header = "Warning! Version mismatch.", Content = new Message($"Api version: {apiVersion}{Environment.NewLine}Tau version:{tauVersion}.{Environment.NewLine}Would you like to continue?") } }, "ConnectDialog"); } if (!isVersionMismatch || (CommonDialogResult)result == CommonDialogResult.Yes) { GlobalStore.SelectedEndpoint = Endpoints[SelectedIndex]; await((ViewModelLocator)App.Current.Resources["Locator"]).EndpointUpdate(); var mainVindow = new MainWindow(); var connectWindow = App.Current.MainWindow; App.Current.MainWindow = mainVindow; mainVindow.Show(); connectWindow.Close(); } } else { await _dialogHandler.Show( new CommonDialog { DataContext = new CommonDialogViewModel { Header = "Warning!", Content = new Message( $"Failed to connect to selected endpoint.{Environment.NewLine}{(response?.Errors == null ? "" : string.Join(Environment.NewLine, response.Errors.Errors))}"), Buttons = ButtonsEnum.Ok } }, "ConnectDialog"); } }); NewCommand = new RelayCommand(async() => { var context = new CommonDialogViewModel { Buttons = ButtonsEnum.OkCancel, Header = "Create new endpoint", Content = new JContent(new { ApiBaseEndpoint = new Uri("https://europe.slamby.com/"), ApiSecret = "", ParallelLimit = 0, BulkSize = 1000 }) }; var view = new CommonDialog { DataContext = context }; var canClose = false; var result = await _dialogHandler.Show(view, "ConnectDialog", async(object sender, DialogClosingEventArgs args) => { if (!canClose && (CommonDialogResult)args.Parameter == CommonDialogResult.Ok) { args.Cancel(); args.Session.UpdateContent(new ProgressDialog()); var IsSuccessFul = false; var errorMessage = ""; try { var statusManager = new StatusManager(((JContent)context.Content).GetJToken().ToObject <Configuration>()); var response = await statusManager.GetStatusAsync(); IsSuccessFul = response.IsSuccessful; } catch (Exception exception) { IsSuccessFul = false; errorMessage = exception is JsonReaderException ? exception.Message : "Failed to connect to selected endpoint!"; } finally { if (!IsSuccessFul) { context.ErrorMessage = string.IsNullOrEmpty(errorMessage) ? "Failed to connect to selected endpoint!" : errorMessage; context.ShowError = true; args.Session.UpdateContent(view); } else { canClose = true; args.Session.Close((CommonDialogResult)args.Parameter); } } } }); if ((CommonDialogResult)result == CommonDialogResult.Ok) { var newEndpoint = ((JContent)context.Content).GetJToken().ToObject <ConfigurationWithId>(); Endpoints.Add(newEndpoint); GlobalStore.Endpoints = Endpoints.ToList(); } }); EditCommand = new RelayCommand(async() => { if (SelectedIndex < 0) { return; } var isSelectedInUse = Endpoints[SelectedIndex].Equals(GlobalStore.SelectedEndpoint); var context = new CommonDialogViewModel { Buttons = ButtonsEnum.OkCancel, Header = "Create new endpoint", Content = new JContent(new { Endpoints[SelectedIndex].ApiBaseEndpoint, Endpoints[SelectedIndex].ApiSecret, Endpoints[SelectedIndex].ParallelLimit, Endpoints[SelectedIndex].BulkSize }) }; var view = new CommonDialog { DataContext = context }; var canClose = false; var result = await _dialogHandler.Show(view, "ConnectDialog", async(object sender, DialogClosingEventArgs args) => { if (!canClose && (CommonDialogResult)args.Parameter == CommonDialogResult.Ok) { args.Cancel(); args.Session.UpdateContent(new ProgressDialog()); var IsSuccessFul = false; try { var statusManager = new StatusManager(((JContent)context.Content).GetJToken().ToObject <Configuration>()); var response = await statusManager.GetStatusAsync(); IsSuccessFul = response.IsSuccessful; } catch (Exception) { IsSuccessFul = false; } finally { if (!IsSuccessFul) { context.ErrorMessage = "Failed to connect to selected endpoint!"; context.ShowError = true; args.Session.UpdateContent(view); } else { canClose = true; args.Session.Close((CommonDialogResult)args.Parameter); } } } }); if ((CommonDialogResult)result == CommonDialogResult.Ok) { var modifiedEndpoint = ((JContent)context.Content).GetJToken().ToObject <ConfigurationWithId>(); modifiedEndpoint.Id = Endpoints[SelectedIndex].Id; Endpoints[SelectedIndex] = modifiedEndpoint; Endpoints = new ObservableCollection <ConfigurationWithId>(Endpoints); SelectedIndex = Endpoints.IndexOf(modifiedEndpoint); GlobalStore.Endpoints = Endpoints.ToList(); if (isSelectedInUse) { GlobalStore.SelectedEndpoint = Endpoints[SelectedIndex]; await((ViewModelLocator)App.Current.Resources["Locator"]).EndpointUpdate(); var mainVindow = new MainWindow(); var connectWindow = App.Current.MainWindow; App.Current.MainWindow = mainVindow; mainVindow.Show(); connectWindow.Close(); } } }); DeleteCommand = new RelayCommand(async() => { if (SelectedIndex < 0) { return; } var context = new CommonDialogViewModel { Buttons = ButtonsEnum.YesNo, Header = "Delete endpoint", Content = new Message("Are you sure to delete the selected endpoint?") }; var result = await _dialogHandler.Show(new CommonDialog { DataContext = context }, "ConnectDialog"); if ((CommonDialogResult)result == CommonDialogResult.Yes) { Endpoints.RemoveAt(SelectedIndex); SelectedIndex = 0; GlobalStore.Endpoints = Endpoints.ToList(); } }); }
private async void ImportCsv <T>() { var ofd = new OpenFileDialog { Multiselect = false, Filter = "CSV|*.csv" }; if (ofd.ShowDialog() == true) { var importSettings = new CsvImportSettings { Delimiter = ",", Force = true }; var dialogResult = await _dialogHandler.Show( new CommonDialog { DataContext = new CommonDialogViewModel { Header = "Csv Import Settings", Content = importSettings, Buttons = ButtonsEnum.OkCancel } }, "RootDialog"); if ((CommonDialogResult)dialogResult == CommonDialogResult.Cancel) { return; } var invalidRows = new List <int>(); var errors = new ConcurrentBag <string>(); var cancellationToken = new CancellationTokenSource(); var status = new StatusDialogViewModel { ProgressValue = 0, Title = typeof(T) == typeof(Tag) ? "Importing Tags" : "Importing documents", CancellationTokenSource = cancellationToken }; await _dialogHandler.Show(new StatusDialog { DataContext = status }, "RootDialog", async (object sender, DialogOpenedEventArgs oa) => { FileStream stream = null; try { stream = new FileStream(ofd.FileName, FileMode.Open); stream.Position = 0; var importResult = new CsvImportResult(); importSettings.CsvReader = CsvProcesser.GetCsvReader(stream, importSettings); while (importResult.CanContinue && !cancellationToken.IsCancellationRequested) { importResult = await CsvProcesser.GetTokens(importSettings, typeof(T) == typeof(Tag)); invalidRows.AddRange(importResult.InvalidRows); status.ErrorCount += importResult.InvalidRows.Count; if (typeof(T) == typeof(Tag)) { var response = new ClientResponseWithObject <BulkResults>(); try { var settings = new TagBulkSettings(); settings.Tags = importResult.Tokens.Select(t => t.ToObject <Tag>()).ToList(); response = await new TagManager(GlobalStore.SelectedEndpoint, SelectedDataSet.Name).BulkTagsAsync(settings); ResponseValidator.Validate(response, false); } catch (Exception ex) { errors.Add(string.Format("Error during bulk process:{0}{1}: {2}", Environment.NewLine, ex.Message, string.Join(", ", response.Errors))); status.ErrorCount += importResult.Tokens.Count; } finally { if (response.IsSuccessful) { var bulkErrors = response.ResponseObject.Results.Where( br => br.StatusCode != (int)HttpStatusCode.OK).ToList(); if (bulkErrors.Any()) { foreach (var error in bulkErrors) { errors.Add(string.Format("Id: {0}, error: {1}", error.Id, error.Error)); status.ErrorCount++; } } } status.DoneCount += (importResult.Tokens.Count + importResult.InvalidRows.Count); status.ProgressValue = (stream.Position / (double)stream.Length) * 100; importResult.Tokens = null; importResult.InvalidRows = null; } } else { var response = new ClientResponseWithObject <BulkResults>(); try { var settings = new DocumentBulkSettings(); var idFieldIsArray = false; if (SelectedDataSet.SampleDocument != null) { var docObject = JObject.FromObject(SelectedDataSet.SampleDocument); idFieldIsArray = docObject?[SelectedDataSet.TagField] is JArray; } else { var docObject = JObject.FromObject(SelectedDataSet.Schema); idFieldIsArray = docObject?["properties"][SelectedDataSet.TagField]["type"].ToString().ToLower() == "array"; } if (idFieldIsArray) { settings.Documents = importResult.Tokens.Select(t => { t[SelectedDataSet.TagField] = new JArray(t[SelectedDataSet.TagField]); return(t); }).Select(t => t.ToObject <object>()).ToList(); } else { settings.Documents = importResult.Tokens.Select(t => t.ToObject <object>()).ToList(); } response = await new DocumentManager(GlobalStore.SelectedEndpoint, SelectedDataSet.Name).BulkDocumentsAsync(settings); ResponseValidator.Validate(response, false); } catch (Exception ex) { Messenger.Default.Send(ex); status.ErrorCount += GlobalStore.SelectedEndpoint.BulkSize; } finally { if (response.IsSuccessful) { var bulkErrors = response.ResponseObject.Results.Where( br => br.StatusCode != (int)HttpStatusCode.OK).ToList(); if (bulkErrors.Any()) { foreach (var error in bulkErrors) { errors.Add(string.Format("Id: {0}, error: {1}", error.Id, error.Error)); status.ErrorCount++; } } } status.DoneCount += (importResult.Tokens.Count + importResult.InvalidRows.Count); status.ProgressValue = (stream.Position / (double)stream.Length) * 100; importResult.Tokens = null; importResult.InvalidRows = null; } } } } catch (Exception exception) { DispatcherHelper.CheckBeginInvokeOnUI(() => Messenger.Default.Send(exception)); } finally { stream?.Close(); status.OperationIsFinished = true; } }); if (invalidRows.Any()) { Messenger.Default.Send(new Exception($"Invalid rows:{Environment.NewLine}{string.Join(Environment.NewLine, invalidRows)}")); } if (errors.Any()) { Messenger.Default.Send(new Exception($"Errors:{Environment.NewLine}{string.Join(Environment.NewLine, errors)}")); } } }
private async Task Import <T>(List <JToken> tokens) { var cancellationToken = new CancellationTokenSource(); var status = new StatusDialogViewModel { Title = typeof(T) == typeof(Tag) ? "Importing Tags" : "Importing documents", CancellationTokenSource = cancellationToken }; await _dialogHandler.Show(new StatusDialog { DataContext = status }, "RootDialog", async (object sender, DialogOpenedEventArgs oa) => { try { var all = tokens.Count; var done = 0; await Task.Run(() => { try { if (typeof(T) == typeof(Tag)) { var response = new ClientResponseWithObject <BulkResults>(); try { var settings = new TagBulkSettings(); settings.Tags = tokens.Select(t => t.ToObject <Tag>()).ToList(); response = new TagManager(GlobalStore.SelectedEndpoint, SelectedDataSet.Name).BulkTagsAsync(settings).Result; ResponseValidator.Validate(response, false); } catch (Exception ex) { Messenger.Default.Send(ex); status.ErrorCount += tokens.Count; } finally { var bulkErrors = response.ResponseObject?.Results.Where(br => br.StatusCode != (int)HttpStatusCode.OK).ToList(); if (bulkErrors != null && bulkErrors.Any()) { foreach (var error in bulkErrors) { Messenger.Default.Send(new Exception(string.Format("Id: {0}, error: {1}", error.Id, error.Error))); status.ErrorCount++; } } status.DoneCount += tokens.Count; status.ProgressValue = 100; } } else { var remaining = tokens.Count; while ((remaining - GlobalStore.SelectedEndpoint.BulkSize) > 0 && !cancellationToken.IsCancellationRequested) { var response = new ClientResponseWithObject <BulkResults>(); try { var settings = new DocumentBulkSettings(); settings.Documents = tokens.Take(GlobalStore.SelectedEndpoint.BulkSize).Select(t => t.ToObject <object>()).ToList(); response = new DocumentManager(GlobalStore.SelectedEndpoint, SelectedDataSet.Name).BulkDocumentsAsync(settings).Result; ResponseValidator.Validate(response, false); } catch (Exception ex) { Messenger.Default.Send(new Exception(string.Format("Error during bulk process at range [{0}-{1}]", done, done + remaining), ex)); status.ErrorCount += GlobalStore.SelectedEndpoint.BulkSize; } finally { var bulkErrors = response.ResponseObject.Results.Where(br => br.StatusCode != (int)HttpStatusCode.OK).ToList(); if (bulkErrors.Any()) { foreach (var error in bulkErrors) { Messenger.Default.Send(new Exception(string.Format("Id: {0}, error: {1}", error.Id, error.Error))); status.ErrorCount++; } } done += GlobalStore.SelectedEndpoint.BulkSize; status.DoneCount = done; status.ProgressValue = (done / (double)all) * 100; remaining -= GlobalStore.SelectedEndpoint.BulkSize; tokens = tokens.Skip(GlobalStore.SelectedEndpoint.BulkSize).ToList(); } } if (remaining > 0 && !cancellationToken.IsCancellationRequested) { var response = new ClientResponseWithObject <BulkResults>(); try { var settings = new DocumentBulkSettings(); settings.Documents = tokens.Take(remaining).Select(t => t.ToObject <object>()).ToList(); response = new DocumentManager(GlobalStore.SelectedEndpoint, SelectedDataSet.Name).BulkDocumentsAsync(settings).Result; ResponseValidator.Validate(response, false); } catch (Exception ex) { Messenger.Default.Send(new Exception(string.Format("Error during bulk process at range [{0}-{1}]", done, done + remaining), ex)); status.ErrorCount += remaining; } finally { var bulkErrors = response.ResponseObject?.Results.Where(br => br.StatusCode != (int)HttpStatusCode.OK).ToList(); if (bulkErrors != null && bulkErrors.Any()) { foreach (var error in bulkErrors) { Messenger.Default.Send(new Exception(string.Format("Id: {0}, error: {1}", error.Id, error.Error))); status.ErrorCount++; } } done += remaining; status.DoneCount = done; status.ProgressValue = (done / (int)all) * 100; remaining -= remaining; } } } } catch (OperationCanceledException) { Log.Info(LogMessages.OperationCancelled); } }); } catch (Exception exception) { DispatcherHelper.CheckBeginInvokeOnUI(() => Messenger.Default.Send(exception)); } finally { status.OperationIsFinished = true; } }); }
internal async Task <ClientResponseWithObject <T> > SendAsync <T>(HttpMethod method, object body, string urlPart, Dictionary <string, string> queryParameters, Dictionary <string, string> headers, bool useGzip = false) { ClientResponseWithObject <T> clientResponse = null; var messageHandler = new ApiHttpMessageHandler(useGzip); var jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.Converters.Add(new StringEnumConverter()); using (var client = new HttpClient(messageHandler)) { client.BaseAddress = _configuration.ApiBaseEndpoint; client.Timeout = _configuration.Timeout; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.TryAddWithoutValidation(Constants.AuthorizationHeader, string.Format("{0} {1}", Constants.AuthorizationMethodSlamby, _configuration.ApiSecret)); client.DefaultRequestHeaders.TryAddWithoutValidation(Constants.SdkVersionHeader, GetType().GetTypeInfo().Assembly.GetName().Version.ToString()); if (_configuration.ParallelLimit > 0) { client.DefaultRequestHeaders.TryAddWithoutValidation(Constants.ApiParallelLimitHeader, _configuration.ParallelLimit.ToString()); } if (useGzip) { client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); } if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } HttpContent content = null; if (body != null) { var json = JsonConvert.SerializeObject(body, jsonSerializerSettings); content = new StringContent(json, Encoding.UTF8, "application/json"); if (useGzip) { content = new CompressedContent(content, "gzip"); } } else { content = new StringContent(string.Empty, Encoding.UTF8, "application/json"); } HttpResponseMessage responseMsg = null; var appendedUri = _appendQueryString(_endpoint, urlPart, queryParameters); if (method == HttpMethod.Get) { responseMsg = await client.GetAsync(appendedUri); } else if (method == HttpMethod.Post) { responseMsg = await client.PostAsync(appendedUri, content); } else if (method == HttpMethod.Delete) { responseMsg = await client.DeleteAsync(appendedUri); } else if (method == HttpMethod.Put) { responseMsg = await client.PutAsync(appendedUri, content); } else { throw new NotImplementedException(); } clientResponse = new ClientResponseWithObject <T> { HttpStatusCode = responseMsg.StatusCode, IsSuccessful = responseMsg.IsSuccessStatusCode, ServerMessage = responseMsg.ReasonPhrase }; var respString = await responseMsg.Content.ReadAsStringAsync(); try { if (!string.IsNullOrEmpty(respString)) { if (responseMsg.IsSuccessStatusCode) { clientResponse.ResponseObject = JsonConvert.DeserializeObject <T>(respString, jsonSerializerSettings); } else { clientResponse.Errors = JsonConvert.DeserializeObject <ErrorsModel>(respString, jsonSerializerSettings); } } } catch (JsonSerializationException ex) { Debug.WriteLine(ex.Message); clientResponse.IsSuccessful = false; clientResponse.Errors = ErrorsModel.Create("Response is not a Valid or Expected Type of JSON:\n" + respString); } clientResponse.ApiVersion = responseMsg.Headers.Where(header => string.Equals(header.Key, Constants.ApiVersionHeader, StringComparison.OrdinalIgnoreCase)) .SelectMany(header => header.Value) .FirstOrDefault() ?? string.Empty; return(clientResponse); } }