Skip to content

Commit 9b280a1

Browse files
Merge pull request #1028 from TransactionProcessing/task/#1027_base_http_methods
base http methods added to ClientProxy base
2 parents 2329d35 + fc3e566 commit 9b280a1

File tree

1 file changed

+113
-2
lines changed

1 file changed

+113
-2
lines changed

ClientProxyBase/ClientProxyBase.cs

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
32
using SimpleResults;
3+
using System;
44

55
namespace ClientProxyBase;
66

7+
using Shared.Results;
78
using System.Collections;
89
using System.Collections.Generic;
910
using System.IO;
1011
using System.Net;
1112
using System.Net.Http;
13+
using System.Net.Http.Headers;
14+
using System.Text;
1215
using System.Threading;
1316
using System.Threading.Tasks;
1417

@@ -102,6 +105,110 @@ protected virtual ResponseData<T> HandleResponseContent<T>(String content)
102105
return JsonConvert.DeserializeObject<ResponseData<T>>(content);
103106
}
104107

108+
protected virtual async Task<Result<TResponse>> SendGetRequest<TResponse>(String uri, String accessToken, CancellationToken cancellationToken)
109+
{
110+
111+
HttpRequestMessage requestMessage = new(HttpMethod.Get, uri);
112+
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
113+
114+
// Make the Http Call here
115+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);
116+
117+
// Process the response
118+
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
119+
120+
if (result.IsFailed)
121+
return ResultHelpers.CreateFailure(result);
122+
123+
TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);
124+
125+
return Result.Success<TResponse>(responseData);
126+
}
127+
128+
protected virtual async Task<Result<TResponse>> SendPostRequest<TRequest, TResponse>(String uri, String accessToken, TRequest content, CancellationToken cancellationToken)
129+
{
130+
131+
HttpRequestMessage requestMessage = new(HttpMethod.Post, uri);
132+
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
133+
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
134+
135+
// Make the Http Call here
136+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);
137+
138+
// Process the response
139+
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
140+
141+
if (result.IsFailed)
142+
return ResultHelpers.CreateFailure(result);
143+
144+
TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);
145+
146+
return Result.Success<TResponse>(responseData);
147+
}
148+
149+
protected virtual async Task<Result<TResponse>> SendPutRequest<TRequest, TResponse>(String uri, String accessToken, TRequest content, CancellationToken cancellationToken)
150+
{
151+
152+
HttpRequestMessage requestMessage = new(HttpMethod.Put, uri);
153+
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
154+
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
155+
156+
// Make the Http Call here
157+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);
158+
159+
// Process the response
160+
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
161+
162+
if (result.IsFailed)
163+
return ResultHelpers.CreateFailure(result);
164+
165+
TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);
166+
167+
return Result.Success<TResponse>(responseData);
168+
}
169+
170+
protected virtual async Task<Result<TResponse>> SendPatchRequest<TRequest, TResponse>(String uri, String accessToken, TRequest content, CancellationToken cancellationToken)
171+
{
172+
173+
HttpRequestMessage requestMessage = new(HttpMethod.Patch, uri);
174+
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
175+
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
176+
177+
// Make the Http Call here
178+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);
179+
180+
// Process the response
181+
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
182+
183+
if (result.IsFailed)
184+
return ResultHelpers.CreateFailure(result);
185+
186+
TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);
187+
188+
return Result.Success<TResponse>(responseData);
189+
}
190+
191+
protected virtual async Task<Result<TResponse>> SendDeleteRequest<TResponse>(String uri, String accessToken, CancellationToken cancellationToken)
192+
{
193+
194+
HttpRequestMessage requestMessage = new(HttpMethod.Delete, uri);
195+
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
196+
197+
// Make the Http Call here
198+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);
199+
200+
// Process the response
201+
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);
202+
203+
if (result.IsFailed)
204+
return ResultHelpers.CreateFailure(result);
205+
206+
TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);
207+
208+
return Result.Success<TResponse>(responseData);
209+
}
210+
211+
105212
#endregion
106213
}
107214

@@ -110,4 +217,8 @@ public ClientHttpException(string? message,
110217
Exception? innerException = null) : base(message, innerException) {
111218

112219
}
220+
}
221+
222+
public static class AuthenticationSchemes {
223+
public static readonly String Bearer = "Bearer";
113224
}

0 commit comments

Comments
 (0)