Skip to content

Commit

Permalink
chore: fixup base path with relative paths for sub url
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Sep 26, 2024
1 parent a110d5b commit fa44792
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/Explore.Cli/ExploreHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
Expand All @@ -20,17 +20,16 @@ public async Task<bool> 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");
}
Expand All @@ -52,11 +51,11 @@ public async Task<bool> 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)
{
Expand All @@ -79,11 +78,11 @@ public async Task<bool> 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)
{
Expand All @@ -108,19 +107,19 @@ public async Task<SpaceResponse> 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)}");

HttpResponseMessage? spacesResponse;

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)
{
Expand All @@ -136,7 +135,6 @@ public async Task<SpaceResponse> 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
Expand All @@ -159,6 +157,7 @@ public async Task<ApiResponse> 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)}");

Expand All @@ -167,7 +166,7 @@ public async Task<ApiResponse> 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)
{
Expand All @@ -178,7 +177,7 @@ public async Task<ApiResponse> 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)
Expand All @@ -201,7 +200,7 @@ public async Task<ApiResponse> UpsertApi(string exploreCookie, bool spaceExists,
public async Task<bool> 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)}");

Expand All @@ -211,11 +210,11 @@ public async Task<bool> 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)
Expand Down Expand Up @@ -255,15 +254,15 @@ public async Task<CreateSpaceResult> 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<SpaceResponse>();
switch (spacesResponse.StatusCode)
{
case HttpStatusCode.Created:
var spaceId = spaceResponse.Result?.Id;
return new CreateSpaceResult
{
Id = new Guid(),
Id = spaceId,
Result = true,
StatusCode = spacesResponse.StatusCode
};
Expand Down Expand Up @@ -313,7 +312,7 @@ public async Task<CreateApiEntryResult> 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:
Expand Down Expand Up @@ -349,7 +348,7 @@ public async Task<CreateApiConnectionResult> 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)
{
Expand Down Expand Up @@ -385,12 +384,11 @@ public async Task<GetSpacesResult> 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
{
Expand Down Expand Up @@ -431,7 +429,7 @@ public async Task<GetSpaceApisResult> 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<PagedApis>();
Expand Down Expand Up @@ -466,7 +464,7 @@ public async Task<GetApiConnectionsForSpaceResult> 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<PagedConnections>();
Expand Down
5 changes: 5 additions & 0 deletions src/Explore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fa44792

Please sign in to comment.