Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 113 additions & 2 deletions ClientProxyBase/ClientProxyBase.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json;
using SimpleResults;
using System;

namespace ClientProxyBase;

using Shared.Results;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -102,12 +105,120 @@
return JsonConvert.DeserializeObject<ResponseData<T>>(content);
}

protected virtual async Task<Result<TResponse>> SendGetRequest<TResponse>(String uri, String accessToken, CancellationToken cancellationToken)
{

HttpRequestMessage requestMessage = new(HttpMethod.Get, uri);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);

// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);

// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);

if (result.IsFailed)
return ResultHelpers.CreateFailure(result);

TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);

return Result.Success<TResponse>(responseData);
}

protected virtual async Task<Result<TResponse>> SendPostRequest<TRequest, TResponse>(String uri, String accessToken, TRequest content, CancellationToken cancellationToken)
{

HttpRequestMessage requestMessage = new(HttpMethod.Post, uri);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");

// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);

// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);

if (result.IsFailed)
return ResultHelpers.CreateFailure(result);

TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);

return Result.Success<TResponse>(responseData);
}

protected virtual async Task<Result<TResponse>> SendPutRequest<TRequest, TResponse>(String uri, String accessToken, TRequest content, CancellationToken cancellationToken)
{

HttpRequestMessage requestMessage = new(HttpMethod.Put, uri);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");

// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);

// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);

if (result.IsFailed)
return ResultHelpers.CreateFailure(result);

TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);

return Result.Success<TResponse>(responseData);
}

protected virtual async Task<Result<TResponse>> SendPatchRequest<TRequest, TResponse>(String uri, String accessToken, TRequest content, CancellationToken cancellationToken)
{

HttpRequestMessage requestMessage = new(HttpMethod.Patch, uri);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");

// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);

// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);

if (result.IsFailed)
return ResultHelpers.CreateFailure(result);

TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);

return Result.Success<TResponse>(responseData);
}

protected virtual async Task<Result<TResponse>> SendDeleteRequest<TResponse>(String uri, String accessToken, CancellationToken cancellationToken)
{

HttpRequestMessage requestMessage = new(HttpMethod.Delete, uri);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationSchemes.Bearer, accessToken);

// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);

// Process the response
Result<String> result = await this.HandleResponseX(httpResponse, cancellationToken);

if (result.IsFailed)
return ResultHelpers.CreateFailure(result);

TResponse responseData = JsonConvert.DeserializeObject<TResponse>(result.Data);

return Result.Success<TResponse>(responseData);
}


#endregion
}

public class ClientHttpException : Exception {
public ClientHttpException(string? message,

Check warning on line 216 in ClientProxyBase/ClientProxyBase.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests - Linux

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 216 in ClientProxyBase/ClientProxyBase.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests - Linux

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 216 in ClientProxyBase/ClientProxyBase.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests - Windows

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 216 in ClientProxyBase/ClientProxyBase.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests - Windows

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
Exception? innerException = null) : base(message, innerException) {

Check warning on line 217 in ClientProxyBase/ClientProxyBase.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests - Linux

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

}
}

public static class AuthenticationSchemes {
public static readonly String Bearer = "Bearer";
}
Loading