-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApiClient.cs
57 lines (52 loc) · 1.8 KB
/
ApiClient.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
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace Consumer
{
public class ApiClient
{
private readonly Uri BaseUri;
public ApiClient(Uri baseUri)
{
this.BaseUri = baseUri;
}
public async Task<HttpResponseMessage> GetAllProducts()
{
using (var client = new HttpClient { BaseAddress = BaseUri })
{
try
{
// client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeaderValue()); // STEP_8
var response = await client.GetAsync($"/api/products");
return response;
}
catch (Exception ex)
{
throw new Exception("There was a problem connecting to Products API.", ex);
}
}
}
public async Task<HttpResponseMessage> GetProduct(int id)
{
using (var client = new HttpClient { BaseAddress = BaseUri })
{
try
{
// client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeaderValue()); // STEP_8
var response = await client.GetAsync($"/api/product/{id}"); // STEP_1 - STEP_4
// var response = await client.GetAsync($"/api/products/{id}"); // STEP_5
return response;
}
catch (Exception ex)
{
throw new Exception("There was a problem connecting to Products API.", ex);
}
}
}
// // STEP_8
// private string AuthorizationHeaderValue()
// {
// return $"Bearer {DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")}";
// }
}
}