Удаление restsharp и использование обычного httpClient #22

Merged
on.nemtina merged 5 commits from fix/#883-remove-restsharp into master 2025-02-03 16:21:32 +05:00
2 changed files with 6 additions and 6 deletions
Showing only changes of commit b129f22b73 - Show all commits

View File

@ -19,7 +19,7 @@ public static class ApiTokenHelper
/// <param name="httpClient"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static async Task Authorize(this HttpClient httpClient, IConfiguration configuration)
public static void Authorize(this HttpClient httpClient, IConfiguration configuration)
{
var authUser = configuration
.GetSection(nameof(AuthUser))
@ -30,7 +30,7 @@ public static class ApiTokenHelper
var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get<string>() ?? string.Empty;
var jwtToken = needUseKeyCloak
? await authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl)
? authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl)
: authUser.CreateDefaultJwtToken();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
@ -73,7 +73,7 @@ public static class ApiTokenHelper
/// <param name="authUser"></param>
/// <param name="keycloakGetTokenUrl"></param>
/// <returns></returns>
private static async Task<string> CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl)
private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl)
{
var sharedClient = new HttpClient();
var parameters = new Dictionary<string, string> {
@ -84,11 +84,11 @@ public static class ApiTokenHelper
};
var encodedContent = new FormUrlEncodedContent(parameters);
using HttpResponseMessage response = await sharedClient.PostAsync(keycloakGetTokenUrl, encodedContent);
using HttpResponseMessage response = sharedClient.PostAsync(keycloakGetTokenUrl, encodedContent).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode == true)
{
var data = await response.Content.ReadAsStreamAsync();
var data = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
var token = JsonSerializer.Deserialize<JwtToken>(data)!;
return token.AccessToken;
}

View File

@ -19,7 +19,7 @@ namespace DD.Persistence.IntegrationTests
public HttpClient CreateClient(string name)
{
var client = factory.CreateClient();
client.Authorize(configuration).GetAwaiter().GetResult();
client.Authorize(configuration);
return client;
}