/// <summary> /// Generates the postman collection. /// </summary> /// <param name="redirects">The redirects.</param> /// <param name="redirectType"></param> /// <returns>System.String.</returns> private string GeneratePostmanCollection(Redirect[] redirects, RedirectType redirectType) { var jsonItemList = new List <PostmanCollectionItemJsonData>(); var index = 0; var selectedSkipValue = CreateForEveryComboBox.SelectedIndex; foreach (var redirect in redirects) { if (selectedSkipValue == 10 || selectedSkipValue == index) { var oldUrl = redirect.OldUrl.Replace("\\", "/").Replace("\"", ""); var newUrl = redirect.NewUrl.Replace("\\", "/").Replace("\"", ""); var domainInfo = Helpers.GetDomainInfo(oldUrl); var hostSegments = domainInfo.Domain.Split('.'); var urlSegments = domainInfo.Path.Split('/'); newUrl = string.IsNullOrEmpty(newUrl) ? "/" : newUrl; if (!newUrl.StartsWith("http://") && !newUrl.StartsWith("https://")) { var targetSiteUrl = $"https://{domainInfo.Domain}"; newUrl = targetSiteUrl + newUrl; } jsonItemList.Add(new PostmanCollectionItemJsonData { Name = oldUrl, Guid = Guid.NewGuid().ToString("D"), StatusCode = redirectType.StatusCode, FullToUrl = newUrl, FullFromUrl = oldUrl, Protocol = domainInfo.HttpMode, Hosts = hostSegments, Paths = urlSegments }); } if (index == 9) { index = 0; } else { index++; } } var data = new PostmanCollectionJsonData { Name = CollectionNameTextBox.Text.Trim(), Guid = Guid.NewGuid().ToString("D"), Items = jsonItemList }; return(PostmanGenerator.GetCollectionJson(data)); }
public static string GetCollectionJson(PostmanCollectionJsonData data) { if (!string.IsNullOrWhiteSpace(CollectionJson) && !string.IsNullOrWhiteSpace(ItemJson)) { var itemStringBuilderList = new List <string>(); foreach (var postmanCollectionItemJsonData in data.Items) { var item = string.Copy(ItemJson); var hosts = GetArrayAsJson(postmanCollectionItemJsonData.Hosts); var paths = GetArrayAsJson(postmanCollectionItemJsonData.Paths); item = item.Replace("{NAME}", postmanCollectionItemJsonData.Name); item = item.Replace("{GUID}", postmanCollectionItemJsonData.Guid); item = item.Replace("{STATUSCODE}", postmanCollectionItemJsonData.StatusCode); item = item.Replace("{FULLTOURL}", postmanCollectionItemJsonData.FullToUrl); item = item.Replace("{FULLFROMUL}", postmanCollectionItemJsonData.FullFromUrl); item = item.Replace("{PROTOCOL}", postmanCollectionItemJsonData.Protocol); item = item.Replace("{HOSTS}", hosts); item = item.Replace("{PATHS}", paths); itemStringBuilderList.Add(item); } var itemsString = string.Join(",", itemStringBuilderList); var collectionString = string.Copy(CollectionJson); collectionString = collectionString.Replace("{NAME}", data.Name); collectionString = collectionString.Replace("{GUID}", data.Guid); collectionString = collectionString.Replace("{ITEMS}", itemsString); return(collectionString); } return(string.Empty); }