From c1516a83fe8b83a945281c9f24d2bb9b05faab67 Mon Sep 17 00:00:00 2001 From: Int32Overflow Date: Wed, 12 Apr 2023 10:49:35 +0200 Subject: [PATCH] Add HttpRequestMessageOptions to RefitSettings and add this dictionary to HttpRequestMessage.(Options/Properties) (#1353) Co-authored-by: Glenn <5834289+glennawatson@users.noreply.github.com> Co-authored-by: Chris Pulman --- Refit.Tests/RequestBuilder.cs | 36 +++++++++++++++++++++++++++ Refit/RefitSettings.cs | 8 +++++- Refit/RequestBuilderImplementation.cs | 13 ++++++++++ version.json | 2 +- 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Refit.Tests/RequestBuilder.cs b/Refit.Tests/RequestBuilder.cs index 5eb0f741e..31fe360cb 100644 --- a/Refit.Tests/RequestBuilder.cs +++ b/Refit.Tests/RequestBuilder.cs @@ -2120,6 +2120,42 @@ public void DynamicRequestPropertiesShouldBeInProperties(string interfaceMethodN #pragma warning restore CS0618 // Type or member is obsolete } + [Fact] + public void OptionsFromSettingsShouldBeInProperties() + { + const string nameProp1 = "UnitTest.Property1"; + string valueProp1 = "TestValue"; + const string nameProp2 = "UnitTest.Property2"; + object valueProp2 = new List() { "123", "345"}; + var fixture = new RequestBuilderImplementation(new RefitSettings() + { + HttpRequestMessageOptions=new Dictionary() + { + [nameProp1] = valueProp1, + [nameProp2] = valueProp2, + }, + }); + var factory = fixture.BuildRequestFactoryForMethod(nameof(IContainAandB.Ping)); + var output = factory(new object[] { }); + +#if NET5_0_OR_GREATER + Assert.NotEmpty(output.Options); + Assert.True(output.Options.TryGetValue(new HttpRequestOptionsKey(nameProp1), out var resultValueProp1)); + Assert.Equal(valueProp1, resultValueProp1); + + Assert.True(output.Options.TryGetValue(new HttpRequestOptionsKey>(nameProp2), out var resultValueProp2)); + Assert.Equal(valueProp2, resultValueProp2); +#else + Assert.NotEmpty(output.Properties); + Assert.True(output.Properties.TryGetValue(nameProp1, out var resultValueProp1)); + Assert.IsType(resultValueProp1); + Assert.Equal(valueProp1, (string)resultValueProp1); + + Assert.True(output.Properties.TryGetValue(nameProp2, out var resultValueProp2)); + Assert.IsType>(resultValueProp2); + Assert.Equal(valueProp2, (List)resultValueProp2); +#endif + } [Fact] public void InterfaceTypeShouldBeInProperties() diff --git a/Refit/RefitSettings.cs b/Refit/RefitSettings.cs index 0ada3d865..11cd8353e 100644 --- a/Refit/RefitSettings.cs +++ b/Refit/RefitSettings.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net.Http; @@ -108,7 +109,12 @@ public RefitSettings( /// public bool Buffered { get; set; } = false; -#if NET5_0_OR_GREATER + /// + /// Optional Key-Value pairs, which are displayed in the property or . + /// + public Dictionary HttpRequestMessageOptions { get; set; } + +#if NET6_0_OR_GREATER /// /// Controls injecting the of the method on the Refit client interface that was invoked into the HttpRequestMessage.Options (defaults to false) /// diff --git a/Refit/RequestBuilderImplementation.cs b/Refit/RequestBuilderImplementation.cs index 5085917ba..66c860333 100644 --- a/Refit/RequestBuilderImplementation.cs +++ b/Refit/RequestBuilderImplementation.cs @@ -724,6 +724,19 @@ Func BuildRequestFactoryForMethod(RestMethodInfo r } } + // Add RefitSetting.HttpRequestMessageOptions to the HttpRequestMessage + if (this.settings.HttpRequestMessageOptions != null) + { + foreach(var p in this.settings.HttpRequestMessageOptions) + { +#if NET5_0_OR_GREATER + ret.Options.Set(new HttpRequestOptionsKey(p.Key), p.Value); +#else + ret.Properties.Add(p); +#endif + } + } + foreach (var property in propertiesToAdd) { #if NET6_0_OR_GREATER diff --git a/version.json b/version.json index aa3467eaf..76e6f9015 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { - "version": "6.3", + "version": "6.4", "publicReleaseRefSpec": [ "^refs/heads/main$", // we release out of main "^refs/heads/rel/v\\d+\\.\\d+" // we also release branches starting with vN.N