Skip to content

Commit

Permalink
RestRequestFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
o.nadymov committed Jun 7, 2024
1 parent 97af2a8 commit 2975116
Show file tree
Hide file tree
Showing 25 changed files with 318 additions and 206 deletions.
17 changes: 13 additions & 4 deletions src/Spoleto.RestClient/Authentication/StaticAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ namespace Spoleto.RestClient.Authentication
{
public sealed class StaticAuthenticator : AuthenticatorBase, IStaticAuthenticator
{
private readonly string _token;
private readonly string _tokenType;
private readonly string _token;

public StaticAuthenticator(string token, string tokenType)
public StaticAuthenticator(string tokenType, string token)
{
_token = token ?? throw new ArgumentNullException(nameof(token));
_tokenType = tokenType ?? throw new ArgumentNullException(nameof(tokenType));
_token = token ?? throw new ArgumentNullException(nameof(token));
}

public override Task Authenticate(IRestClient client, HttpRequestMessage request)
{
request.Headers.Authorization = new AuthenticationHeaderValue(_tokenType, _token);
if (IsStandardTokenType())
request.Headers.Authorization = new AuthenticationHeaderValue(_tokenType, _token);
else
request.Headers.Add(_tokenType, _token);

return Task.CompletedTask;
}

private bool IsStandardTokenType()
{
return _tokenType == "Bearer" || _tokenType == "bearer";

}
}
}
6 changes: 0 additions & 6 deletions src/Spoleto.RestClient/Client/IRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
{
public interface IRestClient : IDisposable
{
JsonRestRequest<TObj> CreateJsonRestRequest<TObj>(string uri, HttpMethod httpMethod = HttpMethod.Get, bool isMultipartFormData = false, TObj? content = null) where TObj : class;

XmlRestRequest<TObj> CreateXmlRestRequest<TObj>(string uri, HttpMethod httpMethod = HttpMethod.Get, bool isMultipartFormData = false, TObj? content = null) where TObj : class;

BinaryRestRequest CreateBinaryRestRequest(string uri, HttpMethod httpMethod = HttpMethod.Get, bool isMultipartFormData = false, byte[]? content = null);

Task<TextRestResponse> ExecuteAsStringAsync(RestRequest request, CancellationToken cancellationToken = default);

Task<BinaryRestResponse> ExecuteAsBytesAsync(RestRequest request, CancellationToken cancellationToken = default);
Expand Down
4 changes: 2 additions & 2 deletions src/Spoleto.RestClient/Client/RestClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public RestClientFactory WithOptions(RestClientOptions options)
/// <summary>
/// Creates the RestClient instance.
/// </summary>
/// <returns>Instance of <see cref="RestClient"/>.</returns>
/// <returns>Instance of <see cref="RestHttpClient"/>.</returns>
public IRestClient Build()
{
if (_client == null)
Expand All @@ -43,7 +43,7 @@ public IRestClient Build()
_disposeHttpClient = true;
}

return new RestClient(_client, _authenticator, _options, _disposeHttpClient);
return new RestHttpClient(_client, _authenticator, _options, _disposeHttpClient);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@

namespace Spoleto.RestClient
{
public class RestClient : IRestClient
public class RestHttpClient : IRestClient
{
private readonly HttpClient _httpClient;
private readonly IAuthenticator? _authenticator;
private readonly RestClientOptions _options;//todo:
private readonly bool _disposeHttpClient;

public RestClient(HttpClient httpClient, IAuthenticator? authenticator = null, RestClientOptions? options = null, bool disposeHttpClient = false)
public RestHttpClient(HttpClient httpClient, IAuthenticator? authenticator = null, RestClientOptions? options = null, bool disposeHttpClient = false)
{
_httpClient = httpClient;
_authenticator = authenticator;
_options = options ?? RestClientOptions.Default;
_disposeHttpClient = disposeHttpClient;
}

public virtual JsonRestRequest<TObj> CreateJsonRestRequest<TObj>(string uri, HttpMethod httpMethod = HttpMethod.Get, bool isMultipartFormData = false, TObj? content = null) where TObj : class => new(uri, httpMethod, isMultipartFormData, content);

public virtual XmlRestRequest<TObj> CreateXmlRestRequest<TObj>(string uri, HttpMethod httpMethod = HttpMethod.Get, bool isMultipartFormData = false, TObj? content = null) where TObj : class => new(uri, httpMethod, isMultipartFormData, content);

public virtual BinaryRestRequest CreateBinaryRestRequest(string uri, HttpMethod httpMethod = HttpMethod.Get, bool isMultipartFormData = false, byte[]? content = null) => new(uri, httpMethod, isMultipartFormData, content);

public async Task<TextRestResponse> ExecuteAsStringAsync(RestRequest request, CancellationToken cancellationToken = default)
{
var restResponse = await InvokeAsync<TextRestResponse>(request, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -54,15 +48,13 @@ public async Task<T> ExecuteAsync<T>(RestRequest request, CancellationToken canc

private async Task<T> InvokeAsync<T>(RestRequest request, CancellationToken cancellationToken = default) where T : IRestResponse, new()
{
using var requestMessage = new HttpRequestMessage(request.HttpMethod.ConvertToHttpMethod(), request.Uri);
using var requestMessage = request.ToHttpRequest();

if (_authenticator != null)
{
await _authenticator.Authenticate(this, requestMessage).ConfigureAwait(false);
}

requestMessage.Content = request.GetHttpContent();

using var responseMessage = await _httpClient.SendAsync(requestMessage, cancellationToken);

var restResponse = await responseMessage.ToRestResponse<T>(cancellationToken).ConfigureAwait(false);
Expand Down
16 changes: 8 additions & 8 deletions src/Spoleto.RestClient/Extensions/HttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ public static void InitializeException(this Exception exception, HttpResponseMes

}

public static System.Net.Http.HttpMethod ConvertToHttpMethod(this HttpMethod httpMethod)
public static System.Net.Http.HttpMethod ConvertToHttpMethod(this RestHttpMethod httpMethod)
=> httpMethod switch
{
HttpMethod.Get => System.Net.Http.HttpMethod.Get,
HttpMethod.Post => System.Net.Http.HttpMethod.Post,
HttpMethod.Put => System.Net.Http.HttpMethod.Put,
RestHttpMethod.Get => System.Net.Http.HttpMethod.Get,
RestHttpMethod.Post => System.Net.Http.HttpMethod.Post,
RestHttpMethod.Put => System.Net.Http.HttpMethod.Put,
#if NET
HttpMethod.Patch => System.Net.Http.HttpMethod.Patch,
RestHttpMethod.Patch => System.Net.Http.HttpMethod.Patch,
#else
HttpMethod.Patch => new System.Net.Http.HttpMethod("PATCH"),
RestHttpMethod.Patch => new System.Net.Http.HttpMethod("PATCH"),
#endif
HttpMethod.Delete => System.Net.Http.HttpMethod.Delete,
HttpMethod.Head => System.Net.Http.HttpMethod.Head,
RestHttpMethod.Delete => System.Net.Http.HttpMethod.Delete,
RestHttpMethod.Head => System.Net.Http.HttpMethod.Head,

_ => throw new ArgumentOutOfRangeException(nameof(httpMethod))
};
Expand Down
4 changes: 1 addition & 3 deletions src/Spoleto.RestClient/Objects/ContentTypes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Net.Mime;

namespace Spoleto.RestClient
namespace Spoleto.RestClient
{
/// <summary>
/// Content types.
Expand Down
9 changes: 9 additions & 0 deletions src/Spoleto.RestClient/Objects/DataFomat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Spoleto.RestClient
{
public enum DataFomat
{
Json,
Xml,
Binary
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Spoleto.RestClient
{
public enum HttpMethod
public enum RestHttpMethod
{
Get,
Post,
Expand Down
28 changes: 0 additions & 28 deletions src/Spoleto.RestClient/RestRequest/BinaryRestRequest.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Spoleto.RestClient/RestRequest/IBinaryRestRequest.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Spoleto.RestClient/RestRequest/IJsonRestRequest.cs

This file was deleted.

6 changes: 2 additions & 4 deletions src/Spoleto.RestClient/RestRequest/IRestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ namespace Spoleto.RestClient
{
public interface IRestRequest
{
HttpMethod HttpMethod { get; }
RestHttpMethod Method { get; }

bool IsMultipartFormData { get; }

string Uri { get; }

HttpContent GetHttpContent();
HttpContent? GetHttpContent();
}
}
7 changes: 0 additions & 7 deletions src/Spoleto.RestClient/RestRequest/IRestRequestGeneric.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Spoleto.RestClient/RestRequest/IXmlRestRequest.cs

This file was deleted.

38 changes: 0 additions & 38 deletions src/Spoleto.RestClient/RestRequest/JsonRestRequest.cs

This file was deleted.

51 changes: 30 additions & 21 deletions src/Spoleto.RestClient/RestRequest/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,50 @@

namespace Spoleto.RestClient
{
public abstract record RestRequest : IRestRequest
public record RestRequest : IRestRequest
{
protected RestRequest(string uri, HttpMethod httpMethod = HttpMethod.Get, bool isMultipartFormData = false)
private readonly HttpContent? _content;

private readonly Dictionary<string, string> _headers = [];

public RestRequest(RestHttpMethod httpMethod, string uri)
: this(httpMethod, uri, null)
{
Uri = uri;
HttpMethod = httpMethod;
IsMultipartFormData = isMultipartFormData;
ContentType = GetContentType();
}

public string Uri { get; }

public HttpMethod HttpMethod { get; }
public RestRequest(RestHttpMethod httpMethod, string uri, HttpContent? content = null)
{
Method = httpMethod;
Uri = uri;
_content = content;
}

public bool IsMultipartFormData { get; }
public RestHttpMethod Method { get; }

public string ContentType { get; set; }
public string Uri { get; }

public virtual Encoding Encoding { get; set; } = Encoding.UTF8;

public bool ThrowIfHttpError { get; set; } = true;

protected abstract string GetContentType();
public Dictionary<string, string> Headers => _headers;

public abstract HttpContent GetHttpContent();
}
public HttpContent? GetHttpContent() => _content;

public abstract record RestRequest<T> : RestRequest, IRestRequestGeneric<T> where T : class
{
protected RestRequest(string uri, HttpMethod httpMethod = HttpMethod.Get,bool isMultipartFormData=false, T? content = null)
: base(uri, httpMethod)
public HttpRequestMessage ToHttpRequest()
{
Content = content;
var request = new HttpRequestMessage(Method.ConvertToHttpMethod(), Uri);
if (_content != null)
{
request.Content = _content;
}

foreach (var header in _headers)
{
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
}

return request;
}

public T? Content { get; set; }
}
}
Loading

0 comments on commit 2975116

Please sign in to comment.