forked from juunas11/Joonasw.AzureAdApiSample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoApiClient.cs
106 lines (94 loc) · 4.11 KB
/
TodoApiClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
namespace Joonasw.AzureAdApiSample.ConsoleNativeClient
{
public class TodoApiClient
{
private static readonly HttpClient Client = new HttpClient();
private readonly ClientSettings _settings;
public TodoApiClient(ClientSettings settings)
{
_settings = settings;
}
public async Task ListTodosAsync()
{
using (var req = new HttpRequestMessage(HttpMethod.Get, $"{_settings.ApiBaseUrl}/api/todos"))
{
string accessToken = await GetAccessTokenAsync();
req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (HttpResponseMessage res = await Client.SendAsync(req))
{
res.EnsureSuccessStatusCode();
string json = await res.Content.ReadAsStringAsync();
List<TodoItem> todos = JsonConvert.DeserializeObject<List<TodoItem>>(json);
ListTodosOnConsole(todos);
}
}
}
private void ListTodosOnConsole(List<TodoItem> todos)
{
Console.WriteLine($"---Todos list--- ({todos.Count} items)");
foreach (TodoItem todo in todos)
{
Console.WriteLine($"{todo.Text}: {(todo.IsDone ? "Done" : "Not done")} ({todo.Id})");
}
}
public async Task<Guid> CreateTodoAsync(TodoItem todoItem)
{
Console.WriteLine("---Create todo item---");
using (var req = new HttpRequestMessage(HttpMethod.Post, $"{_settings.ApiBaseUrl}/api/todos"))
{
string accessToken = await GetAccessTokenAsync();
req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
req.Content = new StringContent(
JsonConvert.SerializeObject(todoItem),
Encoding.UTF8,
"application/json");
using (HttpResponseMessage res = await Client.SendAsync(req))
{
res.EnsureSuccessStatusCode();
string json = await res.Content.ReadAsStringAsync();
TodoItem createdTodo = JsonConvert.DeserializeObject<TodoItem>(json);
Console.WriteLine($"Created: {createdTodo.Text}: {(createdTodo.IsDone ? "Done" : "Not done")} ({createdTodo.Id})");
return createdTodo.Id;
}
}
}
public async Task DeleteTodoAsync(Guid id)
{
Console.WriteLine("---Delete todo item---");
using (var req = new HttpRequestMessage(HttpMethod.Delete, $"{_settings.ApiBaseUrl}/api/todos/{id}"))
{
string accessToken = await GetAccessTokenAsync();
req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (HttpResponseMessage res = await Client.SendAsync(req))
{
res.EnsureSuccessStatusCode();
Console.WriteLine($"Todo item deleted with id {id}");
}
}
}
private async Task<string> GetAccessTokenAsync()
{
var context = new AuthenticationContext(_settings.Authority);
AuthenticationResult result;
try
{
result = await context.AcquireTokenSilentAsync(_settings.ApiResourceUri, _settings.ClientId);
}
catch (AdalSilentTokenAcquisitionException)
{
DeviceCodeResult deviceCodeResult = await context.AcquireDeviceCodeAsync(_settings.ApiResourceUri, _settings.ClientId);
Console.WriteLine(deviceCodeResult.Message);
result = await context.AcquireTokenByDeviceCodeAsync(deviceCodeResult);
}
return result.AccessToken;
}
}
}