Skip to content

Commit

Permalink
Enable nullable on RestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSmoke committed Jan 12, 2024
1 parent 9ca6100 commit 861368b
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net462</TargetFrameworks>
<PackageTags>restclient rest http</PackageTags>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/RestClient/RestClient/src/ErrorBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace ClickView.Extensions.RestClient
{
public class ErrorBody
{
public string Message { get; set; }
public string Message { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public static class EnumerableExtensions
{
public static bool TryGetFirstValue<T>(this IEnumerable<T> source, out T value)
public static bool TryGetFirstValue<T>(this IEnumerable<T> source, out T? value)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
Expand Down
35 changes: 17 additions & 18 deletions src/RestClient/RestClient/src/Extensions/HttpHeadersExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
namespace ClickView.Extensions.RestClient.Extensions
{
using System.Net.Http.Headers;
namespace ClickView.Extensions.RestClient.Extensions;

using System.Net.Http.Headers;

public static class HttpHeadersExtensions
public static class HttpHeadersExtensions
{
public static bool TryGetFirstHeaderValue(this HttpHeaders headers, string name, out string? value)
{
public static bool TryGetFirstHeaderValue(this HttpHeaders headers, string name, out string value)
if (!headers.TryGetValues(name, out var values))
{
if (!headers.TryGetValues(name, out var values))
{
value = null;
return false;
}

if (!values.TryGetFirstValue(out var first))
{
value = null;
return false;
}
value = null;
return false;
}

value = first;
return true;
if (!values.TryGetFirstValue(out var first))
{
value = null;
return false;
}

value = first;
return true;
}
}
2 changes: 1 addition & 1 deletion src/RestClient/RestClient/src/Helpers/ErrorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public static class ErrorHelper
{
public static string GetDefaultErrorMessage(HttpStatusCode httpStatusCode)
public static string? GetDefaultErrorMessage(HttpStatusCode httpStatusCode)
{
switch (httpStatusCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace ClickView.Extensions.RestClient.Http
public class CoreRestClientOptions
{
public CompressionMethod CompressionMethod { get; set; } = CompressionMethod.None;
public IAuthenticator Authenticator { get; set; } = null;
public IAuthenticator? Authenticator { get; set; } = null;
}
}
17 changes: 17 additions & 0 deletions src/RestClient/RestClient/src/NotNullWhenAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if NETFRAMEWORK
namespace System.Diagnostics.CodeAnalysis;

using System;

internal sealed class NotNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter will not be null.
/// </param>
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;

/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}
#endif
10 changes: 5 additions & 5 deletions src/RestClient/RestClient/src/Requests/BaseRestClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace ClickView.Extensions.RestClient.Requests
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -28,7 +29,7 @@ protected BaseRestClientRequest(HttpMethod method, string resource)

public ISerializer Serializer { internal get; set; } = NewtonsoftJsonSerializer.Instance;

internal HttpContent Content { get; set; }
internal HttpContent? Content { get; set; }

/// <summary>
/// Set to true if we should throw an exception on 404
Expand Down Expand Up @@ -119,7 +120,6 @@ internal async Task<TResponse> GetResponseAsync(HttpResponseMessage message)
if (error != null)
{
HandleError(error);

response.Error = error;
}

Expand All @@ -128,7 +128,7 @@ internal async Task<TResponse> GetResponseAsync(HttpResponseMessage message)

protected abstract Task<TResponse> ParseResponseAsync(HttpResponseMessage message);

protected virtual bool TryParseErrorBody(string content, out ErrorBody error)
protected virtual bool TryParseErrorBody(string content, [NotNullWhen(true)] out ErrorBody? error)
{
error = null;
return false;
Expand All @@ -142,7 +142,7 @@ protected virtual ErrorBody GetDefaultErrorBody(HttpStatusCode httpStatusCode, s
};
}

protected async Task<Error> GetErrorAsync(HttpResponseMessage message)
protected async Task<Error?> GetErrorAsync(HttpResponseMessage message)
{
if (message == null)
throw new ArgumentNullException(nameof(message));
Expand Down Expand Up @@ -170,7 +170,7 @@ protected virtual void HandleError(Error error)
throw ErrorHelper.GetErrorException(error);
}

protected T Deserialize<T>(string input)
protected T? Deserialize<T>(string input)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override async Task<RestClientResponse<TData>> ParseResponseAsync(Http
return new RestClientResponse<TData>(message, Deserialize(str));
}

protected TData Deserialize(string input)
protected TData? Deserialize(string input)
{
return Deserialize<TData>(input);
}
Expand Down
6 changes: 3 additions & 3 deletions src/RestClient/RestClient/src/Responses/RestClientResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public RestClientResponse(HttpResponseMessage message)
}

public HttpStatusCode HttpStatusCode { get; }
public Error Error { get; internal set; }
public Error? Error { get; internal set; }
public HttpResponseHeaders Headers { get; }
}

public class RestClientResponse<T> : RestClientResponse
{
public RestClientResponse(HttpResponseMessage message, T data) : base(message)
public RestClientResponse(HttpResponseMessage message, T? data) : base(message)
{
Data = data;
}

public T Data { get; internal set; }
public T? Data { get; internal set; }
}
}
9 changes: 5 additions & 4 deletions src/RestClient/RestClient/src/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class RestClient
{
private readonly Uri _baseAddress;
private readonly Uri? _baseAddress;
private readonly HttpClient _httpClient;
private readonly CoreRestClientOptions _options;

Expand All @@ -21,7 +21,7 @@ public class RestClient
/// </summary>
/// <param name="baseAddress"></param>
/// <param name="options"></param>
public RestClient(Uri baseAddress, RestClientOptions options = null)
public RestClient(Uri baseAddress, RestClientOptions? options = null)
{
_baseAddress = baseAddress ?? throw new ArgumentNullException(nameof(baseAddress));

Expand All @@ -36,10 +36,11 @@ public RestClient(Uri baseAddress, RestClientOptions options = null)
/// </summary>
/// <param name="httpClient"></param>
/// <param name="options"></param>
public RestClient(HttpClient httpClient, CoreRestClientOptions options = null)
public RestClient(HttpClient httpClient, CoreRestClientOptions? options = null)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_options = options ?? RestClientOptions.Default;
_baseAddress = null;
}

/// <summary>
Expand All @@ -48,7 +49,7 @@ public RestClient(HttpClient httpClient, CoreRestClientOptions options = null)
/// <param name="baseAddress"></param>
/// <param name="httpClient"></param>
/// <param name="options"></param>
public RestClient(Uri baseAddress, HttpClient httpClient, CoreRestClientOptions options = null)
public RestClient(Uri baseAddress, HttpClient httpClient, CoreRestClientOptions? options = null)
: this(httpClient, options)
{
_baseAddress = baseAddress ?? throw new ArgumentNullException(nameof(baseAddress));
Expand Down
4 changes: 2 additions & 2 deletions src/RestClient/RestClient/src/Serialization/ISerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface ISerializer

string Serialize(object obj);

T Deserialize<T>(string input);
object Deserialize(string input, Type type);
T? Deserialize<T>(string input);
object? Deserialize(string input, Type type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class NewtonsoftJsonSerializer : ISerializer
{
public static readonly ISerializer Instance = new NewtonsoftJsonSerializer();

private readonly JsonSerializerSettings _jsonSerializerSettings;
private readonly JsonSerializerSettings? _jsonSerializerSettings;

public NewtonsoftJsonSerializer()
{
Expand All @@ -27,12 +27,12 @@ public string Serialize(object obj)
return JsonConvert.SerializeObject(obj, _jsonSerializerSettings);
}

public T Deserialize<T>(string input)
public T? Deserialize<T>(string input)
{
return JsonConvert.DeserializeObject<T>(input, _jsonSerializerSettings);
}

public object Deserialize(string input, Type type)
public object? Deserialize(string input, Type type)
{
return JsonConvert.DeserializeObject(input, type, _jsonSerializerSettings);
}
Expand Down

0 comments on commit 861368b

Please sign in to comment.