feat: created auth hook on TClient
This commit is contained in:
@@ -14,6 +14,7 @@ public class HeadHunterService(
|
||||
IChannel channel,
|
||||
ILogger<HeadHunterService> logger,
|
||||
IOptions<HeadHunterConfig> config,
|
||||
IWebHostEnvironment env,
|
||||
AppDbContext dbContext)
|
||||
{
|
||||
public enum Status
|
||||
@@ -47,45 +48,54 @@ public class HeadHunterService(
|
||||
return Status.UserAuthRejectedError;
|
||||
}
|
||||
|
||||
using var client = new HttpClient();
|
||||
var form = new Dictionary<string, string>
|
||||
HeadHunterTokenResponseDto? responseDto;
|
||||
if (!env.IsDevelopment()) //Production server
|
||||
{
|
||||
{ "client_id", _config.ClientId },
|
||||
{ "client_secret", _config.Secret },
|
||||
{ "code", authorizationCode },
|
||||
{ "grant_type", "authorization_code" },
|
||||
{ "redirect_uri", GetRedirectUrl(userId) }
|
||||
};
|
||||
client.BaseAddress = new UriBuilder(_config.Links.HeadHunterApiDomain)
|
||||
{
|
||||
Port = -1,
|
||||
Scheme = "https"
|
||||
}.Uri;
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Jobot BackEnd Service");
|
||||
|
||||
using var res = await client.SendAsync(
|
||||
new HttpRequestMessage(
|
||||
HttpMethod.Post,
|
||||
_config.Links.HeadHunterTokenRoute)
|
||||
using var client = new HttpClient();
|
||||
var form = new Dictionary<string, string>
|
||||
{
|
||||
Content = new FormUrlEncodedContent(form)
|
||||
});
|
||||
{ "client_id", _config.ClientId },
|
||||
{ "client_secret", _config.Secret },
|
||||
{ "code", authorizationCode },
|
||||
{ "grant_type", "authorization_code" },
|
||||
{ "redirect_uri", GetRedirectUrl(userId) }
|
||||
};
|
||||
client.BaseAddress = new UriBuilder(_config.Links.HeadHunterApiDomain)
|
||||
{
|
||||
Port = -1,
|
||||
Scheme = "https"
|
||||
}.Uri;
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Jobot BackEnd Service");
|
||||
|
||||
if (!res.IsSuccessStatusCode)
|
||||
{
|
||||
logger.LogWarning($"Response of HttpRequest {_config.Links.HeadHunterApiDomain}" +
|
||||
$"{_config.Links.HeadHunterTokenRoute} has unsuccessful status code {res.StatusCode}");
|
||||
logger.LogWarning($"{res.Content.ReadAsStringAsync().Result}");
|
||||
return Status.HeadHunterAuthRejectedError;
|
||||
using var res = await client.SendAsync(
|
||||
new HttpRequestMessage(
|
||||
HttpMethod.Post,
|
||||
_config.Links.HeadHunterTokenRoute)
|
||||
{
|
||||
Content = new FormUrlEncodedContent(form)
|
||||
});
|
||||
|
||||
if (!res.IsSuccessStatusCode)
|
||||
{
|
||||
logger.LogWarning($"Response of HttpRequest {_config.Links.HeadHunterApiDomain}" +
|
||||
$"{_config.Links.HeadHunterTokenRoute} has unsuccessful status code {res.StatusCode}");
|
||||
logger.LogWarning($"{res.Content.ReadAsStringAsync().Result}");
|
||||
return Status.HeadHunterAuthRejectedError;
|
||||
}
|
||||
|
||||
responseDto =
|
||||
JsonSerializer.Deserialize<HeadHunterTokenResponseDto>(await res.Content.ReadAsStringAsync());
|
||||
|
||||
if (responseDto == null)
|
||||
{
|
||||
logger.LogWarning($"User {userId} auth completed with error " +
|
||||
$"{nameof(Status.HeadHunterResponseDeserializationFailedError)}");
|
||||
return Status.HeadHunterResponseDeserializationFailedError;
|
||||
}
|
||||
}
|
||||
|
||||
var responseDto = JsonSerializer.Deserialize<HeadHunterTokenResponseDto>(await res.Content.ReadAsStringAsync());
|
||||
|
||||
if (responseDto == null)
|
||||
else
|
||||
{
|
||||
logger.LogWarning($"User {userId} auth completed with error " +
|
||||
$"{nameof(Status.HeadHunterResponseDeserializationFailedError)}");
|
||||
return Status.HeadHunterResponseDeserializationFailedError;
|
||||
responseDto = new HeadHunterTokenResponseDto("testtoken", 0, "testtoken", "--");
|
||||
}
|
||||
|
||||
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == userId);
|
||||
|
||||
@@ -19,9 +19,9 @@ public class Startup(IConfiguration configuration)
|
||||
services.AddControllers();
|
||||
services.AddLogging();
|
||||
|
||||
await using var rabbitMqConnection = await new ConnectionFactory
|
||||
var rabbitMqConnection = await new ConnectionFactory
|
||||
{
|
||||
HostName = "jobot-rabbitmq"
|
||||
HostName = "rabbitmq"
|
||||
}.CreateConnectionAsync();
|
||||
var channel = await rabbitMqConnection.CreateChannelAsync();
|
||||
await channel.QueueDeclareAsync(
|
||||
|
||||
Reference in New Issue
Block a user