-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientFactory.cs
More file actions
127 lines (109 loc) · 4.02 KB
/
Copy pathHttpClientFactory.cs
File metadata and controls
127 lines (109 loc) · 4.02 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
namespace ClientFactory;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Net.Http.Json;
public class HttpServiceFactory : IHttpService
{
private readonly HttpClient _httpClient;
public HttpServiceFactory()
{
_httpClient = new HttpClient();
}
public async Task<HttpResponseMessage> Get(string accessToken, string uri)
{
try
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await _httpClient.GetAsync(uri);
return response;
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
throw;
}
}
public async Task<HttpResponseMessage> GetWithTime(string accessToken, string uri, TimeSpan timeout = default(TimeSpan))
{
try
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await _httpClient.GetAsync(uri);
return response;
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
throw;
}
}
public async Task<HttpResponseMessage> GetLims(string uri, TimeSpan timeout = default(TimeSpan))
{
try
{
HttpResponseMessage response = await _httpClient.GetAsync(uri);
return response;
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
throw;
}
}
public async Task<HttpResponseMessage> Post(string accessToken, string uri, object objValue)
{
try
{
// Set the authorization header with the provided access token
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// Send the POST request with the object serialized as JSON
HttpResponseMessage response = await _httpClient.PostAsJsonAsync(uri, objValue);
// Ensure the response indicates success; otherwise, throw an exception
response.EnsureSuccessStatusCode();
// Return the response
return response;
}
catch (HttpRequestException e)
{
// Log the error message to the console and rethrow the exception
Console.WriteLine($"Request error: {e.Message}");
throw;
}
}
public async Task<HttpResponseMessage> Put(string accessToken, string uri, object objValue)
{
try
{
// Set the Authorization header
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// Send the PUT request with JSON content
HttpResponseMessage response = await _httpClient.PutAsJsonAsync(uri, objValue);
return response;
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
throw;
}
}
public async Task<HttpResponseMessage> Delete(string accessToken, string uri)
{
try
{
// Set the Authorization header
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// Send the DELETE request
HttpResponseMessage response = await _httpClient.DeleteAsync(uri);
// Ensure the response indicates success; otherwise, throw an exception
response.EnsureSuccessStatusCode();
return response;
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
throw;
}
}
}