From fa4479208b30ea3ed451043d8a39e1d739f1ace5 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Thu, 26 Sep 2024 15:24:39 +0100 Subject: [PATCH] chore: fixup base path with relative paths for sub url --- src/Explore.Cli/ExploreHttpClient.cs | 48 +++++++++++++--------------- src/Explore.Cli/Program.cs | 5 +++ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Explore.Cli/ExploreHttpClient.cs b/src/Explore.Cli/ExploreHttpClient.cs index edb6a5b..3e90fa7 100644 --- a/src/Explore.Cli/ExploreHttpClient.cs +++ b/src/Explore.Cli/ExploreHttpClient.cs @@ -9,7 +9,7 @@ public class ExploreHttpClient { private readonly HttpClient _httpClient; - public ExploreHttpClient(string baseAddress = "https://api.explore.swaggerhub.com/spaces-api/v1") + public ExploreHttpClient(string baseAddress = "https://api.explore.swaggerhub.com/spaces-api/v1/") { _httpClient = new HttpClient { BaseAddress = new Uri(baseAddress) }; } @@ -20,17 +20,16 @@ public async Task CheckSpaceExists(string exploreCookie, string? id, bool? { return false; } - + _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var spacesResponse = await _httpClient.GetAsync($"/spaces/{id}"); + var spacesResponse = await _httpClient.GetAsync($"spaces/{id}"); if (spacesResponse.StatusCode == HttpStatusCode.OK) { if (!UtilityHelper.IsContentTypeExpected(spacesResponse.Content.Headers, "application/hal+json")) { - Console.WriteLine(spacesResponse); AnsiConsole.MarkupLine($"[red]Please review your credentials, Unexpected response GET spaces endpoint[/]"); throw new HttpRequestException("Please review your credentials, Unexpected response GET spaces endpoint"); } @@ -52,11 +51,11 @@ public async Task CheckApiExists(string exploreCookie, string spaceId, str { return false; } - + _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var spacesResponse = await _httpClient.GetAsync($"/spaces/{spaceId}/apis/{id}"); + var spacesResponse = await _httpClient.GetAsync($"spaces/{spaceId}/apis/{id}"); if (spacesResponse.StatusCode == HttpStatusCode.OK) { @@ -79,11 +78,11 @@ public async Task CheckConnectionExists(string exploreCookie, string space } - + _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var response = await _httpClient.GetAsync($"/spaces/{spaceId}/apis/{apiId}/connections/{id}"); + var response = await _httpClient.GetAsync($"spaces/{spaceId}/apis/{apiId}/connections/{id}"); if (response.StatusCode == HttpStatusCode.OK) { @@ -108,7 +107,7 @@ public async Task UpsertSpace(string exploreCookie, bool spaceExi Name = name } ), Encoding.UTF8, "application/json"); - + _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); @@ -116,11 +115,11 @@ public async Task UpsertSpace(string exploreCookie, bool spaceExi if (string.IsNullOrEmpty(id) || !spaceExists) { - spacesResponse = await _httpClient.PostAsync("/spaces", spaceContent); + spacesResponse = await _httpClient.PostAsync("spaces", spaceContent); } else { - spacesResponse = await _httpClient.PutAsync($"/spaces/{id}", spaceContent); + spacesResponse = await _httpClient.PutAsync($"spaces/{id}", spaceContent); if (spacesResponse.StatusCode == HttpStatusCode.Conflict) { @@ -136,7 +135,6 @@ public async Task UpsertSpace(string exploreCookie, bool spaceExi if (!UtilityHelper.IsContentTypeExpected(spacesResponse.Content.Headers, "application/hal+json") && !UtilityHelper.IsContentTypeExpected(spacesResponse.Content.Headers, "application/json")) { - Console.WriteLine(spacesResponse); AnsiConsole.MarkupLine($"[red]Please review your credentials, Unexpected response from POST/PUT spaces API for name: {name}, id:{id}[/]"); } else @@ -159,6 +157,7 @@ public async Task UpsertApi(string exploreCookie, bool spaceExists, } ), Encoding.UTF8, "application/json"); + _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); @@ -167,7 +166,7 @@ public async Task UpsertApi(string exploreCookie, bool spaceExists, if (spaceExists && await CheckApiExists(exploreCookie, spaceId, id, verboseOutput)) { // update the api - apiResponse = await _httpClient.PutAsync($"/spaces/{spaceId}/apis/{id}", apiContent); + apiResponse = await _httpClient.PutAsync($"spaces/{spaceId}/apis/{id}", apiContent); if (apiResponse.StatusCode == HttpStatusCode.Conflict) { @@ -178,7 +177,7 @@ public async Task UpsertApi(string exploreCookie, bool spaceExists, else { //create the api - apiResponse = await _httpClient.PostAsync($"/spaces/{spaceId}/apis", apiContent); + apiResponse = await _httpClient.PostAsync($"spaces/{spaceId}/apis", apiContent); } if (apiResponse.IsSuccessStatusCode) @@ -201,7 +200,7 @@ public async Task UpsertApi(string exploreCookie, bool spaceExists, public async Task UpsertConnection(string exploreCookie, bool spaceExists, string spaceId, string apiId, string? connectionId, Connection? connection, bool? verboseOutput) { - + _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); @@ -211,11 +210,11 @@ public async Task UpsertConnection(string exploreCookie, bool spaceExists, if (spaceExists && await CheckConnectionExists(exploreCookie, spaceId, apiId, connectionId, verboseOutput)) { - connectionResponse = await _httpClient.PutAsync($"/spaces/{spaceId}/apis/{apiId}/connections/{connectionId}", connectionContent); + connectionResponse = await _httpClient.PutAsync($"spaces/{spaceId}/apis/{apiId}/connections/{connectionId}", connectionContent); } else { - connectionResponse = await _httpClient.PostAsync($"/spaces/{spaceId}/apis/{apiId}/connections", connectionContent); + connectionResponse = await _httpClient.PostAsync($"spaces/{spaceId}/apis/{apiId}/connections", connectionContent); } if (connectionResponse.IsSuccessStatusCode) @@ -255,7 +254,7 @@ public async Task CreateSpace(string exploreCookie, string sp _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var spacesResponse = await _httpClient.PostAsync("/spaces", spaceContent); + var spacesResponse = await _httpClient.PostAsync("spaces", spaceContent); var spaceResponse = spacesResponse.Content.ReadFromJsonAsync(); switch (spacesResponse.StatusCode) { @@ -263,7 +262,7 @@ public async Task CreateSpace(string exploreCookie, string sp var spaceId = spaceResponse.Result?.Id; return new CreateSpaceResult { - Id = new Guid(), + Id = spaceId, Result = true, StatusCode = spacesResponse.StatusCode }; @@ -313,7 +312,7 @@ public async Task CreateApiEntry(string exploreCookie, Gui _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var apiResponse = await _httpClient.PostAsync($"/spaces/{spaceId}/apis", apiContent); + var apiResponse = await _httpClient.PostAsync($"spaces/{spaceId}/apis", apiContent); switch (apiResponse.StatusCode) { case HttpStatusCode.Created: @@ -349,7 +348,7 @@ public async Task CreateApiConnection(string exploreC _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var connectionResponse = await _httpClient.PostAsync($"/spaces/{spaceId}/apis/{apiId}/connections", connectionContent); + var connectionResponse = await _httpClient.PostAsync($"spaces/{spaceId}/apis/{apiId}/connections", connectionContent); switch (connectionResponse.StatusCode) { @@ -385,12 +384,11 @@ public async Task GetSpaces(string exploreCookie) _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var spacesResponse = await _httpClient.GetAsync("/spaces?page=0&size=2000"); + var spacesResponse = await _httpClient.GetAsync("spaces?page=0&size=2000"); if (spacesResponse.StatusCode == HttpStatusCode.OK) { if (!UtilityHelper.IsContentTypeExpected(spacesResponse.Content.Headers, "application/hal+json")) { - Console.WriteLine(spacesResponse); AnsiConsole.MarkupLine($"[red]Please review your credentials, Unexpected response GET spaces endpoint[/]"); return new GetSpacesResult { @@ -431,7 +429,7 @@ public async Task GetSpaceApis(string exploreCookie, Guid? s _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var apisResponse = await _httpClient.GetAsync($"/spaces/{spaceId}/apis?page=0&size=2000"); + var apisResponse = await _httpClient.GetAsync($"spaces/{spaceId}/apis?page=0&size=2000"); if (apisResponse.StatusCode == HttpStatusCode.OK) { var apis = await apisResponse.Content.ReadFromJsonAsync(); @@ -466,7 +464,7 @@ public async Task GetApiConnectionsForSpace(str _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie); _httpClient.DefaultRequestHeaders.Add("X-Xsrf-Token", $"{UtilityHelper.ExtractXSRFTokenFromCookie(exploreCookie)}"); - var connectionsResponse = await _httpClient.GetAsync($"/spaces/{spaceId}/apis/{apiId}/connections?page=0&size=2000"); + var connectionsResponse = await _httpClient.GetAsync($"spaces/{spaceId}/apis/{apiId}/connections?page=0&size=2000"); if (connectionsResponse.StatusCode == HttpStatusCode.OK) { var connections = await connectionsResponse.Content.ReadFromJsonAsync(); diff --git a/src/Explore.Cli/Program.cs b/src/Explore.Cli/Program.cs index 485b2f8..bc207ca 100644 --- a/src/Explore.Cli/Program.cs +++ b/src/Explore.Cli/Program.cs @@ -696,6 +696,11 @@ internal static async Task ExportSpaces(string exploreCookie, string filePath, s .Select(name => name.Trim()) .ToList(); var spaces = spacesResponse.Spaces; + + if (spaces?.Embedded == null){ + AnsiConsole.MarkupLine($"[red]No spaces to export[/]"); + return; + } var panel = new Panel($"You have [green]{spaces!.Embedded!.Spaces!.Count} spaces[/] in explore"); panel.Width = 100; panel.Header = new PanelHeader("SwaggerHub Explore Data").Centered();