Skip to content

Commit

Permalink
fix obsolete code, clean up warning
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Jun 5, 2024
1 parent 27af755 commit 8227a91
Show file tree
Hide file tree
Showing 31 changed files with 140 additions and 156 deletions.
2 changes: 2 additions & 0 deletions src/FluentRest/ContentSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Ignore Spelling: Serializer

namespace FluentRest;

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/FluentRest/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;

namespace FluentRest;

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions src/FluentRest/FluentClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Net.Http;
// Ignore Spelling: Serializer

namespace FluentRest;

Expand Down
4 changes: 0 additions & 4 deletions src/FluentRest/FluentClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace FluentRest;

/// <summary>
Expand Down
5 changes: 0 additions & 5 deletions src/FluentRest/FluentDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace FluentRest;

Expand Down
2 changes: 0 additions & 2 deletions src/FluentRest/FluentProperties.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace FluentRest;

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/FluentRest/FormBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Net.Http;

namespace FluentRest;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/FluentRest/HeaderBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace FluentRest;
Expand Down
4 changes: 0 additions & 4 deletions src/FluentRest/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace FluentRest;

/// <summary>
Expand Down
87 changes: 64 additions & 23 deletions src/FluentRest/HttpMessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
// Ignore Spelling: Serializer Deserialize

namespace FluentRest;

Expand All @@ -13,6 +7,49 @@ namespace FluentRest;
/// </summary>
public static class HttpMessageExtensions
{
public static TValue GetOrAddOption<TValue>(this HttpRequestMessage requestMessage, string key, Func<string, TValue> valueFactory)
{
#if NET5_0_OR_GREATER
var optionKey = new HttpRequestOptionsKey<TValue>(key);
if (requestMessage.Options.TryGetValue(optionKey, out var value))
return value;

value = valueFactory(key);
requestMessage.Options.Set(optionKey, value);
return value;
#else
if (requestMessage.Properties.TryGetValue(key, out var propertyValue))
return (TValue)propertyValue;

propertyValue = valueFactory(key);
requestMessage.Properties.Add(key, propertyValue);

return (TValue)propertyValue;
#endif
}

public static bool TryGetOption<TValue>(this HttpRequestMessage requestMessage, string key, out TValue value)
{
#if NET5_0_OR_GREATER
var optionKey = new HttpRequestOptionsKey<TValue>(key);
return requestMessage.Options.TryGetValue(optionKey, out value);
#else
var found = requestMessage.Properties.TryGetValue(key, out var propertyValue);
value = found ? (TValue)propertyValue : default;
return found;
#endif
}

public static void SetOption<TValue>(this HttpRequestMessage requestMessage, string key, TValue value)
{
#if NET5_0_OR_GREATER
var optionKey = new HttpRequestOptionsKey<TValue>(key);
requestMessage.Options.Set(optionKey, value);
#else
requestMessage.Properties[key] = value;
#endif
}

/// <summary>
/// Gets the <see cref="UrlBuilder"/> from the specified <paramref name="requestMessage" /> properties dictionary.
/// </summary>
Expand All @@ -26,13 +63,13 @@ public static UrlBuilder GetUrlBuilder(this HttpRequestMessage requestMessage)
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

var propertyValue = requestMessage.Properties.GetOrAdd(FluentProperties.RequestUrlBuilder, k =>
var propertyValue = requestMessage.GetOrAddOption(FluentProperties.RequestUrlBuilder, k =>
requestMessage.RequestUri == null
? new UrlBuilder()
: new UrlBuilder(requestMessage.RequestUri)
);

return propertyValue as UrlBuilder;
return propertyValue;
}

/// <summary>
Expand All @@ -46,7 +83,7 @@ public static void SetUrlBuilder(this HttpRequestMessage requestMessage, UrlBuil
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties[FluentProperties.RequestUrlBuilder] = urlBuilder;
requestMessage.SetOption(FluentProperties.RequestUrlBuilder, urlBuilder);
}


Expand All @@ -63,7 +100,7 @@ public static object GetContentData(this HttpRequestMessage requestMessage)
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties.TryGetValue(FluentProperties.RequestContentData, out var propertyValue);
requestMessage.TryGetOption<object>(FluentProperties.RequestContentData, out var propertyValue);
return propertyValue;
}

Expand All @@ -78,7 +115,7 @@ public static void SetContentData(this HttpRequestMessage requestMessage, object
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties[FluentProperties.RequestContentData] = contentData;
requestMessage.SetOption(FluentProperties.RequestContentData, contentData);
}


Expand All @@ -95,8 +132,8 @@ public static Dictionary<string, ICollection<string>> GetFormData(this HttpReque
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

var propertyValue = requestMessage.Properties.GetOrAdd(FluentProperties.RequestFormData, k => new Dictionary<string, ICollection<string>>());
return propertyValue as Dictionary<string, ICollection<string>>;
var propertyValue = requestMessage.GetOrAddOption(FluentProperties.RequestFormData, k => new Dictionary<string, ICollection<string>>());
return propertyValue;
}


Expand All @@ -113,8 +150,10 @@ public static HttpCompletionOption GetCompletionOption(this HttpRequestMessage r
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties.TryGetValue(FluentProperties.HttpCompletionOption, out var propertyValue);
return (HttpCompletionOption)(propertyValue ?? HttpCompletionOption.ResponseContentRead);
if (requestMessage.TryGetOption<HttpCompletionOption>(FluentProperties.HttpCompletionOption, out var value))
return value;

return HttpCompletionOption.ResponseContentRead;
}

/// <summary>
Expand All @@ -128,7 +167,7 @@ public static void SetCompletionOption(this HttpRequestMessage requestMessage, H
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties[FluentProperties.HttpCompletionOption] = completionOption;
requestMessage.SetOption(FluentProperties.HttpCompletionOption, completionOption);
}


Expand All @@ -145,8 +184,10 @@ public static CancellationToken GetCancellationToken(this HttpRequestMessage req
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties.TryGetValue(FluentProperties.CancellationToken, out var propertyValue);
return (CancellationToken)(propertyValue ?? CancellationToken.None);
if (requestMessage.TryGetOption<CancellationToken>(FluentProperties.CancellationToken, out var propertyValue))
return propertyValue;

return CancellationToken.None;
}

/// <summary>
Expand All @@ -160,7 +201,7 @@ public static void SetCancellationToken(this HttpRequestMessage requestMessage,
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties[FluentProperties.CancellationToken] = cancellationToken;
requestMessage.SetOption(FluentProperties.CancellationToken, cancellationToken);
}


Expand All @@ -177,8 +218,8 @@ public static IContentSerializer GetContentSerializer(this HttpRequestMessage re
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

var propertyValue = requestMessage.Properties.GetOrAdd(FluentProperties.ContentSerializer, k => ContentSerializer.Current);
return propertyValue as IContentSerializer;
var propertyValue = requestMessage.GetOrAddOption(FluentProperties.ContentSerializer, k => ContentSerializer.Current);
return propertyValue;
}

/// <summary>
Expand All @@ -192,7 +233,7 @@ public static void SetContentSerializer(this HttpRequestMessage requestMessage,
if (requestMessage == null)
throw new ArgumentNullException(nameof(requestMessage));

requestMessage.Properties[FluentProperties.ContentSerializer] = contentSerializer ?? ContentSerializer.Current;
requestMessage.SetOption(FluentProperties.ContentSerializer, contentSerializer ?? ContentSerializer.Current);
}


Expand Down
2 changes: 0 additions & 2 deletions src/FluentRest/HttpRequestHeaders.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace FluentRest;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/FluentRest/HttpResponseHeaders.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace FluentRest;

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/FluentRest/IContentSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace FluentRest;

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/FluentRest/IFluentClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Net.Http;

namespace FluentRest;

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/FluentRest/JsonContentSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace FluentRest;

Expand Down
6 changes: 1 addition & 5 deletions src/FluentRest/PostBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

Expand Down Expand Up @@ -175,7 +171,7 @@ public TBuilder Content<TData>(TData data)
return Content(httpContent);

// save for later serialization
RequestMessage.Properties[FluentProperties.RequestContentData] = data;
RequestMessage.SetOption(FluentProperties.RequestContentData, data);

if (RequestMessage.Method == HttpMethod.Get)
RequestMessage.Method = HttpMethod.Post;
Expand Down
3 changes: 0 additions & 3 deletions src/FluentRest/ProblemDetails.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace FluentRest;
Expand Down
7 changes: 3 additions & 4 deletions src/FluentRest/ProblemDetailsConverter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace FluentRest;

internal sealed class ProblemDetailsConverter : JsonConverter<ProblemDetails>
{
private static readonly JsonEncodedText Type = JsonEncodedText.Encode("type");
private static readonly JsonEncodedText ProblemType = JsonEncodedText.Encode("type");
private static readonly JsonEncodedText Title = JsonEncodedText.Encode("title");
private static readonly JsonEncodedText Status = JsonEncodedText.Encode("status");
private static readonly JsonEncodedText Detail = JsonEncodedText.Encode("detail");
Expand Down Expand Up @@ -37,7 +36,7 @@ public override void Write(Utf8JsonWriter writer, ProblemDetails value, JsonSeri

internal static void ReadValue(ref Utf8JsonReader reader, ProblemDetails value, JsonSerializerOptions options)
{
if (TryReadStringProperty(ref reader, Type, out var propertyValue))
if (TryReadStringProperty(ref reader, ProblemType, out var propertyValue))
{
value.Type = propertyValue;
}
Expand Down Expand Up @@ -83,7 +82,7 @@ internal static bool TryReadStringProperty(ref Utf8JsonReader reader, JsonEncode
internal static void WriteProblemDetails(Utf8JsonWriter writer, ProblemDetails value, JsonSerializerOptions options)
{
if (value.Type != null)
writer.WriteString(Type, value.Type);
writer.WriteString(ProblemType, value.Type);

if (value.Title != null)
writer.WriteString(Title, value.Title);
Expand Down
4 changes: 3 additions & 1 deletion src/FluentRest/ProblemDetailsJsonContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json;
// Ignore Spelling: Json

using System.Text.Json;
using System.Text.Json.Serialization;

namespace FluentRest;
Expand Down
5 changes: 0 additions & 5 deletions src/FluentRest/ProblemException.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using System;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace FluentRest;

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/FluentRest/QueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Net.Http;

namespace FluentRest;

/// <summary>
Expand Down
6 changes: 1 addition & 5 deletions src/FluentRest/RequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Net.Http;
using System.Threading;

namespace FluentRest;

/// <summary>
Expand Down Expand Up @@ -63,7 +59,7 @@ public TBuilder State(string key, object value)
if (string.IsNullOrEmpty(key))
throw new ArgumentException("Argument is null or empty", nameof(key));

RequestMessage.Properties[key] = value;
RequestMessage.SetOption(key, value);
return this as TBuilder;
}
}
1 change: 0 additions & 1 deletion src/FluentRest/RequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Text;

namespace FluentRest;
Expand Down
3 changes: 0 additions & 3 deletions src/FluentRest/SendBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Net.Http;

namespace FluentRest;

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/FluentRest/UrlBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FluentRest;
Expand Down
Loading

0 comments on commit 8227a91

Please sign in to comment.