-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new language features to clean code
- Loading branch information
Showing
4 changed files
with
91 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
namespace ConsoleApp.Models.Configuration | ||
namespace ConsoleApp.Models.Configuration; | ||
|
||
public class ConsoleAppSettings | ||
{ | ||
public class ConsoleAppSettings | ||
{ | ||
public string? WelcomeMessage { get; set; } | ||
} | ||
public string? WelcomeMessage { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,52 @@ | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json.Nodes; | ||
using ConsoleApp.Models.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace ConsoleApp.Services | ||
namespace ConsoleApp.Services; | ||
|
||
internal interface IDemoService | ||
{ | ||
internal interface IDemoService | ||
{ | ||
void WriteWelcomeMessage(); | ||
Task<string> MakeGraphApiCall(); | ||
} | ||
void WriteWelcomeMessage(); | ||
Task<string> MakeGraphApiCall(); | ||
} | ||
|
||
internal class DemoService : IDemoService | ||
internal class DemoService(ILogger<DemoService> logger, | ||
IClientCredentialService clientCredentialService, | ||
IOptions<ConsoleAppSettings> consoleAppSettings, | ||
IHttpClientFactory httpClientFactory) : IDemoService | ||
{ | ||
private readonly ILogger<DemoService> _logger = logger; | ||
private readonly IClientCredentialService _clientCredentialService = clientCredentialService; | ||
private readonly IOptions<ConsoleAppSettings> _consoleAppSettings = consoleAppSettings; | ||
private readonly IHttpClientFactory _httpClientFactory = httpClientFactory; | ||
|
||
public async Task<string> MakeGraphApiCall() | ||
{ | ||
private readonly ILogger<DemoService> _logger; | ||
private readonly IClientCredentialService _clientCredentialService; | ||
private readonly IOptions<ConsoleAppSettings> _consoleAppSettings; | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public DemoService(ILogger<DemoService> logger, | ||
IClientCredentialService clientCredentialService, | ||
IOptions<ConsoleAppSettings> consoleAppSettings, | ||
IHttpClientFactory httpClientFactory) | ||
{ | ||
_logger = logger; | ||
_clientCredentialService = clientCredentialService; | ||
_consoleAppSettings = consoleAppSettings; | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
var accessToken = await _clientCredentialService.GetAccessToken( | ||
["https://graph.microsoft.com/.default"]); | ||
|
||
public async Task<string> MakeGraphApiCall() | ||
{ | ||
var accessToken = await _clientCredentialService.GetAccessToken( | ||
new[] { "https://graph.microsoft.com/.default" }); | ||
|
||
var httpClient = _httpClientFactory.CreateClient("GraphApi"); | ||
httpClient.DefaultRequestHeaders.Authorization = | ||
new AuthenticationHeaderValue("Bearer", accessToken); | ||
|
||
var response = await httpClient.GetAsync("/v1.0/users"); | ||
|
||
JsonNode? result = null; | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var json = await response.Content.ReadAsStringAsync(); | ||
result = JsonNode.Parse(json); | ||
} | ||
return result is null | ||
? "" | ||
: result.ToJsonString(); | ||
} | ||
var httpClient = _httpClientFactory.CreateClient("GraphApi"); | ||
httpClient.DefaultRequestHeaders.Authorization = | ||
new AuthenticationHeaderValue("Bearer", accessToken); | ||
|
||
public void WriteWelcomeMessage() | ||
var response = await httpClient.GetAsync("/v1.0/users"); | ||
|
||
JsonNode? result = null; | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
_logger.LogInformation("Saying hello"); | ||
Console.WriteLine(_consoleAppSettings.Value?.WelcomeMessage); | ||
var json = await response.Content.ReadAsStringAsync(); | ||
result = JsonNode.Parse(json); | ||
} | ||
return result is null | ||
? "" | ||
: result.ToJsonString(); | ||
} | ||
} | ||
|
||
public void WriteWelcomeMessage() | ||
{ | ||
_logger.LogInformation("Saying hello"); | ||
Console.WriteLine(_consoleAppSettings.Value?.WelcomeMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,27 @@ | ||
using ConsoleApp.Services; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace ConsoleApp | ||
{ | ||
internal class WorkerService : IHostedService | ||
{ | ||
private readonly IDemoService _demoService; | ||
namespace ConsoleApp; | ||
|
||
public WorkerService(IDemoService demoService) | ||
{ | ||
_demoService = demoService; | ||
} | ||
internal class WorkerService(IDemoService demoService) : IHostedService | ||
{ | ||
private readonly IDemoService _demoService = demoService; | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var response = await _demoService.MakeGraphApiCall(); | ||
if (!string.IsNullOrWhiteSpace(response)) | ||
{ | ||
var response = await _demoService.MakeGraphApiCall(); | ||
if (!string.IsNullOrWhiteSpace(response)) | ||
{ | ||
Console.WriteLine(response); | ||
} | ||
|
||
while (true) | ||
{ | ||
_demoService.WriteWelcomeMessage(); | ||
await Task.Delay(3000); | ||
} | ||
Console.WriteLine(response); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
=> Task.CompletedTask; | ||
while (true) | ||
{ | ||
_demoService.WriteWelcomeMessage(); | ||
await Task.Delay(3000); | ||
} | ||
} | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
=> Task.CompletedTask; | ||
} |