Skip to content

Commit

Permalink
Merge pull request #37 from SmartBear-DevRel/refactor/use_explore_htt…
Browse files Browse the repository at this point in the history
…p_client

Refactor/use explore http client
  • Loading branch information
YOU54F authored Sep 26, 2024
2 parents 5ec2fd0 + 7ade6c6 commit f6d3b4b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 430 deletions.
52 changes: 23 additions & 29 deletions src/Explore.Cli/ExploreHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using Explore.Cli.Models.Explore;
using System.Threading.Tasks;
using System.CommandLine;
using System.Net;
using System.Net.Http.Json;
using Spectre.Console;
Expand All @@ -13,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 @@ -24,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 @@ -56,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 @@ -83,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 @@ -112,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 @@ -140,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 @@ -163,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 @@ -171,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 @@ -182,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 @@ -205,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 @@ -215,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 @@ -259,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 @@ -317,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 @@ -353,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 @@ -389,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 @@ -435,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 @@ -470,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
Loading

0 comments on commit f6d3b4b

Please sign in to comment.